在Android中使用具有定期任务的BroadcastReceiver内部服务

时间:2015-09-02 10:47:36

标签: android service broadcastreceiver periodic-task

我尝试在Service内启动BroadcastReceiverBroadcastReceiver收到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:     公共类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没有!任何想法在这里出了什么问题?

1 个答案:

答案 0 :(得分:1)

使用PendingIntent.getService()代替PendingIntent.getBroadcast()的{​​{1}}引用PendingIntent,就像您的情况一样。

查看官方文档:http://developer.android.com/reference/android/app/PendingIntent.html#getService(android.content.Context,int,android.content.Intent,int)。

此外,无需在此处实例化服务:

Service

你应该能够做到:

SainarService sainarService = new SainarService(context);