我使用闹钟管理器使用按钮点击启动服务。它也在工作。我这样做了:
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为我启动服务。
我的BroadCast接收器
公共类MyReceiver扩展了BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
//here i want to start the service through the alarm manager
}
}
但我不知道我该怎么做。请为我解决这个问题提供合适的解决方案。
答案 0 :(得分:0)
您可以使用registerReceiver()函数。 但是你希望警报管理器触发广播。广播接收器用于在向应用程序广播某些内容时接收广播。
答案 1 :(得分:0)
您只需在Service
AlarmManager
将您的内容用于调用onReceive()
即可
@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
}