Android WakefulBroadcastReceiver在首次运行时不起作用

时间:2018-08-02 16:08:54

标签: android wakelock android-wake-lock

我有问题。我已经创建了一个用于使用WakefulBroadcastReceiver的测试应用程序。首次安装后,唤醒锁不起作用。如果我关闭应用程序并重新打开唤醒锁,则工作正常。 我不明白问题出在哪里。

清单中我有:

<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 个答案:

没有答案