2012-02-14 87 views
0

我使用按钮点击使用报警管理器启动服务。它也在工作。我这样做:通过广播接收机自动启动服务

Intent scheduleSMS = new Intent(SMSProjectDatabase.this,SendSMS.class); 
    PendingIntent pendingIntent = PendingIntent.getService(SMSProjectDatabase.this,0, scheduleSMS,0); 
    AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE); 

    Calendar calendar = Calendar.getInstance(); 

    calendar.setTimeInMillis(System.currentTimeMillis()); 
    calendar.add(Calendar.SECOND,(int)GeneralUtil.calculateTimeDifference(getApplicationContext())); 
    Log.i("SMS is scheduled after ",""+GeneralUtil.calculateTimeDifference(getApplicationContext())); 

    alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent); 

这里SendSMS是一个服务,将由警报管理器启动。但这不是我想要在我的代码中启动服务的方式。我想使用Alarmmanager为Broadcastreceiver触发广播。 Broadcastreceiver然后为我启动服务。

我的广播接收装置,

公共类MyReceiver扩展广播接收器{

@Override 
public void onReceive(Context context, Intent intent) { 
    //here i want to start the service through the alarm manager 

} 

}

但我唐诺我应该怎么办this.Please提供相应的解决方案为我解决这个问题。

回答

0

你只是单纯的需要把你的东西使用AlarmManageronReceive()

@Override 
public void onReceive(Context arg0, Intent arg1) { 
    // To get AlarmManager instance working you can use Context arg0 
    AlarmManager alarmManager = (AlarmManager) 
              arg0.getSystemService(ALARM_SERVICE); 
    // put your stuff to start Service using AlarmManager 
} 
内调用 Service
0

您可以使用registerReceiver()函数。 但是你想让阿拉姆曼斯格触发广播。广播接收器用于在广播应用程序时接收广播。