如何在Android中使用Alarm Manager启动服务?
在我的应用程序中,我尝试使用警报管理器启动服务。当我单击按钮时,服务应该在我给出的特定时间启动。 我的警报管理器代码如下:
public void onClick(View view)
{
if(view == m_btnActivate)
{
Calendar cur_cal = Calendar.getInstance();
cur_cal.setTimeInMillis(System.currentTimeMillis());
cur_cal.add(Calendar.SECOND, 50);
Log.d("Testing", "Calender Set time:"+cur_cal.getTime());
Intent intent = new Intent(DashboardScreen.this, ServiceClass.class);
Log.d("Testing", "Intent created");
PendingIntent pi = PendingIntent.getService(DashboardScreen.this, 0, intent, 0);
AlarmManager alarm_manager = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
alarm_manager.set(AlarmManager.RTC, cur_cal.getTimeInMillis(), pi);
Log.d("Testing", "alarm manager set");
Toast.makeText(this, "ServiceClass.onCreate()", Toast.LENGTH_LONG).show();
}
}
下面是我的服务类:
public class ServiceClass extends Service{
@Override
public void onCreate() {
// TODO Auto-generated method stub
super.onCreate();
Log.d("Testing", "Service got created");
Toast.makeText(this, "ServiceClass.onCreate()", Toast.LENGTH_LONG).show();
}
@Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
return null;
}
@Override
public void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
}
@Override
public void onStart(Intent intent, int startId) {
// TODO Auto-generated method stub
super.onStart(intent, startId);
Toast.makeText(this, "ServiceClass.onStart()", Toast.LENGTH_LONG).show();
Log.d("Testing", "Service got started");
}
}
如果服务将启动,它应该打印服务类中的日志消息。但它没有显示。有人可以帮忙吗?
In my application, I am trying to start a service using Alarm manager. When I am clicking a button service should start in a particular time which I am giving.
My code for Alarm Manager is given below:
public void onClick(View view)
{
if(view == m_btnActivate)
{
Calendar cur_cal = Calendar.getInstance();
cur_cal.setTimeInMillis(System.currentTimeMillis());
cur_cal.add(Calendar.SECOND, 50);
Log.d("Testing", "Calender Set time:"+cur_cal.getTime());
Intent intent = new Intent(DashboardScreen.this, ServiceClass.class);
Log.d("Testing", "Intent created");
PendingIntent pi = PendingIntent.getService(DashboardScreen.this, 0, intent, 0);
AlarmManager alarm_manager = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
alarm_manager.set(AlarmManager.RTC, cur_cal.getTimeInMillis(), pi);
Log.d("Testing", "alarm manager set");
Toast.makeText(this, "ServiceClass.onCreate()", Toast.LENGTH_LONG).show();
}
}
And bellow is my Service Class:
public class ServiceClass extends Service{
@Override
public void onCreate() {
// TODO Auto-generated method stub
super.onCreate();
Log.d("Testing", "Service got created");
Toast.makeText(this, "ServiceClass.onCreate()", Toast.LENGTH_LONG).show();
}
@Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
return null;
}
@Override
public void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
}
@Override
public void onStart(Intent intent, int startId) {
// TODO Auto-generated method stub
super.onStart(intent, startId);
Toast.makeText(this, "ServiceClass.onStart()", Toast.LENGTH_LONG).show();
Log.d("Testing", "Service got started");
}
}
If the service will start it should print the log messages in the service class. But it is not showing. Can any one help?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这就是我所使用的,用于在当前时间 30 秒后启动服务,
尝试一下,让我知道会发生什么...
编辑:
在你的 manifest.xml 文件中
This is what I have used, for starting service after 30 seconds from current time,
Try it, and let me know what happen...
EDIT:
In your manifest.xml file
我知道这是一个老问题,但为了帮助谷歌的人们,这里是如何让你的服务保持活力的方法。
该服务每 5 秒被终止并重新启动一次。
I know this is an old question, but just to help the people from google, here is how you can keep your service alive.
The service is killed and restarted every 5 seconds.