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>



更多相关文章

  1. Android 一个下载任务分为多个线程下载
  2. Android 多线程1
  3. Android 处理多线程 UserTask
  4. Android 里子线程真的不能刷新UI吗?
  5. Android 你不知道的Service(服务) & Thread(线程)

随机推荐

  1. Android应用程序与SurfaceFlinger服务的
  2. Android(安卓)Stagefright
  3. Android书籍分享
  4. RelativeLayout中常用属性解释
  5. 初识Android
  6. Android中R.java没有自动生成解决方案
  7. 我的Android(安卓)4 学习系列之开始入手:
  8. Android(安卓)-- 无线网络配置信息的管理
  9. 优秀的Android开源软件
  10. Android消息机制浅析——面试总结