AlarmManager am = (AlarmManager) getActivity()
.getSystemService(Context.ALARM_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
am.setExact(AlarmManager.RTC_WAKEUP, TimeUtils
.stringToLong(recordTime, TimeUtils.NO_SECOND_FORMAT), sender);
}else {
am.set(AlarmManager.RTC_WAKEUP, TimeUtils
.stringToLong(recordTime, TimeUtils.NO_SECOND_FORMAT), sender);
}
@Override
public void onCreate() {
super.onCreate();
Daemon.run(DaemonService.this,
DaemonService.class, Daemon.INTERVAL_ONE_MINUTE);
startTimeTask();
grayGuard();
}
private void grayGuard() {
if (Build.VERSION.SDK_INT < 18) {
//API < 18 ,此方法能有效隐藏Notification上的图标
startForeground(GRAY_SERVICE_ID, new Notification());
} else {
Intent innerIntent = new Intent(this, DaemonInnerService.class);
startService(innerIntent);
startForeground(GRAY_SERVICE_ID, new Notification());
}
//发送唤醒广播来促使挂掉的UI进程重新启动起来
AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
Intent alarmIntent = new Intent();
alarmIntent.setAction(WakeReceiver.GRAY_WAKE_ACTION);
PendingIntent operation = PendingIntent.getBroadcast(this,
WAKE_REQUEST_CODE, alarmIntent, PendingIntent.FLAG_UPDATE_CURRENT);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
alarmManager.setWindow(AlarmManager.RTC_WAKEUP,
System.currentTimeMillis(), ALARM_INTERVAL, operation);
}else {
alarmManager.setInexactRepeating(AlarmManager.RTC_WAKEUP,
System.currentTimeMillis(), ALARM_INTERVAL, operation);
}
}
/**
* 给 API >= 18 的平台上用的灰色保活手段
*/
public static class DaemonInnerService extends Service {
@Override
public void onCreate() {
Log.i(LOG_TAG, "InnerService -> onCreate");
super.onCreate();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.i(LOG_TAG, "InnerService -> onStartCommand");
startForeground(GRAY_SERVICE_ID, new Notification());
//stopForeground(true);
stopSelf();
return super.onStartCommand(intent, flags, startId);
}
@Override
public IBinder onBind(Intent intent) {
throw new UnsupportedOperationException("Not yet implemented");
}
@Override
public void onDestroy() {
Log.i(LOG_TAG, "InnerService -> onDestroy");
super.onDestroy();
}
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
mJobScheduler = (JobScheduler) getSystemService(Context.JOB_SCHEDULER_SERVICE);
JobInfo.Builder builder = new JobInfo.Builder(JOB_ID,
new ComponentName(getPackageName(), JobSchedulerService.class.getName()));
builder.setPeriodic(60 * 1000); //每隔60秒运行一次
builder.setRequiresCharging(true);
builder.setPersisted(true); //设置设备重启后,是否重新执行任务
builder.setRequiresDeviceIdle(true);
if (mJobScheduler.schedule(builder.build()) <= 0) {
//If something goes wrong
}
}
@TargetApi(Build.VERSION_CODES.M)
public static boolean isIgnoringBatteryOptimizations(Activity activity){
String packageName = activity.getPackageName();
PowerManager pm = (PowerManager) activity
.getSystemService(Context.POWER_SERVICE);
if (pm.isIgnoringBatteryOptimizations(packageName)) {
return true;
}else {
return false;
}
}
/**
* 针对N以上的Doze模式
*
* @param activity
*/
public static void isIgnoreBatteryOption(Activity activity) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
try {
Intent intent = new Intent();
String packageName = activity.getPackageName();
PowerManager pm = (PowerManager) activity.getSystemService(Context.POWER_SERVICE);
if (!pm.isIgnoringBatteryOptimizations(packageName)) {
// intent.setAction(Settings.ACTION_IGNORE_BATTERY_OPTIMIZATION_SETTINGS);
intent.setAction(Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS);
intent.setData(Uri.parse("package:" + packageName));
activity.startActivityForResult(intent, REQUEST_IGNORE_BATTERY_CODE);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
if (requestCode == BatteryUtils.REQUEST_IGNORE_BATTERY_CODE){
//TODO something
}
}else if (resultCode == RESULT_CANCELED){
if (requestCode == BatteryUtils.REQUEST_IGNORE_BATTERY_CODE){
ToastUtils.show(getActivity(), "请开启忽略电池优化~");
}
}
}
private void initAlarmService() {
startService(new Intent(this, DaemonService.class));//启动闹钟服务
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
//JobScheduler
...
}
//绑定闹钟服务
Intent intent = new Intent(this, DaemonService.class);
intent.setAction("android.intent.action.DaemonService");
bindService(intent, mConnection, Context.BIND_AUTO_CREATE);
}
@Override
protected void onDestroy() {
super.onDestroy();
try {//判断是否有闹钟,没有则关闭闹钟服务
String alarm = localPreferencesHelper.getString(LocalPreferencesHelper.ALARM_CLOCK);
if (daemonService != -1 && mIRemoteService != null) {
// android.os.Process.killProcess(daemonService);
mIRemoteService.resetAlarm();
}
if (!alarm.equals("[]")) {
if (daemonService != -1) {
startService(new Intent(this, DaemonService.class));
}
} else {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
mJobScheduler.cancel(JOB_ID);
}
}
unbindService(mConnection); //解除绑定服务。
} catch (Exception e) {
}
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有