将 Service Inside BroadcastReceiver 与定期任务一起使用 In Android
Use Service Inside BroadcastReceiver with periodic task In Android
我试图在 BroadcastReceiver
中启动 Service
。 BroadcastReceiver
收到 BOOT_COMPLETED
。我需要定期启动 Service
。我试过下面的代码:
BroadcastReceiver
:
public class SainarReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
App.setContext(context);
ExceptionHelpers.dLog("SainrCoService", SainarReceiver.class.getSimpleName() + " on Receive : " + App.getString(R.string.app_name));
SainarService sainarService = new SainarService(context);
Intent mIntent = new Intent(context , sainarService.getClass());
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, mIntent, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + 1000, 1000, pendingIntent);
}
}
Service
:
public class SainarService 扩展服务 {
public SainarService(){
}
public SainarService(Context iContext){
this.iContext = iContext;
App.setContext(iContext);
ExceptionHelpers.dLog("SainrCoService", SainarService.class.getSimpleName() + " Constructor : " + App.getString(R.string.app_name));
}
private Context iContext;
@Override
public IBinder onBind(Intent intent) {
// ExceptionHelpers.dLog("SainrCoService", SainarService.class.getSimpleName() + " on Bind : " + App.getString(R.string.app_name));
return null;
}
@Override
public void onCreate() {
super.onCreate();
ExceptionHelpers.dLog("SainrCoService", SainarService.class.getSimpleName() + " on Create : " + App.getString(R.string.app_name));
}
@Override
public void onStart(Intent intent, int startId) {
super.onStart(intent, startId);
ExceptionHelpers.dLog("SainrCoService", SainarService.class.getSimpleName() + " on Start : " + App.getString(R.string.app_name));
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
ExceptionHelpers.dLog("SainrCoService", SainarService.class.getSimpleName() + " on Start Command : " + App.getString(R.string.app_name));
return super.onStartCommand(intent, flags, startId);
}
}
清单:
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
<receiver
android:name="com.emaarit.app.sainarco.receivers.SainarReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
</intent-filter>
</receiver>
<service android:enabled="true" android:name="com.emaarit.app.sainarco.receivers.SainarService"/>
在 BOOT_COMPLETED
上 BroadcastReceiver
开始,但 Service
没有!知道这里出了什么问题吗?
使用 PendingIntent.getService()
而不是 PendingIntent
的 PendingIntent.getBroadcast()
引用 Service
,就像您的情况一样。
看看官方文档:http://developer.android.com/reference/android/app/PendingIntent.html#getService(android.content.Context, int, android.content.Intent, int).
这里也不需要实例化服务:
SainarService sainarService = new SainarService(context);
你应该可以做到:
Intent mIntent = new Intent(context , SainarService.class);
PendingIntent pendingIntent = PendingIntent.getService(context, 0, mIntent, PendingIntent.FLAG_UPDATE_CURRENT);
我试图在 BroadcastReceiver
中启动 Service
。 BroadcastReceiver
收到 BOOT_COMPLETED
。我需要定期启动 Service
。我试过下面的代码:
BroadcastReceiver
:
public class SainarReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
App.setContext(context);
ExceptionHelpers.dLog("SainrCoService", SainarReceiver.class.getSimpleName() + " on Receive : " + App.getString(R.string.app_name));
SainarService sainarService = new SainarService(context);
Intent mIntent = new Intent(context , sainarService.getClass());
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, mIntent, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + 1000, 1000, pendingIntent);
}
}
Service
:
public class SainarService 扩展服务 {
public SainarService(){
}
public SainarService(Context iContext){
this.iContext = iContext;
App.setContext(iContext);
ExceptionHelpers.dLog("SainrCoService", SainarService.class.getSimpleName() + " Constructor : " + App.getString(R.string.app_name));
}
private Context iContext;
@Override
public IBinder onBind(Intent intent) {
// ExceptionHelpers.dLog("SainrCoService", SainarService.class.getSimpleName() + " on Bind : " + App.getString(R.string.app_name));
return null;
}
@Override
public void onCreate() {
super.onCreate();
ExceptionHelpers.dLog("SainrCoService", SainarService.class.getSimpleName() + " on Create : " + App.getString(R.string.app_name));
}
@Override
public void onStart(Intent intent, int startId) {
super.onStart(intent, startId);
ExceptionHelpers.dLog("SainrCoService", SainarService.class.getSimpleName() + " on Start : " + App.getString(R.string.app_name));
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
ExceptionHelpers.dLog("SainrCoService", SainarService.class.getSimpleName() + " on Start Command : " + App.getString(R.string.app_name));
return super.onStartCommand(intent, flags, startId);
}
}
清单:
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
<receiver
android:name="com.emaarit.app.sainarco.receivers.SainarReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
</intent-filter>
</receiver>
<service android:enabled="true" android:name="com.emaarit.app.sainarco.receivers.SainarService"/>
在 BOOT_COMPLETED
上 BroadcastReceiver
开始,但 Service
没有!知道这里出了什么问题吗?
使用 PendingIntent.getService()
而不是 PendingIntent
的 PendingIntent.getBroadcast()
引用 Service
,就像您的情况一样。
看看官方文档:http://developer.android.com/reference/android/app/PendingIntent.html#getService(android.content.Context, int, android.content.Intent, int).
这里也不需要实例化服务:
SainarService sainarService = new SainarService(context);
你应该可以做到:
Intent mIntent = new Intent(context , SainarService.class);
PendingIntent pendingIntent = PendingIntent.getService(context, 0, mIntent, PendingIntent.FLAG_UPDATE_CURRENT);