1. 开机
<receiver android:name="com.gaoyuan.mysafe.receiver.BootCompleteReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
</intent-filter>
</receiver>
如果想在receiver中启动一个activity,
要给intent加一个flag
public class StartupReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Intent startupintent = new Intent(context,StrongTracks.class);
startupintent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(startupintent);
}
}
权限: <uses-permission
android:name="android.permission.RECEIVE_BOOT_COMPLETED"
/>
2. 接收短信
<receiver android:name="com.gaoyuan.mysafe.receiver.SmsReceiver">
<intent-filter android:priority="1000">
<action android:name="android.provider.Telephony.SMS_RECEIVED"/>
</intent-filter>
</receiver>
这个action在4.1以上的adt里是提示不出来的.
在onReceive方法中获取短信, 经典的代码, 必须记住
Object[] objs = (Object[]) intent.getExtras().get("pdus");
for (Object obj : objs) {
SmsMessage smsMessage = SmsMessage.createFromPdu((byte[]) obj);
// 得到发送者号码, 应该先判断一下是否是安全号码发过来的, 这里就不判断了
String sender = smsMessage.getOriginatingAddress();
// 获取短信内容
String body = smsMessage.getMessageBody();
// 处理短信...
// 如果有必要的话, 中断广播
abortBroadcast();
}
权限:
<uses-permission
android:name="android.permission.RECEIVE_SMS"/>