一、知识准备

  ①常驻广播接受者:使用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】

 import android.Manifest;
import android.content.BroadcastReceiver;
import android.content.IntentFilter;
import android.content.pm.PackageManager;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log; import com.example.administrator.myapplication.receiver.NReceiver; public class MainActivity extends AppCompatActivity { BroadcastReceiver receiver = null; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getpermission(); registerBroadcast(); } private void getpermission() {
if (ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.READ_SMS)
!= PackageManager.PERMISSION_GRANTED||ContextCompat.checkSelfPermission(MainActivity.this,Manifest.permission.RECEIVE_SMS)
!=PackageManager.PERMISSION_GRANTED){
ActivityCompat.requestPermissions(MainActivity.this,new String[]{
Manifest.permission.READ_SMS,Manifest.permission.RECEIVE_SMS
},1);
}
} private void registerBroadcast() {
receiver = new NReceiver();
IntentFilter filter = new IntentFilter("android.provider.Telephony.SMS_RECEIVED");
registerReceiver(receiver,filter);
Log.i("TAG", "registerBroadcast: ---------register");
} @Override
protected void onDestroy() {
super.onDestroy();
unregisterReceiver(receiver);
}
}

【提示】由于是要接收短信的广播,所以需要获取权限

【AndroidManifest】

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

【提示】非常驻型广播无需在AndroidManifest中进行注册广播。

【效果】

  ①利用模拟器开发工具模拟发送信息

    

    

  ②将APP完全退出,再发送信息,不再收到提示

    

二、项目案例二

【标题】常驻型广播接收收到短信的广播

【提示】再项目一得基础上修改

【AndroidManifest】注册

 <receiver
android:name=".receiver.NReceiver"
android:enabled="true"
android:exported="true">
<intent-filter >
<action android:name="android.provider.Telephony.SMS_RECEIVED"></action>
</intent-filter>
</receiver>

【MainActivity】

【提示】由于常驻型广播已经在AndroidManifest注册,则无须利用registerReceiver(receiver,intentfilter)进行注册和销毁。Activity中只需进行动态权限的申请

 public class MainActivity extends AppCompatActivity {

     BroadcastReceiver receiver = null;

     @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getpermission(); } private void getpermission() {
if (ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.READ_SMS)
!= PackageManager.PERMISSION_GRANTED || ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.RECEIVE_SMS)
!= PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(MainActivity.this, new String[]{
Manifest.permission.READ_SMS, Manifest.permission.RECEIVE_SMS
}, 1);
}
} }

【效果】