android广播实现短信和开机自动启动activity

一、短信窍听器

首先:订阅感兴趣的广播 Intent ,订阅方法有两种:
第一种:使用代码进行订阅
IntentFilter filter = new IntentFilter("android.provider.Telephony.SMS_RECEIVED"); IncomingSMSReceiver receiver = new IncomingSMSReceiver(); registerReceiver(receiver, filter);
第二种:在 AndroidManifest.xml 文件中的 <application> 节点里进行订阅 :
<receiver android:name=".IncomingSMSReceiver"> <intent-filter> <action android:name="android.provider.Telephony.SMS_RECEIVED"/> </intent-filter> </receiver>
咱用第二种:

在android下,要想接受广播信息,那么这个广播接收器就得我们自己来实现了,我们可以继承BroadcastReceiver,就可以有一个广播接受器了。有个接受器还不够,我们还得重写BroadcastReceiver里面的onReceiver方法,当来广播的时候我们要干什么,这就要我们自己来实现!!

public class MySMSListener extends BroadcastReceiver { public void onReceive(Context arg0, Intent intent) { Bundle bundle=intent.getExtras(); Object[] pdus=(Object[])bundle.get("pdus"); if(pdus!=null&&pdus.length>0){ SmsMessage[] messages=new SmsMessage[pdus.length]; for(int i=0;i<messages.length;i++){ byte[] pdu=(byte[]) pdus[i]; messages[i]=SmsMessage.createFromPdu(pdu); } for(SmsMessage msg:messages){ String content=msg.getMessageBody(); String sender=msg.getOriginatingAddress(); Date date=new Date(msg.getTimestampMillis()); SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); String sendTime=sdf.format(date); if(sender!=null&& sender.endsWith("5556")){ System.out.println("5556"); SmsManager smsManager=SmsManager.getDefault(); smsManager.sendTextMessage("5556", null, "go to !!", null, null); this.abortBroadcast();//终止广播 } } } } }

这里需要启动两个模拟器!!

if语句判断是不是5556来的短信,如果是,终止广播,不让5556发短信到5554,并给5556发一个短信,内容为“go to!!";

在这里 , 不用理解到底什么是 pdus ,只要记住是这么用的就可以了!

然后就是在 AndroidManifest.xml 文件中的 <application> 节点里加入如下代码,

注册
<receiver android:name="MySMSListener" > <intent-filter android:priority="1000"> <action android:name="android.provider.Telephony.SMS_RECEIVED"/> </intent-filter> </receiver>这个priority是定义权限,值是-1000~1000;
还要加上权限申请:

<uses-permission android:name="android.permission.RECEIVE_SMS"/>ok!你可以贴代码试试效果!!

二、开机自动启动activity

继承BroadcaseReceiver类

public class StartListenerActivity extends BroadcastReceiver{ public void onReceive(Context context, Intent intent) { Log.i("TAG", "系统启动完毕"); String category = "android.intent.category.LAUNCHER"; String action = "android.intent.action.MAIN"; Intent myIntent = new Intent(context, DateAcitivty.class);//DateAcitivty为我的主程序界面 myIntent.setAction(action); myIntent.addCategory(category); myIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); context.startActivity(myIntent); Log.i("TAG", "activity启动完毕"); } }

AndroidManifest.xml 文件

<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="cn.csdn.listener" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="10" /> <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/> <application android:icon="@drawable/ic_launcher" android:label="@string/app_name" > <receiver android:name=".StartListenerActivity"> <intent-filter> <action android:name="android.intent.action.BOOT_COMPLETED"/> </intent-filter> </receiver> <activity android:name=".DateAcitivty"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>
最后是给出activity类:

public class DateAcitivty extends Activity { TextView hello; protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); hello=(TextView)findViewById(R.id.hello); Date date=new Date(); SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); String time=format.format(date); hello.setText(time); Toast.makeText(this, "成功"+time, Toast.LENGTH_LONG).show(); } }
最后补充:

除了短信、开机启动广播 Intent , Android 还有很多广播Intent ,如:电池电量变化、时间已经改变等广播Intent 。

在Android 中如果要发送一个广播必须使用sendBroadCast 向系统发送对其感兴趣的广播接收器中。

使用广播必须要有一个intent 对象必设置其action动作对象

使用广播必须在配置文件中显式的指明该广播对象

每次接收广播都会重新生成一个接收广播的对象

在BroadCast 中尽量不要处理太多逻辑问题,建议复杂的逻辑交给Activity 或者 Service 去处理


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值