我正在研究闹钟应用程序,它在不同时刻多次为用户提供信息。一切都很完美,除了当我从电脑上断开手机并关闭屏幕(例如按电源按钮),警报不会关闭。但是当我按下按钮手动唤醒电话时,警报突然响起。
这发生在Android 4.4,API 19(KITKAT)上。在Android 2.3手机上每次都会正确唤醒。
请帮帮我。如何使其适用于所有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>
答案 0 :(得分:0)
所以代码是正确的。您可以添加的唯一内容是onReceive方法中的另一个唤醒锁。