Android 常驻广播和非常驻广播
一、知识准备
①常驻广播接受者:使用AndroidManifest.xml注册,接受者不随Activity的销毁而销毁,也就是拥有独立的生命周期。
②非常驻广播接受者:使用registerReceiver(receiver,intentfilter)对广播接收者进行注册。同事需要使用unregisterReceiver(receiver)注销这个广播接收者。注册时表示创建广播接收者,注销时表示销毁这个广播接收者。生命周期由开发者决定。通常在activity的onCreate方法中创建广播接收者,在onDestroy中销毁广播接收者。生命周期可控。
③建议了解http://www.cnblogs.com/xqz0618/p/msg_broadcast.html
二、项目案例一
【标题】利用非常驻广播接受收到短信的广播
【步骤】
①创建一个广播NReceiver
②在Activity中进行注册
③在Activity销毁时注销
【项目结构】
【NReceiver】
【MainActivity】
1 import android.Manifest;
2 import android.content.BroadcastReceiver;
3 import android.content.IntentFilter;
4 import android.content.pm.PackageManager;
5 import android.support.v4.app.ActivityCompat;
6 import android.support.v4.content.ContextCompat;
7 import android.support.v7.app.AppCompatActivity;
8 import android.os.Bundle;
9 import android.util.Log;
10
11 import com.example.administrator.myapplication.receiver.NReceiver;
12
13 public class MainActivity extends AppCompatActivity {
14
15 BroadcastReceiver receiver = null;
16
17 @Override
18 protected void onCreate(Bundle savedInstanceState) {
19 super.onCreate(savedInstanceState);
20 setContentView(R.layout.activity_main);
21 getpermission();
22
23 registerBroadcast();
24
25 }
26
27 private void getpermission() {
28 if (ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.READ_SMS)
29 != PackageManager.PERMISSION_GRANTED||ContextCompat.checkSelfPermission(MainActivity.this,Manifest.permission.RECEIVE_SMS)
30 !=PackageManager.PERMISSION_GRANTED){
31 ActivityCompat.requestPermissions(MainActivity.this,new String[]{
32 Manifest.permission.READ_SMS,Manifest.permission.RECEIVE_SMS
33 },1);
34 }
35 }
36
37 private void registerBroadcast() {
38 receiver = new NReceiver();
39 IntentFilter filter = new IntentFilter("android.provider.Telephony.SMS_RECEIVED");
40 registerReceiver(receiver,filter);
41 Log.i("TAG", "registerBroadcast: ---------register");
42 }
43
44 @Override
45 protected void onDestroy() {
46 super.onDestroy();
47 unregisterReceiver(receiver);
48 }
49 }
【提示】由于是要接收短信的广播,所以需要获取权限
【AndroidManifest】
1 <uses-permission android:name="android.permission.READ_SMS"></uses-permission>
2 <uses-permission android:name="android.permission.RECEIVE_SMS"></uses-permission>
【提示】非常驻型广播无需在AndroidManifest中进行注册广播。
【效果】
①利用模拟器开发工具模拟发送信息
②将APP完全退出,再发送信息,不再收到提示
二、项目案例二
【标题】常驻型广播接收收到短信的广播
【提示】再项目一得基础上修改
【AndroidManifest】注册
1 <receiver
2 android:name=".receiver.NReceiver"
3 android:enabled="true"
4 android:exported="true">
5 <intent-filter >
6 <action android:name="android.provider.Telephony.SMS_RECEIVED"></action>
7 </intent-filter>
8 </receiver>
【MainActivity】
【提示】由于常驻型广播已经在AndroidManifest注册,则无须利用registerReceiver(receiver,intentfilter)进行注册和销毁。Activity中只需进行动态权限的申请
1 public class MainActivity extends AppCompatActivity {
2
3 BroadcastReceiver receiver = null;
4
5 @Override
6 protected void onCreate(Bundle savedInstanceState) {
7 super.onCreate(savedInstanceState);
8 setContentView(R.layout.activity_main);
9 getpermission();
10
11 }
12
13 private void getpermission() {
14 if (ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.READ_SMS)
15 != PackageManager.PERMISSION_GRANTED || ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.RECEIVE_SMS)
16 != PackageManager.PERMISSION_GRANTED) {
17 ActivityCompat.requestPermissions(MainActivity.this, new String[]{
18 Manifest.permission.READ_SMS, Manifest.permission.RECEIVE_SMS
19 }, 1);
20 }
21 }
22
23 }
【效果】