赞
踩
8.0适配: service中: public static final String CHANNEL_ID_STRING = "service_01"; //适配8.0service NotificationManager notificationManager = (NotificationManager) getBaseContext().getSystemService(Context.NOTIFICATION_SERVICE); NotificationChannel mChannel = null; if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) { mChannel = new NotificationChannel(CHANNEL_ID_STRING, getString(R.string.app_name), NotificationManager.IMPORTANCE_LOW); notificationManager.createNotificationChannel(mChannel); Notification notification = new Notification.Builder(getApplicationContext(), CHANNEL_ID_STRING).build(); startForeground(1, notification); } 跳转启动中: Intent intent=new Intent(PageSettingActivity.this, RestService.class) .putExtra("type", "start_time") .putExtra("isStart", switch_hint_time.isChecked()) .putExtra("time", time_hint) .addFlags(START_FLAG_REDELIVERY); //------------------------android 8.0以上崩溃解决----------------------------------- if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { startForegroundService(intent); } else { startService(intent); } //接收到广播,进行操作 //广播作为该类的成员变量; BroadcastReceiver broadcastReceiver=new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { Log.e(TAG, "onReceive: " ); String type=intent.getStringExtra("type"); String action = intent.getAction(); Log.e(TAG, "onReceive type: "+type ); if (Intent.ACTION_POWER_CONNECTED.equals(action)) { isPowerConnect = true; powerConnectTime = System.currentTimeMillis(); Log.e(TAG, "onReceive 充电: "); // JumpUtils.INSTANCE.toLockScreenBatterJump(); } if (Intent.ACTION_POWER_DISCONNECTED.equals(action)) { isPowerConnect = false; powerConnectTime = 0; Log.e(TAG, "onReceive 断电: "); // JumpUtils.INSTANCE.toLockScreenJump(); } if (isPowerConnect) { if (Intent.ACTION_BATTERY_CHANGED.equals(action)) { if (batterListener != null) { batterListener.change(); } } } if (Intent.ACTION_SCREEN_ON.equals(action)) { // 开屏 Log.e(TAG, "onReceive 开屏: "); } else if (Intent.ACTION_SCREEN_OFF.equals(action)) { // 锁屏 Log.e(TAG, "onReceive 锁屏: "); } else if (Intent.ACTION_USER_PRESENT.equals(action)) { // 解锁 if (isPowerConnect) { // 锁屏充电状态 JumpUtils.INSTANCE.toLockScreenBatterJump(); } else { JumpUtils.INSTANCE.toLockScreenJump(); } Log.e(TAG, "onReceive 解锁: "); } // 下载完成开始安装 if (action.equals(DownloadManager.ACTION_DOWNLOAD_COMPLETE)) { if (isDownLoadWeather) { isDownLoadWeather = false; installApk(context); } } if (type.equals("message")) { handler.sendEmptyMessage(HistoryMessage); } } }; 广播注册:LoginActivity.Action是表明广播的来源 IntentFilter filter=new IntentFilter(LoginActivity.Action);//这里的action与intent绑定; registerReceiver(broadcastReceiver,filter); 在发送广播的地方: public static final String Action="LoginMqtt"; Intent intent=new Intent(Action); intent.putExtra("type","message"); this.sendBroadcast(intent); service中: @Override public int onStartCommand(Intent intent, int flags, int startId) { Log.e(TAG, "onStartCommand: "); checkBatterState(); registerScreenActionReceiver(); return START_STICKY; } /** * 查看电池状态 true表示充电状态,并且初始化充电时长 * */ private void checkBatterState() { Intent batteryBroadcast = getApplicationContext().registerReceiver(null, new IntentFilter(Intent.ACTION_BATTERY_CHANGED)); // 0 means we are discharging, anything else means charging boolean isCharging = batteryBroadcast.getIntExtra(BatteryManager.EXTRA_PLUGGED, -1) != 0; isPowerConnect=isCharging; if (isPowerConnect){ powerConnectTime=System.currentTimeMillis(); }else { powerConnectTime=0; } Log.e(TAG, "checkBatterState isCharging: "+isCharging ); } private void registerScreenActionReceiver() { final IntentFilter filter = new IntentFilter(); // 锁屏监听 filter.addAction(Intent.ACTION_SCREEN_OFF); filter.addAction(Intent.ACTION_SCREEN_ON); filter.addAction(Intent.ACTION_USER_PRESENT); filter.addAction(DownloadManager.ACTION_DOWNLOAD_COMPLETE); // Context.APP_OPS_SERVICE filter.addAction(Context.WINDOW_SERVICE); // 电池监听 filter.addAction(Intent.ACTION_BATTERY_CHANGED); filter.addAction(Intent.ACTION_BATTERY_LOW); filter.addAction(Intent.ACTION_BATTERY_OKAY); filter.addAction(Intent.ACTION_POWER_CONNECTED); filter.addAction(Intent.ACTION_POWER_DISCONNECTED); if (receiver == null) { receiver = new LockScreenBroadcastReceiver(); filter.setPriority(Integer.MAX_VALUE); registerReceiver(receiver, filter); } } /** * cpu热度检测 */ private void autoCupCheck() { cacheThreadPool.execute(new Runnable() { @Override public void run() { DeviceManagerUtils utils = new DeviceManagerUtils(); List<String> list = utils.getThermalInfo(); for (int i = 0; i < list.size(); i++) { String temp = list.get(i); if (list.get(i).contains("quiet_therm")) { // quiet_therm : 0.035°C CpuTempture = getTemp(temp); if (winUtils.isAtHome(getApplicationContext())) { if (Integer.parseInt(CpuTempture) > 60) { // 显示cpu降温对话框1、表示内存优化,2、表示cup降温 showAutoCpuHot = true;//更新cpu是否需要降温 Message msg = new Message(); msg.what = startIntentFunction; Bundle bundle = new Bundle(); bundle.putString("type", "2"); bundle.putString("value", CpuTempture); msg.setData(bundle); handler.sendMessage(msg); return; }else { showAutoCpuHot = false;//更新cpu是否需要降温 } } } } } }); } public class DealsReceiver extends BroadcastReceiver { private static DealsReceiver instance = new DealsReceiver(); private DealsReceiver(){ } public static DealsReceiver getInstance(){ return instance; } }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。