当前位置:操作系统 > 安卓/Android >>

Android-利用广播Broadcast接收SMS短信

BroadcastReceiver
[java] 
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注册
[html]  
<receiver android:name=".BroadCastTest2_SMS">  
    <intent-filter >  
        <action android:name="android.provider.Telephony.SMS_RECEIVED"/>  
    </intent-filter>  
</receiver>  
 
权限
[html]  
<uses-permission android:name="android.permission.RECEIVE_SMS"/>  
 
Activity文件
[java] 
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布局文件
[html]  
<?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" >  
  
     
补充:移动开发 , Android ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,