程序通过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;@Overridepublic 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 {@Overridepublic 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>


更多相关文章

  1. Android调用系统设置
  2. Android(安卓)Trusty TEE 2.启动分析
  3. android联系人、短信、邮件url总结
  4. Android(安卓)获取IMEI号码
  5. 设置菜单默认值设置
  6. android 权限集合
  7. Android程序全屏方法
  8. Android之使用PackageManager取得程序的包名、图标等
  9. Android双击返回键退出程序代码

随机推荐

  1. android studio使用感受
  2. 【android】uses-permission和permission
  3. Android-0 Android studio编译报错相关
  4. android自定义对话框工具类
  5. Android深入浅出之Binder机制
  6. android状态栏一体化
  7. Android Applications Tutorial 23. HTTP
  8. android 获取信息和安装Apk
  9. android学习——android 常见的错误 和
  10. Android openGL hook