android广播实现短信窃听器和开机自动启动activity
2011-12-26 21:17
501 查看
一、短信窍听器
首先:订阅感兴趣的广播 Intent ,订阅方法有两种:
第一种:使用代码进行订阅
IntentFilter filter = new IntentFilter("android.provider.Telephony.SMS_RECEIVED");
IncomingSMSReceiver receiver = new IncomingSMSReceiver();
registerReceiver(receiver, filter);
第二种:在 AndroidManifest.xml 文件中的 <application> 节点里进行订阅 :
咱用第二种:
在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 文件
最后是给出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 去处理
首先:订阅感兴趣的广播 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 去处理
相关文章推荐
- 无废话Android之activity的生命周期、activity的启动模式、activity横竖屏切换的生命周期、开启新的activity获取他的返回值、利用广播实现ip拨号、短信接收广播、短信监听器(6)
- Android开发之使用BroadcastReceiver实现开机自动启动(源代码分享)
- Android:BroadcastReceiver:采用广播接收者实现短信窃听器
- Android实现开机自动启动APP
- android131 360 05 手势触摸滑动,sim卡,开机启动的广播,手机联系人,SharedPreferences,拦截短信
- android如何实现开机自动启动Service或app
- android实现开机自动启动Service或app
- Android中实现开机自动启动服务(service)实例
- android如何实现开机自动启动Service或app
- Android实现开机自动启动Service或app的方法
- Android开发:系统自动实现开机启动
- android如何实现开机自动启动Service或app
- Android开发:系统自动实现开机启动
- Android开发:系统自动实现开机启动
- android开机自动启动service或者activity
- android中如何实现开机启动某些服务,activity等
- android开机启动接收广播和接收短信广播DEMO
- Android开发之使用BroadcastReceiver实现开机自动启动(源代码分享)
- Android 第十九课 大喇叭--广播机制----动态注册监听网络变化与静态注册实现开机启动
- Android开发:系统自动实现开机启动