【Android】利用广播Broadcast接收SMS短信

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";
	
	@Override
	public void onReceive(Context context, Intent intent) {
		// TODO Auto-generated method stub
		if (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;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.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>

效果图




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值