我使用PendingIntent启动AlarmManager,并在几部手机上启动警报没有响应。在某些设备上工作正常,其他设备失败。我在不同的手机上做了一些测试。
Nexus工作正常,三星Galaxy S4 zoom(4.2)工作正常。
三星笔记2(4.3)工作正常。
OPPO(4.4.4)报警器死亡。
我还实现了广播接收器,它们可以在所有设备上正常工作。
Log.v(TAG, "START ALARM");
Intent intentAlarm = new Intent(context, AlarmReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context.getApplicationContext(), 0, intentAlarm, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
alarmManager.setInexactRepeating(AlarmManager.ELAPSED_REALTIME, 1000, 5000, pendingIntent);
答案 0 :(得分:7)
检查应用是否处于停止状态。当应用处于停止状态时,它不会收到任何警报或事件。
另外,我想这可能是OEM /制造商特定的固件/操作系统问题。要检查报警是否实际使用 adb shell dumpsys alarm 并检查是否您的应用程序警报已实际安排好。
要检查它是否处于停止状态,请使用以下命令:
adb shell dumpsys package" com.package.name" 并检查 的 "停止=真" 强>
要了解有关停止状态的更多信息,请参阅:
Launch controls on stopped applications
Starting from Android 3.1, the system's package manager keeps track of applications that are in a stopped state and provides a means of controlling their launch from background processes and other applications.
Note that an application's stopped state is not the same as an Activity's stopped state. The system manages those two stopped states separately.
The platform defines two new intent flags that let a sender specify whether the Intent should be allowed to activate components in stopped application.
FLAG_INCLUDE_STOPPED_PACKAGES — Include intent filters of stopped applications in the list of potential targets to resolve against.
FLAG_EXCLUDE_STOPPED_PACKAGES — Exclude intent filters of stopped applications from the list of potential targets.
When neither or both of these flags is defined in an intent, the default behavior is to include filters of stopped applications in the list of potential targets.
Note that the system adds FLAG_EXCLUDE_STOPPED_PACKAGES to all broadcast intents. It does this to prevent broadcasts from background services from inadvertently or unnecessarily launching components of stoppped applications. A background service or application can override this behavior by adding the FLAG_INCLUDE_STOPPED_PACKAGES flag to broadcast intents that should be allowed to activate stopped applications.
Applications are in a stopped state when they are first installed but are not yet launched and when they are manually stopped by the user (in Manage Applications).
请注意停止状态与应用程序进程未运行不同。
答案 1 :(得分:5)
这里可能会有几个不同的问题:
ELAPSED_REALTIME
)不会唤醒设备以发出警报。相反,如果它在设备休眠时到期,它将在下次设备唤醒时发送。triggerAtMillis
的{{1}}值在设备启动后1秒钟请求第一个警报。如果设备已经启动并运行并且您请求此警报,则第一个警报可能不会触发,并可能导致后续警报无法安排。这只是一个猜测,我没有通过查看4.4.4 AOSP来源验证在API 19(Android 4.4)中更改了警报处理以处理警报计时器的整理(默认情况下都不准确)并且此更改可能会影响第二个项目符号的内容。您可以尝试将1000
值更改为triggerAtMillis
请注意,如果您需要将设备从睡眠状态唤醒,则需要使用(SystemClock.elapsedRealtime() + 1000)
警报变体,并让_WAKEUP
进行唤醒锁定{{1}处理警报时,或BroadcastReceiver
释放。
答案 2 :(得分:3)
这只是猜测,但我认为这个问题与API有关。从KitKat开始,系统会混淆AlarmManager。也许考虑尝试在kitkat以上的abd使用其他系统。
"注意:从API 19(KITKAT)开始,警报传递不准确:操作系统将移动警报以最小化唤醒和电池使用。有新的API支持需要严格交付保证的应用程序;请参阅setWindow(int,long,long,PendingIntent)和setExact(int,long,PendingIntent)。 targetSdkVersion早于API 19的应用程序将继续查看之前的行为,其中所有警报都在请求时准确传递。 "
取自http://developer.android.com/reference/android/app/AlarmManager.html
答案 3 :(得分:0)
尝试以下方法:
1)为你的清单添加一个Wake_lock权限。
<uses-permission android:name="android.permission.WAKE_LOCK">
2)改变
alarmManager.setInexactRepeating(AlarmManager.ELAPSED_REALTIME, 1000, 5000, pendingIntent);
与
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), 1000, 5000, pendingIntent);
答案 4 :(得分:0)
你能告诉我们AlarmReceiver.class代码吗?
也许您需要在onStartCommand方法上使用return START_STICKY;
?
答案 5 :(得分:0)
尝试将AlarmManager置于后台服务中。
答案 6 :(得分:0)
应用程序正常关闭后,您的闹钟将继续存在。如果强制停止,或设备重新启动,或者安装了应用程序更新,或者您的应用程序已卸载,则它们将丢失。您可以为某些情况创建BroadcastReceivers
以重新创建警报。
此外,setInexactRepeating
正是如此:不准确。当警报触发时依赖于实现,无法准确预测。
答案 7 :(得分:0)
Try this it works when activity is not running..
Calendar calendar = Calendar.getInstance();
long timemills = calendar.getTimeInMillis();
Intent myIntent = new Intent(this, TimeChangeReceiver.class);
pendingIntent = PendingIntent.getBroadcast(this, 0, myIntent, 0);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC, timemills, pendingIntent);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, timemills,
10000, pendingIntent);
答案 8 :(得分:0)
我在我的项目中也使用了警报服务,在6或7分钟内完成了预备任务。它在所有手机中运行良好。
我已经发出警报这样的服务:
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.SystemClock;
import android.util.Log;
public class MyAlarmService {
private static PendingIntent resetAlarm;
private static String TAG="CellPoliceChildGPSAlarmService";
private static AlarmManager am;
public static void start(Context context) {
try {
// We want the alarm to go off 30 seconds from now.
long firstTime = SystemClock.elapsedRealtime();
// Create an IntentSender that will launch our service, to be scheduled with the alarm manager.
//resetAlarm = PendingIntent.getService(context, 0, new Intent(context, Get_NonRootDetails.class), 0);
resetAlarm = PendingIntent.getService(context, 0, new Intent(context, CallNonRBackgroundService.class), 0);
// Schedule the alarm!
am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
Log.i(TAG, firstTime+"");
am.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, firstTime, 10*1000*60, resetAlarm);
}
catch (Exception e) {
Log.v("CellInfo", "Exception while start the MyAlarmService at: " + e.getMessage());
}
}
public static void stop(Context context) {
try {
// When interval going to change from web services
am.cancel(resetAlarm);
}
catch (Exception e) {
Log.v("CellInfo", "Exception while start the MyAlarmService at: " + e.getMessage());
}
}
}
我已经打过电话或者这样开始;
MyAlarmService.start(SplashActivity.this);
在Manifest中获得许可:
<uses-permission android:name="android.permission.WAKE_LOCK">
<service
android:name="com.secure.DataCountService"
android:enabled="true" >
<intent-filter>
<action android:name="com.secure.MyService" />
</intent-filter>
</service>
对于通知我还使用了待处理的意图,如;
Intent notificationIntent = new Intent(context, DashBoardActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0);
notification.setLatestEventInfo(context, contentTitle, PushNotificationUtils.notiMsg, pendingIntent);
notification.flags |= notification.FLAG_AUTO_CANCEL;