Android AlarmManager RTC_WAKEUP 未唤醒 CPU

Android AlarmManager RTC_WAKEUP not waking CPU

我正在开发闹钟应用程序,它会在不同时刻多次通知用户。一切正常,除了 当我断开 phone 与 PC 的连接并关闭屏幕时(例如按下电源按钮),警报不会出发。但是当我按下按钮手动唤醒phone时,闹钟突然响了。

这发生在 Android 4.4,API 19 (KITKAT)。在 Android 2.3 phone 每次都能正确唤醒。

请帮助我。如何使其适用于所有 Android 版本?

MainActivity.java:

    Button b = (Button) findViewById(R.id.button);
    b.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            setAlarm(0);
            setAlarm(1);
            setAlarm(4);
            setAlarm(7);
        }});

public void setAlarm(int index) {
    long tim = new GregorianCalendar().getTimeInMillis() + 5 * 1000 * (index+1);
    Intent intent = new Intent(this, MyReceiver.class);
    intent.setData(Uri.parse(Integer.toString( index )));
    PendingIntent pi = PendingIntent.getBroadcast(this, index, intent, PendingIntent.FLAG_ONE_SHOT);
    AlarmManager am = (AlarmManager) this.getSystemService(Context.ALARM_SERVICE);
    if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.KITKAT) {
        am.setExact(AlarmManager.RTC_WAKEUP, tim, pi);
    } else {
        am.set(AlarmManager.RTC_WAKEUP, tim, pi);
    }
}

MyReceiver.java:

public void onReceive(Context context, Intent intent) {
    Intent mIntent = new Intent(context, NotifActivity.class);
    mIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    context.startActivity(mIntent);
}

NotifActivity.java:

boolean done;
public PowerManager.WakeLock mWakeLock;

@Override
protected void onCreate(Bundle savedInstanceState) {
    done = false;
    PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
    mWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP |
            PowerManager.ON_AFTER_RELEASE, "TEST_LOCK");
    mWakeLock.acquire();

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_notif);

    getWindow().addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED);
    getWindow().addFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD);
    getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
    getWindow().addFlags(WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);
    getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
    getWindow().addFlags(WindowManager.LayoutParams.FLAG_ALLOW_LOCK_WHILE_SCREEN_ON);

    final Vibrator vibe = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
    vibe.vibrate(100);

    done = true;
}

@Override
protected void onDestroy() {
    super.onDestroy();
    if (done) {
        try {
            if (mWakeLock != null)
                mWakeLock.release();
        } catch (Exception e) {
            //e.printStackTrace();
        }
    }
}

AndroidManifest.xml:

 <uses-permission android:name="android.permission.VIBRATE" />
    <uses-permission android:name="android.permission.WAKE_LOCK" />
    <uses-permission android:name="com.android.alarm.permission.SET_ALARM" />
    <uses-permission android:name="android.permission.DISABLE_KEYGUARD" />

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <receiver
            android:name=".MyReceiver"
            android:enabled="true"
            android:exported="false"
            android:process=":remote"/>

        <activity android:name=".NotifActivity"></activity>
    </application>

好的,问题解决了。多么愚蠢的错误...我正在使用 SONY XPERIA phone 进行调试。它开启了一些奇怪的省电模式 "STAMINA",这可以防止 phone 被任何非 SONY 的应用程序唤醒。

所以代码是正确的。您唯一可以添加的是 onReceive 方法中的另一个唤醒锁。