Android利用BroadcastReceiver实现短信监听
16lz
2021-01-26
MainActivity如下:
01 | package cc.testsmsbroadcastreceiver; |
02 | import cc.testsmsbroadcastreceiver.SMSBroadcastReceiver.MessageListener; |
03 | import android.os.Bundle; |
04 | import android.widget.TextView; |
05 | import android.app.Activity; |
06 | /** |
07 |
* Demo描述: |
08 |
* 利用BroadcastReceiver实现监听短信 |
09 |
* |
10 |
* 注意权限: |
11 |
* <uses-permission android:name="android.permission.RECEIVE_SMS"/> |
12 |
* |
13 |
* 详细资料: |
14 |
* http://blog.csdn.net/lfdfhl/article/details/8195400 |
15 |
* |
16 |
*/ |
17 | public class MainActivity extends Activity{ |
18 |
private TextView mTextView; |
19 |
private SMSBroadcastReceiver mSMSBroadcastReceiver; |
20 |
@Override |
21 |
protected void onCreate(Bundle savedInstanceState) { |
22 |
super .onCreate(savedInstanceState); |
23 |
setContentView(R.layout.main); |
24 |
init(); |
25 |
} |
26 |
private void init(){ |
27 |
mTextView=(TextView) findViewById(R.id.textView); |
28 |
mSMSBroadcastReceiver= new SMSBroadcastReceiver(); |
29 |
mSMSBroadcastReceiver.setOnReceivedMessageListener( new MessageListener() { |
30 |
@Override |
31 |
public void OnReceived(String message) { |
32 |
mTextView.setText(message); |
33 |
} |
34 |
}); |
35 |
} |
36 |
37 | } |
SMSBroadcastReceiver如下:
01 | package cc.testsmsbroadcastreceiver; |
02 | import java.text.SimpleDateFormat; |
03 | import java.util.Date; |
04 | import android.content.BroadcastReceiver; |
05 | import android.content.Context; |
06 | import android.content.Intent; |
07 | import android.telephony.SmsMessage; |
08 | /** |
09 |
* 配置广播接收者: |
10 |
* <receiver android:name=".SMSBroadcastReceiver"> |
11 |
* <intent-filter android:priority="1000"> |
12 |
* <action android:name="android.provider.Telephony.SMS_RECEIVED"/> |
13 |
* </intent-filter> |
14 |
* </receiver> |
15 |
* |
16 |
* 注意: |
17 |
* <intent-filter android:priority="1000">表示: |
18 |
* 设置此广播接收者的级别为最高 |
19 |
*/ |
20 |
21 | public class SMSBroadcastReceiver extends BroadcastReceiver { |
22 |
private static MessageListener mMessageListener; |
23 |
public SMSBroadcastReceiver() { |
24 |
super (); |
25 |
} |
26 |
27 |
@Override |
28 |
public void onReceive(Context context, Intent intent) { |
29 |
Object [] pdus= (Object[]) intent.getExtras().get( "pdus" ); |
30 |
for (Object pdu:pdus){ |
31 |
SmsMessage smsMessage=SmsMessage.createFromPdu(( byte [])pdu); |
32 |
String sender=smsMessage.getDisplayOriginatingAddress(); |
33 |
String content=smsMessage.getMessageBody(); |
34 |
long date=smsMessage.getTimestampMillis(); |
35 |
Date timeDate= new Date(date); |
36 |
SimpleDateFormat simpleDateFormat= new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss" ); |
37 |
String time=simpleDateFormat.format(timeDate); |
38 |
|
39 |
System.out.println( "短信来自:" +sender); |
40 |
System.out.println( "短信内容:" +content); |
41 |
System.out.println( "短信时间:" +time); |
42 |
|
43 |
mMessageListener.OnReceived(content); |
44 |
|
45 |
//如果短信来自5556,不再往下传递 |
46 |
if ( "5556" .equals(sender)){ |
47 |
System.out.println( " abort " ); |
48 |
abortBroadcast(); |
49 |
} |
50 |
|
51 |
} |
52 |
} |
53 |
|
54 |
// 回调接口 |
55 |
public interface MessageListener { |
56 |
public void OnReceived(String message); |
57 |
} |
58 |
59 |
public void setOnReceivedMessageListener(MessageListener messageListener) { |
60 |
this .mMessageListener=messageListener; |
61 |
} |
62 | } |
main.xml如下:
01 | < RelativeLayout xmlns:android = "http://schemas.android.com/apk/res/android" |
02 |
xmlns:tools = "http://schemas.android.com/tools" |
03 |
android:layout_width = "match_parent" |
04 |
android:layout_height = "match_parent" |
05 |
> |
06 |
07 |
< TextView |
08 |
android:id = "@+id/textView" |
09 |
android:layout_width = "wrap_content" |
10 |
android:layout_height = "wrap_content" |
11 |
android:text = "@string/hello_world" |
12 |
android:layout_centerInParent = "true" /> |
13 |
14 | </ RelativeLayout > |
AndroidManifest.xml如下:
01 | <? xml version = "1.0" encoding = "utf-8" ?> |
02 | < manifest xmlns:android = "http://schemas.android.com/apk/res/android" |
03 |
package = "cc.testsmsbroadcastreceiver" |
04 |
android:versionCode = "1" |
05 |
android:versionName = "1.0" > |
06 |
07 |
< uses-sdk |
08 |
android:minSdkVersion = "8" |
09 |
android:targetSdkVersion = "8" /> |
10 |
|
11 |
< uses-permission android:name = "android.permission.RECEIVE_SMS" /> |
12 |
|
13 |
14 |
< application |
15 |
android:allowBackup = "true" |
16 |
android:icon = "@drawable/ic_launcher" |
17 |
android:label = "@string/app_name" |
18 |
android:theme = "@style/AppTheme" > |
19 |
< activity |
20 |
android:name = "cc.testsmsbroadcastreceiver.MainActivity" |
21 |
android:label = "@string/app_name" > |
22 |
< intent-filter > |
23 |
< action android:name = "android.intent.action.MAIN" /> |
24 |
25 |
< category android:name = "android.intent.category.LAUNCHER" /> |
26 |
</ intent-filter > |
27 |
</ activity > |
28 |
|
29 |
< receiver android:name = ".SMSBroadcastReceiver" > |
30 |
< intent-filter android:priority = "1000" > |
31 |
< action android:name = "android.provider.Telephony.SMS_RECEIVED" /> |
32 |
</ intent-filter > |
33 |
</ receiver > |
34 |
</ application > |
35 |
36 | </ manifest > |
更多相关文章
- 网络实时监测广播类
- 发送短信之分割短信 SMSManager
- Android在Service服务中调用Activity活动的方法,实现UI界面更新操
- 直接用 Chrome 扩展来回复 Android(安卓)手机上的短信
- Android中对闹钟Alarm的事件处理
- Android小玩意儿-- 从头开发一个正经的MusicPlayer(二)
- android装逼技术之暗码小DOME
- Android(安卓)launcher动态Icon的实现方法
- Android面试系列之一