0

Servicea の中で aを開始しようとしましたBroadcastReceiverBroadcastReceiverを受け取りますBOOT_COMPLETEDService定期的に開始する必要があります。私は以下のコードを試しました:

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);
    }
}

: Servicepublic class SainarService extends Service {

    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_COMPLETEDBroadcastReceiverますが、開始されServiceません。ここで何がうまくいかないのですか?

4

1 に答える 1