利用BroadcastReceiver监听短信_移动开发_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > 移动开发 > 利用BroadcastReceiver监听短信

利用BroadcastReceiver监听短信

 2011/1/17 7:40:03  gundumw100  http://gundumw100.javaeye.com  我要评论(0)
  • 摘要:随便一个Activitypackagecom.wt.app;importandroid.app.Activity;importandroid.os.Bundle;importandroid.view.View;importandroid.widget.Button;importandroid.widget.TextView;importandroid.widget.Toast;publicclassAppextendsActivity{privateTextViewtextview
  • 标签:利用 监听
随便一个Activity
package com.wt.app;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class App extends Activity {
	private TextView textview;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        textview = (TextView)this.findViewById(R.id.tv1);
		
		String string = "在DDMS中模拟发送一条短信到5554,就会有提醒!";
		textview.setTextSize(30);
		textview.setText(string);
		
		Button button = (Button) findViewById(R.id.button1);
		/* 监听button的事件信息 */
		button.setOnClickListener(new Button.OnClickListener() {
			public void onClick(View v)
			{
				displayToast("短信内容在上面显示!");
			}
		});
    }
    
	/* 显示Toast  */
	public void displayToast(String str)
	{
		Toast.makeText(this, str, Toast.LENGTH_SHORT).show();
	}
}




当监听到android.provider.Telephony.SMS_RECEIVED的intent-filter时启动SMSReceiver广播。
package com.wt.app;

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

public class SMSReceiver extends BroadcastReceiver
{
	/*当收到短信时,就会触发此方法*/
	public void onReceive(Context context, Intent intent)
	{
		Bundle bundle = intent.getExtras();
		Object messages[] = (Object[]) bundle.get("pdus");
		SmsMessage smsMessage[] = new SmsMessage[messages.length];
		for (int n = 0; n < messages.length; n++)
		{
			smsMessage[n] = SmsMessage.createFromPdu((byte[]) messages[n]);
		}
		//产生一个Toast
		Toast toast = Toast.makeText(context, "你有新的短信: " + smsMessage[0].getMessageBody(), Toast.LENGTH_LONG);
		//设置toast显示的位置
		//toast.setGravity(Gravity.TOP|Gravity.LEFT, 0, 200);
		//显示该Toast
		toast.show();
	}
}

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.wt.app"
      android:versionCode="1"
      android:versionName="1.0">
    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".App"
                  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=".SMSReceiver" android:enabled="true">  
			<intent-filter>
				<action android:name="android.provider.Telephony.SMS_RECEIVED"/>  
			</intent-filter>
		</receiver>
    </application>
	<uses-permission android:name="android.permission.RECEIVE_SMS"></uses-permission>
    <uses-sdk android:minSdkVersion="3" />

</manifest> 

在DDMS中模拟发送一条短信:


查看短信:


发表评论
用户名: 匿名