|
程序通过BroadCastReceiver监听短消息,并判断短信的号码,当满足预设号码时,启动一个Activity。
SmsReceiverActivity.java- package com.zeph.android.sms.receiver;
- import android.app.Activity;
- import android.content.Intent;
- import android.os.Bundle;
- import android.widget.TextView;
- /**
- *
- * @author BenZeph
- *
- */
- public class SmsReceiverActivity extends Activity {
- private TextView myTextView;
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- Intent intent = getIntent();
- String message = intent.getStringExtra("MESSAGE");
- myTextView = (TextView) findViewById(R.id.myTextView);
- myTextView.setText(message);
- }
- }
复制代码 程序的重点在BroadCastReceiver
SmsBroadCastReceiver.java- package com.zeph.android.sms.receiver;
- import android.content.BroadcastReceiver;
- import android.content.Context;
- import android.content.Intent;
- import android.os.Bundle;
- import android.telephony.SmsMessage;
- /**
- *
- * @author BenZeph
- *
- */
- public class SmsBroadCastReceiver extends BroadcastReceiver {
- @Override
- public void onReceive(Context context, Intent intent) {
- String phoneNum = "";
- StringBuilder sb = new StringBuilder();
- Bundle bundle = intent.getExtras();
- if (bundle != null) {
- Object[] myObjectPuds = (Object[]) bundle.get("pdus");
- SmsMessage[] messages = new SmsMessage[myObjectPuds.length];
- for (int i = 0; i < myObjectPuds.length; i++)
- messages[i] = SmsMessage
- .createFromPdu((byte[]) myObjectPuds[i]);
- for (SmsMessage currentMessage : messages) {
- sb.append("消息来自:\n");
- sb.append(currentMessage.getDisplayOriginatingAddress());
- phoneNum = currentMessage.getDisplayOriginatingAddress();
- sb.append("\n短消息内容\n");
- sb.append(currentMessage.getDisplayMessageBody());
- }
- }
- if (phoneNum.equals("09681000631")) {
- Intent intentNewTask = new Intent(context,
- SmsReceiverActivity.class);
- intentNewTask.putExtra("MESSAGE", sb.toString());
- intentNewTask.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
- context.startActivity(intentNewTask);
- }
- }
- }
复制代码 Manifest要注册Receiver,同时提供短消息的User-Permission
Manifest- <?xml version="1.0" encoding="utf-8"?>
- <manifest xmlns:android="http://schemas.android.com/apk/res/android"
- package="com.zeph.android.sms.receiver"
- android:versionCode="1"
- android:versionName="1.0" >
- <uses-sdk android:minSdkVersion="8" />
- <uses-permission android:name="android.permission.RECEIVE_SMS"/>
- <application
- android:icon="@drawable/ic_launcher"
- android:label="@string/app_name" >
- <activity
- android:label="@string/app_name"
- android:name=".SmsReceiverActivity" >
- <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>
- <action android:name="android.provider.Telephony.SMS_RECEIVED"/>
- </intent-filter>
- </receiver>
- </application>
- </manifest>
复制代码 http://blog.csdn.net/benw1988/article/details/7000813 |
|