如何在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(); }}
而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"); }}
如果服务将启动,它应该在服务类中打印日志消息。但它没有表现出来。任何人都可以帮忙吗?
慕码人2483693
跃然一笑
相关分类