[英]How to start Service using Alarm Manager in Android?
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: 而bellow是我的服务类:
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?
任何人都可以帮忙吗?
This is what I have used, for starting service after 30 seconds from current time, 这是我用过的,从当前时间开始服务30秒后,
Intent intent = new Intent(DashboardScreen.this, ServiceClass.class);
PendingIntent pintent = PendingIntent.getService(DashboardScreen.this, 0, intent, 0);
AlarmManager alarm = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
alarm.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), 30*1000, pintent);
Try it, and let me know what happen... 尝试一下,让我知道发生了什么......
EDIT: 编辑:
In your manifest.xml file 在manifest.xml文件中
<service android:enabled="true" android:name=".ServiceClass" />
I know this is an old question, but just to help the people from google, here is how you can keep your service alive. 我知道这是一个老问题,但只是为了帮助谷歌的人们,这里是你如何保持你的服务活着。
Intent ishintent = new Intent(this, HeartBeat.class);
PendingIntent pintent = PendingIntent.getService(this, 0, ishintent, 0);
AlarmManager alarm = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
alarm.cancel(pintent);
alarm.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(),5000, pintent);
The service is killed and restarted every 5 seconds. 该服务每5秒钟被杀死并重新启动一次。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.