Android之短信监听与内容获取

来源:互联网 发布:国外类似知乎的网站 编辑:程序博客网 时间:2024/05/05 18:38

主题:监听短信并获取短信内容

  1. 设置权限:
    <?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android"    package="com.lin.sms"    android:versionCode="1"    android:versionName="1.0" >    <uses-sdk        android:minSdkVersion="14"        android:targetSdkVersion="21" />    <span style="color:#ff0000;"><uses-permission android:name="android.permission.RECEIVE_SMS" /></span>     <span style="color:#ff0000;"><uses-permission android:name="android.permission.READ_SMS" /></span>    <application        android:allowBackup="true"        android:icon="@drawable/ic_launcher"        android:label="@string/app_name"        android:theme="@style/AppTheme" >        <activity            android:name="com.lin.sms.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>    </application></manifest>

  2. 布局文件:<pre name="code" class="html"><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/textView1"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_alignParentLeft="true"        android:layout_alignParentTop="true"        android:layout_marginLeft="25dp"        android:layout_marginTop="104dp"        android:text="号码:" />    <TextView        android:id="@+id/textView3"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_alignRight="@+id/textView1"        android:layout_below="@+id/textView1"        android:layout_marginTop="29dp"        android:text="内容:" />    <TextView        android:id="@+id/content"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_alignBaseline="@+id/textView3"        android:layout_alignBottom="@+id/textView3"        android:layout_marginLeft="16dp"        android:layout_toRightOf="@+id/textView3"        android:hint="短信内容" />    <TextView        android:id="@+id/number"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_alignBaseline="@+id/textView1"        android:layout_alignBottom="@+id/textView1"        android:layout_alignLeft="@+id/content"        android:hint="来电号码" /></RelativeLayout>

  3. Java代码:<pre name="code" class="java">package com.lin.sms;import com.lin.sms.R;import android.app.Activity;import android.content.BroadcastReceiver;import android.content.Context;import android.content.Intent;import android.content.IntentFilter;import android.os.Bundle;import android.telephony.SmsMessage;import android.widget.TextView;public class MainActivity extends Activity {<span style="color:#ff0000;">public static final String SMS_RECEIVED_ACTION = "android.provider.Telephony.SMS_RECEIVED";</span>private TextView tel_num;private TextView tel_content;private String content;private String sender;<span style="color:#ff0000;">private BroadcastReceiver mReceiver = new BroadcastReceiver() </span>{@Overridepublic void onReceive(Context context, Intent intent) {// 先判断广播消息String action = intent.getAction();if (SMS_RECEIVED_ACTION.equals(action)) {// 获取intent参数Bundle bundle = intent.getExtras();// 判断bundle内容if (bundle != null) {// 取pdus内容,转换为Object[]Object[] pdus = (Object[]) bundle.get("pdus");// 解析短信SmsMessage[] messages = new SmsMessage[pdus.length];for (int i = 0; i < messages.length; i++) {byte[] pdu = (byte[]) pdus[i];messages[i] = SmsMessage.createFromPdu(pdu);}// 解析完内容后分析具体参数for (SmsMessage msg : messages) {content = msg.getMessageBody();sender = msg.getOriginatingAddress();if (sender.equals("10086")) {//此处为设置当来短信的号码为10086时候才把获取到的数据显示出来display();}}}}}};private void display() {tel_num.setText(sender);tel_content.setText(content);}@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);System.out.println("============首页");tel_num = (TextView) findViewById(R.id.number);tel_content = (TextView) findViewById(R.id.content);}@Overrideprotected void onStart() {<span style="color:#ff0000;">IntentFilter intentFilter = new IntentFilter();</span><span style="color:#ff0000;">intentFilter.addAction(SMS_RECEIVED_ACTION);</span><span style="color:#ff0000;">registerReceiver(mReceiver, intentFilter);</span>super.onStart();}@Overrideprotected void onDestroy() {<span style="color:#ff0000;">unregisterReceiver(mReceiver);</span>super.onDestroy();}}
总结:代码比较简单,相信大家看看就能懂,红色字体为关键处,勿漏。
0 0
原创粉丝点击