SMSReceiver.:
public class SMSReceiver extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
Log.e("TAG", "来短信了");
Object[] object = (Object[]) intent.getExtras().get("pdus");
for(Object obj : object){
//创建短信的消息对象
SmsMessage message = SmsMessage.createFromPdu((byte[])obj);
//获取短信的发送者
String from = message.getOriginatingAddress();
//获取消息的内容
String msgBody = message.getMessageBody();
Log.e("TAG", "from:"+from+",msgBody:"+msgBody);
}
}
}
AppInstallReceiver:
public class AppInstallReceiver extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
//获取应用的报名
Uri data = intent.getData();
if("android.intent.action.PACKAGE_INSTALL".equals(action)){
Log.e("TAG", "install"+data);
}else if("android.intent.action.PACKAGE_REMOVED".equals(action)){
Log.e("TAG", "removed"+data);
}
}
}
BooteReceiver:
public class BooteReceiver extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
Log.e("TAG", "机器开启了");
Intent i = new Intent(context,MainActivity.class);
/**
* 现在是在广播接受这种创建一个Activity
* 当前应用没有任何Activity在运行,所以不存在一个任务栈
* 需要通过制定一个Flag,在创建Activity的同时创建任务栈
*/
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);
}
}
清单文件声明:
<!-- 处理外拨电话的权限 -->
<uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS"/>
<!-- 接受短信的权限 -->
<uses-permission android:name="android.permission.RECEIVE_SMS"/>
<!-- 接受开机广播的权限 -->
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
<receiver android:name=".SMSReceiver">
<intent-filter >
<action
android:name="android.provider.Telephony.SMS_RECEIVED"/>
</intent-filter>
</receiver>
<receiver android:name=".AppinstallReceiver">
<intent-filter >
<action
android:name="android.intent.action.PACKAGE_INSTALL"/>
<action
android:name="android.intent.action.PACKAGE_REMOVED"/>
<data android:scheme="pacrage"/>
</intent-filter>
</receiver>
<receiver android:name=".BootReceiver">
<intent-filter >
<action
android:name="android.intent.action.BOOT_COMPLETED"/>
</intent-filter>
</receiver>