zoukankan      html  css  js  c++  java
  • 四大组件之一 BroadcastReceiver (短信拦截)

    main.xml

    View Code
    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width
    ="fill_parent"
    android:layout_height
    ="fill_parent"
    android:orientation
    ="vertical" >
    <TextView
    android:layout_width="fill_parent"
    android:layout_height
    ="wrap_content"
    android:id
    ="@+id/tv"
    android:text
    ="@string/hello" />
    </LinearLayout>

    SMSReceiver.java

    View Code
    package com.puyotech.sms;

    import android.content.BroadcastReceiver;
    import android.content.Context;
    import android.content.Intent;
    import android.telephony.SmsMessage;
    import android.util.Log;
    import android.widget.Toast;

    public class SMSReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
    Object[]sms=(Object[]) intent.getExtras().get("pdus");
    SmsMessage[] msg=new SmsMessage[sms.length];
    StringBuilder sb = new StringBuilder();
    Log.i("@@", "短信内容已经获得");
    for (int i = 0; i < sms.length; i++) {
    msg[i] = SmsMessage.createFromPdu((byte[]) sms[i]);
    }
    for (SmsMessage currMsg : msg) {
    sb.append("您收到了来自:【");
    sb.append(currMsg.getDisplayOriginatingAddress());
    sb.append("】的信息,内容:");
    sb.append(currMsg.getDisplayMessageBody());
    }
    Log.i("@@", sb.toString());
    Toast toast = Toast.makeText(context, "收到了短消息: " + sb.toString(),Toast.LENGTH_LONG);
    toast.show();
    }

    }

    SMSTestActivity.java

    View Code
    package com.puyotech.sms;

    import android.app.Activity;
    import android.os.Bundle;
    import android.widget.TextView;

    public class SMSTestActivity extends Activity {
    private TextView tv;

    @Override
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    new Thread(new Runnable() {

    @Override
    public void run() {
    tv=(TextView) findViewById(R.id.tv);

    tv.post(new Runnable() {
    @Override
    public void run() {
    tv.setText("你妹的,在子线程也可以实例化啊");
    }
    });

    }
    }).start();
    }
    }

    AndroidManifest.xml

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

    <uses-sdk android:minSdkVersion="8" />
    <uses-permission android:name="android.permission.RECEIVE_SMS"/>
    <uses-permission android:name="android.permission.READ_SMS"/>
    <application android:icon="@drawable/ic_launcher" android:label="@string/app_name" >
    <activity
    android:name=".SMSTestActivity"
    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:permission="android.permission.BROADCAST_SMS">
    <intent-filter android:priority="2147483648" >
    <action android:name="android.provider.Telephony.SMS_RECEIVED"/>
    <category android:name="android.intent.category.DEFAULT"/>
    <category android:name="android.intent.category.HOME"/>
    <category android:name="android.intent.category.LAUNCHER"/>
    <category android:name="android.intent.category.ALTERNATIVE"/>
    </intent-filter>
    </receiver>

    </application>

    </manifest>



  • 相关阅读:
    java程序打包成jar 配置文件信息路径
    django 认证系统
    django 表单验证和字段验证
    python 面向对象编程
    Python new() 方法
    Django 分页 以及自定义分页
    django 自己编写admin
    Django CRM客户关系管理系统
    Django model中的 class Meta 详解
    Django CRM系统
  • 原文地址:https://www.cnblogs.com/androidsj/p/2405734.html
Copyright © 2011-2022 走看看