【Android】利用广播Broadcast接收SMS短信
16lz
2021-12-04
BroadcastReceiver
package com.app.test02;import android.content.BroadcastReceiver;import android.content.Context;import android.content.Intent;import android.os.Bundle;import android.telephony.SmsMessage;import android.widget.Toast;/** * 以BroadcastReceiver接收SMS短信 * */public class BroadCastTest2_SMS extends BroadcastReceiver{public static final String ACTION = "android.provider.Telephony.SMS_RECEIVED";@Overridepublic void onReceive(Context context, Intent intent) {// TODO Auto-generated method stubif (ACTION.equals(intent.getAction())) {Intent i = new Intent(context, BroadCastActivity2_SMS.class);i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);SmsMessage[] msgs = getMessageFromIntent(intent);StringBuilder sBuilder = new StringBuilder();if (msgs != null && msgs.length > 0 ) {for (SmsMessage msg : msgs) {sBuilder.append("接收到了短信:\n发件人是:");sBuilder.append(msg.getDisplayOriginatingAddress());sBuilder.append("\n------短信内容-------\n");sBuilder.append(msg.getDisplayMessageBody());i.putExtra("sms_address", msg.getDisplayOriginatingAddress());i.putExtra("sms_body", msg.getDisplayMessageBody());}}Toast.makeText(context, sBuilder.toString(), 1000).show();context.startActivity(i);}}public static SmsMessage[] getMessageFromIntent(Intent intent) {SmsMessage retmeMessage[] = null;Bundle bundle = intent.getExtras();Object pdus[] = (Object[]) bundle.get("pdus");retmeMessage = new SmsMessage[pdus.length];for (int i = 0; i < pdus.length; i++) {byte[] bytedata = (byte[]) pdus[i];retmeMessage[i] = SmsMessage.createFromPdu(bytedata);}return retmeMessage;}}
AndroidManifest注册
<receiver android:name=".BroadCastTest2_SMS"> <intent-filter > <action android:name="android.provider.Telephony.SMS_RECEIVED"/> </intent-filter> </receiver>
权限
<uses-permission android:name="android.permission.RECEIVE_SMS"/>
Activity文件
package com.app.test02;import android.app.Activity;import android.content.Intent;import android.os.Bundle;import android.widget.TextView;public class BroadCastActivity2_SMS extends Activity{private TextView textView;@Overrideprotected void onCreate(Bundle savedInstanceState) {// TODO Auto-generated method stubsuper.onCreate(savedInstanceState);setContentView(R.layout.activity_bc2_sms);textView = (TextView) findViewById(R.id.textView1);Intent intent = getIntent();if (intent != null) {String address = intent.getStringExtra("sms_address");if (address != null) {textView.append("\n\n发件人:\n" + address);String bodyString = intent.getStringExtra("sms_body");if (bodyString != null) {textView.append("\n短信内容:\n" + bodyString);}}}}}
Layout布局文件
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:background="#fff" android:padding="10dp"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" android:gravity="center_horizontal" > <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="短信" android:textColor="#000" /> </LinearLayout></LinearLayout>
效果图
更多相关文章
- NPM 和webpack 的基础使用
- 【阿里云镜像】使用阿里巴巴DNS镜像源——DNS配置教程
- android 保存和读取文件
- 【Android】动态注册广播接收器
- Android追加写入文件的几种方法
- android xml布局中TextView文字居中方法
- Anubis,一个支持Android(安卓)apk(Android(安卓)binaries)行为分
- android java 调用 .so 动态库实例
- android 读写文件