android 广播BroadcastReceiver,充电、断电、下载、开屏、锁屏、解锁的监听及service初始化广播的监听事件,获取充电状态,cpu温度,service的适配

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;
    }
    }
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

jian11058

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值