Android短信监听(一)——利用BroadcastReceiver实现短信监听

时间:2014-05-15 23:56:53   收藏:0   阅读:600

MainActivity如下:

package cc.testsmsbroadcastreceiver;
import cc.testsmsbroadcastreceiver.SMSBroadcastReceiver.MessageListener;
import android.os.Bundle;
import android.widget.TextView;
import android.app.Activity;
/**
 * Demo描述:
 * 利用BroadcastReceiver实现监听短信
 * 
 * 注意权限:
 * <uses-permission android:name="android.permission.RECEIVE_SMS"/>
 * 
 * 详细资料:
 * http://blog.csdn.net/lfdfhl/article/details/8195400
 * 
 */
public class MainActivity extends Activity{
    private TextView mTextView;
    private SMSBroadcastReceiver mSMSBroadcastReceiver;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		init();
	}
    private void init(){
    	mTextView=(TextView) findViewById(R.id.textView);
    	mSMSBroadcastReceiver=new SMSBroadcastReceiver();
    	mSMSBroadcastReceiver.setOnReceivedMessageListener(new MessageListener() {
			@Override
			public void OnReceived(String message) {
				mTextView.setText(message);
			}
		});
    }

}

SMSBroadcastReceiver如下:

package cc.testsmsbroadcastreceiver;
import java.text.SimpleDateFormat;
import java.util.Date;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.telephony.SmsMessage;
/**
 * 配置广播接收者:
 *  <receiver android:name=".SMSBroadcastReceiver">
 *     <intent-filter android:priority="1000">
 *         <action android:name="android.provider.Telephony.SMS_RECEIVED"/>
 *     </intent-filter>
 *  </receiver>
 *  
 *  注意:
 *  <intent-filter android:priority="1000">表示:
 *  设置此广播接收者的级别为最高
 */

public class SMSBroadcastReceiver extends BroadcastReceiver {
    private static MessageListener mMessageListener;
	public SMSBroadcastReceiver() {
		super();
	}

	@Override
	public void onReceive(Context context, Intent intent) {
		     Object [] pdus= (Object[]) intent.getExtras().get("pdus");
		     for(Object pdu:pdus){
		    	SmsMessage smsMessage=SmsMessage.createFromPdu((byte [])pdu);
		    	String sender=smsMessage.getDisplayOriginatingAddress();
		    	String content=smsMessage.getMessageBody();
		    	long date=smsMessage.getTimestampMillis();
		    	Date timeDate=new Date(date);
		    	SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		    	String time=simpleDateFormat.format(timeDate);
		    	
		    	System.out.println("短信来自:"+sender);
		    	System.out.println("短信内容:"+content);
		    	System.out.println("短信时间:"+time);
		    	
		    	mMessageListener.OnReceived(content);
		    	
		    	//如果短信来自5556,不再往下传递
		    	if("5556".equals(sender)){
		    		System.out.println(" abort ");
		    		abortBroadcast();
		    	}
		    	
		     }
	}
	
	// 回调接口
		public interface MessageListener {
			public void OnReceived(String message);
		}

		public void setOnReceivedMessageListener(MessageListener messageListener) {
			this.mMessageListener=messageListener;
		}
}

main.xml如下:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
     >

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world"
        android:layout_centerInParent="true" />

</RelativeLayout>

AndroidManifest.xml如下:

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

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="8" />
    
    <uses-permission android:name="android.permission.RECEIVE_SMS"/>
    

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="cc.testsmsbroadcastreceiver.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        
        <receiver android:name=".SMSBroadcastReceiver">
			<intent-filter android:priority="1000">
				<action android:name="android.provider.Telephony.SMS_RECEIVED"/>
			</intent-filter>
		</receiver>
    </application>

</manifest>


Android短信监听(一)——利用BroadcastReceiver实现短信监听,布布扣,bubuko.com

评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!