1 <receiver
2 android:name=".receiver.MsgReceiver"
3 android:enabled="true"
4 android:exported="true">
5 intent-filter6 action ="android.provider.Telephony.SMS_RECEIVED"></action7 <!--接收收到信息的广播-->
8 </9 receiver>
1 import android.content.BroadcastReceiver;
2 android.content.Context;
3 android.content.Intent;
4 android.widget.Toast;
5
6 public class MsgReceiver extends BroadcastReceiver {
7
8 @Override
9 void onReceive(Context context,Intent intent) {
10 // TODO: This method is called when the BroadcastReceiver is receiving
11 an Intent broadcast.
12 Toast.makeText(context,"收到信息",Toast.LENGTH_SHORT).show();
13 添加Toast便于观察
14 }
15 }
1 uses-permission ="android.permission.READ_SMS"uses-permission2 ="android.permission.RECEIVE_SMS">
android.Manifest;
android.content.pm.PackageManager;
android.support.v4.app.ActivityCompat;
android.support.v7.app.AppCompatActivity;
android.os.Bundle;
class MainActivity AppCompatActivity {
@Override
protected onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (ActivityCompat.checkSelfPermission(MainActivity.this
ActivityCompat.checkSelfPermission(MainActivity.PackageManager.PERMISSION_GRANTED){
ActivityCompat.requestPermissions(MainActivity.new String[]{Manifest.permission.READ_SMS,Manifest.permission.RECEIVE_SMS},1);
}动态申请权限
}
}