Android8.0以上监听开关机,息亮屏,解锁等系统广播轻松实现

11 篇文章 0 订阅

       随着Android版本越来越高,对用户得体验效果极好,但是苦逼了我们这些开发人员,但是办法总比困难多!!!
闲话不多说,直接进入正题:

Android系统启动完成后会发出启动完成广播(android.intent.action.BOOT_COMPLETED),所有注册了接收启动完成广播的接收器(BroadcastReceiver)都会收到此广播。相对应也会收到关机完成广播(android.intent.action.ACTION_SHUTDOWN);

 不同的是开机广播需要在AndroidManifest.xml文件里添加授予应用程序访问系统开机事件的权限

    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />

权限到位,接下就是自定义咱们自己所需要的广播接收器,用于接收广播内容;


/**
 * @创建时间 2019/4/19.
 * @作者 Manuel
 * @描述 接收器
 */

public class BootCompletedReceiver extends BroadcastReceiver{
    @Override
    public void onReceive(Context context, Intent intent) {
        
        switch (intent.getAction()){
            case Intent.ACTION_BOOT_COMPLETED:
                Log.e("TAG","手机开机了");
                 break;
            case  Intent.ACTION_SHUTDOWN:
                Log.e("TAG","手机关机了");
                 break;
            case ACTION_SCREEN_ON:
                Log.e("TAG","亮屏");
                break;
            case ACTION_SCREEN_OFF:
                Log.e("TAG","息屏");
                break;
            case ACTION_USER_PRESENT:
                Log.e("TAG","手机解锁");
                break;
        }
    }
}

接收器搞定当然就是咱们的注册广播咯,那大家都知道静态和动态注册, 下面分别以两种模式进行实现:

  这是静态申请的权限:

      <receiver
            android:name=".BootCompletedReceiver"
            android:enabled="true"
            android:exported="true"
            android:priority="2147483647">
            <intent-filter>
                <!-- 开机广播 -->
                <action android:name="android.intent.action.BOOT_COMPLETED" />
            </intent-filter>

            <intent-filter>
                <!-- 关机广播 -->
                <action android:name="android.intent.action.ACTION_SHUTDOWN" />
            </intent-filter>
            <!-- 解锁广播 -->
            <intent-filter>
                <action android:name="android.intent.action.USER_PRESENT" />
            </intent-filter>
        </receiver>

下面是动态申请

package com.huibo.myphonerestart;

import android.content.Intent;
import android.content.IntentFilter;
import android.net.ConnectivityManager;
import android.net.wifi.WifiManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
/**
 * @创建时间 2019/4/19.
 * @作者 Manuel
 * @描述 活动
 */
public class MainActivity extends AppCompatActivity {

    private   BootCompletedReceiver bootCompletedReceiver;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        bootCompletedReceiver = new BootCompletedReceiver();
        IntentFilter intentFilter = new IntentFilter();
        //亮屏
        intentFilter.addAction(Intent.ACTION_SCREEN_ON);
        //息屏
        intentFilter.addAction(Intent.ACTION_SCREEN_OFF);
        //解锁
        intentFilter.addAction(Intent.ACTION_USER_PRESENT);
        registerReceiver(bootCompletedReceiver,intentFilter);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (bootCompletedReceiver!=null){
            unregisterReceiver(bootCompletedReceiver);
        }
    }
}

  注意:动态注册完之后,在不用的时候取消注册,所有在onDestroy 我们进行取消注册,这样就不会造成内存泄露问题。

如发现没有接收到开关机广播监听内容,这就说明手机系统进行了一些拦截,请给自己的项目加上自启动,完美搞定。

     祝大家在Android的道路上越走越远!!!

 

 

 

 

  • 6
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 7
    评论
Android 8.0 及以上版本,为了增强应用程序的安全性,Android 引入了后台限制,禁止未在前台运行的应用程序启动服务。如果您想在后台启动服务,需要使用 `startForegroundService()` 方法。这个方法会启动一个前台服务,然后你可以在服务启动后在通知栏显示一个通知,以此来告知用户服务正在运行。 以下是一个使用 `startForegroundService()` 的示例代码: ``` if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { // 创建一个 NotificationChannel NotificationChannel channel = new NotificationChannel("channel_id", "channel_name", NotificationManager.IMPORTANCE_DEFAULT); // 向系统注册 NotificationChannel NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); notificationManager.createNotificationChannel(channel); } // 创建一个 Intent,启动你的服务 Intent serviceIntent = new Intent(this, YourService.class); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { // 在 Android 8.0 及以上版本上,需要调用 startForegroundService() 方法启动服务。 startForegroundService(serviceIntent); } else { // 在 Android 8.0 以下版本上,可以直接调用 startService() 方法启动服务。 startService(serviceIntent); } ``` 注意:如果你使用的是 `startForeground()` 方法,会在 Android 8.0 及以上版本上抛出 `IllegalStateException` 异常,因为 Android 8.0 及以上版本禁止在后台启动服务。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值