Android WakefulBroadcastReceiver не работает при первом запуске

У меня проблема. Я создал тестовое приложение для использования WakefulBroadcastReceiver. В первый раз после установки не работает замок. Если я закрою приложение и снова открою, Wake Lock работает хорошо. Я не понимаю, где проблема.

в манифесте у меня есть:

<uses-permission android:name="android.permission.WAKE_LOCK" />

а также

<receiver android:name=".servizio.MyWakefulReceiver"></receiver>

у меня есть один файл для WakefulBroadcastReceiver:

import android.content.Context;
import android.content.Intent;
import android.support.v4.content.WakefulBroadcastReceiver;

public class MyWakefulReceiver extends WakefulBroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {

    // Start the service, keeping the device awake while the service is
    // launching. This is the Intent to deliver to the service.
    Intent service = new Intent(context, MyIntentService.class);
    startWakefulService(context, service);
}
}

и один файл для обслуживания:

import android.app.IntentService;
import android.app.NotificationManager;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.NotificationCompat;

public class MyIntentService extends IntentService {
public static final int NOTIFICATION_ID = 1;
private NotificationManager mNotificationManager;
NotificationCompat.Builder builder;
public MyIntentService() {
    super("MyIntentService");
}
@Override
protected void onHandleIntent(Intent intent) {
    Bundle extras = intent.getExtras();
    // Do the work that requires your app to keep the CPU running.
    // ...
    // Release the wake lock provided by the WakefulBroadcastReceiver.
    MyWakefulReceiver.completeWakefulIntent(intent);
}
}

0 ответов

Другие вопросы по тегам