Broadcast读取短信

 

一, AndroidManifest.xml

 

 

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.android.lei"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk android:minSdkVersion="14" />

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:label="@string/app_name"
            android:name=".BroadcastMSMActivity" >
            <intent-filter >
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>         
    </application>
   
    <uses-permission android:name="android.permission.RECEIVE_SMS"></uses-permission>

<!--获得SMS权限,否则BroadcastReceiver不能实现-->

 

</manifest>

 

二。Activity,注册广播,并接收广播

 

package com.android.lei;

import android.app.Activity;
import android.content.IntentFilter;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class BroadcastMSMActivity extends Activity {
    /** Called when the activity is first created. */
 private Button regist = null;
 private Button unregist = null;
 private SMSReceiver smsr = null;
 private static final String SMS_ACTION = "android.provider.Telephony.SMS_RECEIVED";
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
       
        regist = (Button)findViewById(R.id.regist);
        regist.setOnClickListener(new Regist());
        unregist = (Button)findViewById(R.id.unregist);
        unregist.setOnClickListener(new Unregist());
    }
   
    class Regist implements OnClickListener{

  @Override
  public void onClick(View v) {
   //creat一个BroadcastReceiver对象
   smsr = new SMSReceiver();
   //creat a filter object
   IntentFilter filter = new IntentFilter();
   //add Action atgument to filter
   filter.addAction(SMS_ACTION);
   //send a Broadcast to Receiver,为smsr对象注册Receive的filter
   BroadcastMSMActivity.this.registerReceiver(smsr, filter);
   
   System.out.println("Broadcast has Broaded!");
  }
     
    }
   
    class Unregist implements OnClickListener{

  @Override
  public void onClick(View v) {
   BroadcastMSMActivity.this.unregisterReceiver(smsr);
   //canel Broadcast
  }
     
    }
}

 

三,接收 广播时执行的方法

package com.android.lei;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.SmsMessage;

public class SMSReceiver extends BroadcastReceiver{

 @Override
 public void onReceive(Context context, Intent intent) {
  
  System.out.println("Broadcast has received!");
  Bundle bundle = intent.getExtras();
  //getExtrals:Retrieves a map of extended data from the intent.
  Object[] obj = (Object[])bundle.get("pdus");    
  
  SmsMessage[] message = new SmsMessage[obj.length];
  System.out.println(message.length);
  
  for(int i = 0; i<obj.length; i++){
   
   message[i] = SmsMessage.createFromPdu((byte[]) obj[i]);
   // 吧obj转化为字节数组传给message,Create an SmsMessage from a raw PDU.

   System.out.println(message[i].getDisplayMessageBody());
   //getDisplayMessageBody:Returns the message body, or email message body if this message was from an email gateway.
  }
 }

}

 

四,main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">"

    <Button
        android:id="@+id/regist"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="REGISTER"/>
    <Button
        android:id="@+id/unregist"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="UNREGISTER"/>"

</LinearLayout>

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值