应用程序Pupose :简单的闹钟。
应用程序在Android 4.2.1(Jelly Beans)上正常运行。
应用程序完全搞砸了Android 5.1.1(Lollipop)
问题详细信息:当闹钟时间结束时,将调用“服务”。但是当我关闭闹钟时,它会在2-3秒后再次调用,然后立即延迟一段时间。无论我多少次关闭闹钟..活动关闭并一次又一次地重新出现(必须从最近的应用程序中刷出应用程序)。
另一个问题是,服务理念是确保......无论用户是否关闭应用程序,警报都会响铃......这在Android 4.2.1上是正常的,但是如果我关闭应用程序那么报警也是如此(服务)。
另一个问题:如果设置闹钟并关闭应用广播接收器,则不会被呼叫。当时没有采取任何行动。
尚未检查其他版本......
我正在使用AlarmManager使用以下代码设置闹钟定时器。函数从活动中调用。
public static void setAlarm(Context context, int pendingIntentCode, long timeToAlarm, int alarmMode, String alarmTitle) {
AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(context, AlarmReceiver.class);
Log.d(TAG, "Pending Intent Code" + pendingIntentCode);
intent.putExtra("pendingIntentCode", pendingIntentCode);
intent.putExtra("alarmMode", alarmMode);
intent.putExtra("alarmTitle", alarmTitle);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, pendingIntentCode, intent, PendingIntent.FLAG_UPDATE_CURRENT);
if (SDK_INT < Build.VERSION_CODES.KITKAT) {
alarmManager.set(AlarmManager.RTC_WAKEUP, timeToAlarm, pendingIntent);
} else if (Build.VERSION_CODES.KITKAT <= SDK_INT && SDK_INT < Build.VERSION_CODES.M) {
alarmManager.setExact(AlarmManager.RTC_WAKEUP, timeToAlarm, pendingIntent);
} else if (SDK_INT >= Build.VERSION_CODES.M) {
alarmManager.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, timeToAlarm, pendingIntent);
}
}
BroadCast接收器代码。
@Override
public void onReceive(Context context, Intent intent) {
int pendingIntentCode = intent.getIntExtra("pendingIntentCode", 0);
int mode = intent.getIntExtra("alarmMode", 0);
String alarmTitle = intent.getStringExtra("alarmTitle");
Intent startServiceIntent = new Intent(context, AlarmByService.class);
startServiceIntent.putExtra("alarmTitle", alarmTitle);
startServiceIntent.putExtra("alarmMode", mode);
startServiceIntent.putExtra("pendingIntentCode", pendingIntentCode);
context.startService(startServiceIntent);
Log.d(TAG, "on Receive");
}
在服务类中,剩余的功能是相同的...除了调用MediaPlayer.stop的ondestroy。
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
super.onStartCommand(intent, flags, startId);
sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
editor = sharedPreferences.edit();
int annoyingMethod = sharedPreferences.getInt("annoyingMethod", 0);
String calmModeTone = sharedPreferences.getString("alarm_tone_calm_mode", "default");
String annoyingModeTone = sharedPreferences.getString("alarm_tone_annoying_mode", "default");
vibration = sharedPreferences.getInt("vibration", 0);
int mode = intent.getIntExtra("alarmMode", 0);
int pendingIntentCode = intent.getIntExtra("pendingIntentCode", 0);
String alarmTitle = intent.getStringExtra("alarmTitle");
Intent activityToCall = null;
if (mode == 0) {
activityToCall = new Intent(AlarmByService.this, CalmModeAlarmDisplay.class);
activityToCall.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
activityToCall.putExtra("pendingIntentCode", pendingIntentCode);
activityToCall.putExtra("alarmTitle", alarmTitle);
MediaPlayerForAlarm.playAlarm(calmModeTone, this, vibration);
} else {
MediaPlayerForAlarm.playAlarm(annoyingModeTone, this, 1);
switch (annoyingMethod) {
case 0:
activityToCall = new Intent(AlarmByService.this, DrawPattern.class);
activityToCall.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
activityToCall.putExtra("pendingIntentCode", pendingIntentCode);
activityToCall.putExtra("alarmTitle", alarmTitle);
break;
case 1:
activityToCall = new Intent(AlarmByService.this, SolveMathProblem.class);
activityToCall.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
activityToCall.putExtra("pendingIntentCode", pendingIntentCode);
activityToCall.putExtra("alarmTitle", alarmTitle);
break;
case 2:
activityToCall = new Intent(AlarmByService.this, ShakeDevice.class);
activityToCall.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
activityToCall.putExtra("pendingIntentCode", pendingIntentCode);
activityToCall.putExtra("alarmTitle", alarmTitle);
break;
}
}
if (activityToCall != null) {
Log.d(TAG, "Calling Activity Intent is not Null");
startActivity(activityToCall);
}
if (alarmTitle == null || pendingIntentCode == 0) {
Log.d(TAG, "Stopping Service Unintentional Call");
stopSelf();
}
Log.d(TAG, "Title " + alarmTitle);
Log.d(TAG, "Vibration: " + vibration);
PendingIntent pendingIntent = PendingIntent.getActivity(AlarmByService.this, 0, activityToCall, 0);
Notification alarmNotification = new NotificationCompat.Builder(AlarmByService.this)
.setSmallIcon(R.drawable.app_icon)
.setContentTitle(alarmTitle)
.setContentText("Tap here to see Alarm Detail.")
.setContentIntent(pendingIntent).build();
startForeground(1337, alarmNotification);
return START_NOT_STICKY;
}
过去两天我用Google搜索了这么多时间......并且找不到任何东西......可能是我不擅长谷歌搜索......任何帮助都是WellCome ......
如果提供的信息不足,我可以提供更多......
先谢谢。