使用服务注册特殊广播接收者

Android中的广播:系统在运行过程中,会发生很多事件,系统为了让其他应用知道系统发生了这个事件,会发送一个对应事件的广播,比如:电量改变,收到短信,拨打电话,屏幕解锁,系统开机,只有注册一个广播接收者,就可以接收到系统发送的广播。

屏幕锁屏和解锁、电量改变等广播属于安卓系统中操作特别频繁的广播事件,若在MainActivity中注册,当MainActivity销毁时无法接收广播,所以应该在服务中去注册广播接收者,必须使用代码注册!

首先这是定义的广播接收者:

 1public class MyReceiver extends BroadcastReceiver { 
 2    @Override
 3    public void onReceive(Context context, Intent intent) {
 4        //获取当前事件类型
 5        String action = intent.getAction();
 6        if("android.intent.action.SCREEN_OFF".equals(action)){
 7            //屏幕锁屏
 8            System.out.println("屏幕锁屏");
 9 
10        }else if("android.intent.action.SCREEN_ON".equals(action)){
11            //屏幕解锁
12            System.out.println("屏幕解锁");
13        }
14    }
15}

动态注册广播的Service:

 1import android.app.Service;
 2import android.content.Intent;
 3import android.content.IntentFilter;
 4import android.os.IBinder;
 5 
 6public class ScreenService extends Service {
 7    private MyReceiver myReceiver;
 8    public ScreenService() {
 9    }
10 
11    @Override
12    public IBinder onBind(Intent intent) {
13        return null;
14    }
15 
16    @Override
17    public void onCreate() {
18        //获取MyReceiver实例
19        myReceiver = new MyReceiver();
20 
21        //添加Action
22        IntentFilter filter = new IntentFilter();
23        filter.addAction("android.intent.action.SCREEN_OFF");
24        filter.addAction("android.intent.action.SCREEN_ON");
25        //动态注册广播
26        registerReceiver(myReceiver,filter);
27        super.onCreate();
28    }
29 
30    @Override
31    public void onDestroy() {
32        //当服务销毁的时候取消注册广播
33        unregisterReceiver(myReceiver);
34        super.onDestroy();
35    }
36}

MainActivity在加载的时候就开启服务:

 1package useservice.xpu.nevergiveup.serviceresgitreceiver;
 2 
 3import android.content.Intent;
 4import android.support.v7.app.AppCompatActivity;
 5import android.os.Bundle;
 6 
 7public class MainActivity extends AppCompatActivity {
 8 
 9    @Override
10    protected void onCreate(Bundle savedInstanceState) {
11        super.onCreate(savedInstanceState);
12        setContentView(R.layout.activity_main);
13        startService(new Intent(getApplicationContext(),
14                                ScreenService.class));
15    }
16}

之后别忘记配置一下Service

 1<?xml version="1.0" encoding="utf-8"?>
 2<manifest xmlns:android="http://schemas.android.com/apk/res/android"
 3    package="useservice.xpu.nevergiveup.serviceresgitreceiver">
 4    <application
 5        android:allowBackup="true"
 6        android:icon="@mipmap/ic_launcher"
 7        android:label="@string/app_name"
 8        android:roundIcon="@mipmap/ic_launcher_round"
 9        android:supportsRtl="true"
10        android:theme="@style/AppTheme">
11        <activity android:name=".MainActivity">
12            <intent-filter>
13                <action android:name="android.intent.action.MAIN" />
14 
15                <category android:name="android.intent.category.LAUNCHER" />
16            </intent-filter>
17        </activity>
18        <service android:name=".ScreenService"/>
19    </application> 
20</manifest>

mark