him*_*eam 24 android alarmmanager android-doze-and-standby
我认为很多时候会问这个问题,stackoverflow但仍然有很多人在努力解决这个问题.
在我的Android应用程序中,我必须每半小时唤醒设备以获取当前位置并将其发送到服务器.为此,我使用AlarmManager了setExactAndAllowWhileIdle()方法和WakefulBroadcastReceiver.它几乎适用于所有标准/流行设备,如三星,LG(Nexus),索尼,松下,联想,摩托罗拉,微型等等....但其他一些设备主要是中国设备不支持或不允许设备唤醒从打瞌睡模式开始setExactAndAllowWhileIdle().我已在leeco letV (Android OS 6.1)设备中对其进行了测试,不允许报警管理器在特定时间间隔内唤醒.
我在下面提到的代码部分:
UserTrackingReceiverIntentService.java
public class UserTrackingReceiverIntentService extends IntentService {
public static final String TAG = "UserTrackingReceiverIntentService";
Context context;
public UserTrackingReceiverIntentService() {
super("UserTrackingReceiverIntentService");
}
@TargetApi(Build.VERSION_CODES.M)
@Override
protected void onHandleIntent(Intent intent) {
this.context = this;
if (!Util.isMyServiceRunning(LocationService.class, context)) {
context.startService(new Intent(context, LocationService.class));
}
Calendar calendar = Calendar.getInstance();
//********************************** SETTING NEXT ALARM *********************************************
Intent intentWakeFullBroacastReceiver = new Intent(context, SimpleWakefulReceiver.class);
PendingIntent sender = PendingIntent.getBroadcast(context, 1001, intentWakeFullBroacastReceiver, 0);
AlarmManager alarmManager = (AlarmManager) context.getSystemService(context.ALARM_SERVICE);
if (calendar.get(Calendar.MINUTE) >= 0 && calendar.get(Calendar.MINUTE) < 30) {
calendar.set(Calendar.HOUR_OF_DAY, calendar.get(Calendar.HOUR_OF_DAY));
calendar.set(Calendar.MINUTE, 30);
calendar.set(Calendar.SECOND, 0);
} else if (calendar.get(Calendar.MINUTE) >= 30) {
if (calendar.get(Calendar.HOUR_OF_DAY) == 23) {
calendar.set(Calendar.HOUR_OF_DAY, 0);
calendar.add(Calendar.DAY_OF_MONTH, 1);
} else {
calendar.set(Calendar.HOUR_OF_DAY, calendar.get(Calendar.HOUR_OF_DAY) + 1);
}
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
} else {
calendar.set(Calendar.HOUR_OF_DAY, calendar.get(Calendar.HOUR_OF_DAY));
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
}
//MARSHMALLOW OR ABOVE
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
alarmManager.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP,
calendar.getTimeInMillis(), sender);
}
//LOLLIPOP 21 OR ABOVE
else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
AlarmManager.AlarmClockInfo alarmClockInfo = new AlarmManager.AlarmClockInfo(calendar.getTimeInMillis(), sender);
alarmManager.setAlarmClock(alarmClockInfo, sender);
}
//KITKAT 19 OR ABOVE
else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
alarmManager.setExact(AlarmManager.RTC_WAKEUP,
calendar.getTimeInMillis(), sender);
}
//FOR BELOW KITKAT ALL DEVICES
else {
alarmManager.set(AlarmManager.RTC_WAKEUP,
calendar.getTimeInMillis(), sender);
}
Util.registerHeartbeatReceiver(context);
SimpleWakefulReceiver.completeWakefulIntent(intent);
}
}
Run Code Online (Sandbox Code Playgroud)
每次上述服务一旦拨打电话,30分钟后设置下一次警报.
public class SimpleWakefulReceiver extends WakefulBroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
// This is the Intent to deliver to our service.
Calendar calendar = Calendar.getInstance();
Intent service = new Intent(context, UserTrackingReceiverIntentService.class);
Date date = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
String DateTime = sdf.format(date);
DateTime = Util.locatToUTC(DateTime);
service.putExtra("date", String.valueOf(DateTime));
String latitude = Util.ReadSharePrefrence(context, "track_lat");
String longitude = Util.ReadSharePrefrence(context, "track_lng");
service.putExtra("lat", latitude);
service.putExtra("lon", longitude);
Log.i("SimpleWakefulReceiver", "Starting service @ " + calendar.get(Calendar.HOUR_OF_DAY) + " : " + calendar.get(Calendar.MINUTE) + " : " + calendar.get(Calendar.SECOND));
// Start the service, keeping the device awake while it is launching.
startWakefulService(context, service);
}
}
Run Code Online (Sandbox Code Playgroud)
因为我已经从leeco letV"设置">"电池">"对齐唤醒">"禁用"等设备手动更改了一些设置.如果启用电池可以忽略来自应用程序的强制唤醒.
我的问题:
在这种设备中发生了什么?为什么他们不允许在特定时间间隔内发出警报?
在像金立的一些设备加上晚上2-3分钟的警报......为什么?
android.net.conn.CONNECTIVITY_CHANGE 广播接收器在网络连接发生变化时进行监听.....如果在某些设备(例如Gionee s plus)中无法正常工作,该怎么办?
对于各种制造商来说,电池优化设置有很多变化....如果它是什么解决方案,它是否会损害我们的应用程序的后台服务.
您好,我在 Xiomi Phone.leeco 中测试时遇到了同样的问题,我不太清楚。小米手机有自己的操作系统,即MIUI。当我们清除RAM时,它会清除除操作系统中注册的一些应用程序之外的所有应用程序。像 Whatsapp 和 facebook 等一些其他著名的应用程序。
我尝试过服务、警报管理器和调度程序,但没有取得任何成功。
他们提供了一些手动特定的方式来运行服务。我检查过 Leeco 它使用 Android 操作系统。
MIUI 7.0 的解决方案 => 安全 => 自动启动 => 选择要在后台运行的应用程序 => 重新启动 重新 启动后,您的设备应该能够像其他 Android 设备一样在后台运行应用程序服务。
根据我的说法,您可以尝试使用服务,然后检查它是否有效。
谢谢希望你能从中得到一点帮助。
| 归档时间: |
|
| 查看次数: |
4683 次 |
| 最近记录: |