首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 移动开发 > Android >

BroadcastReceiver出现不能监听解决方法

2013-07-08 
BroadcastReceiver出现不能监听小弟刚学android不久,用BroadcastReceiver开发了一个短信监听器。code如下:

BroadcastReceiver出现不能监听
小弟刚学android不久,用BroadcastReceiver开发了一个短信监听器。
code如下:


<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.android.listener"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        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" >
        <receiver android:name="com.android.listener.MessageReciver" >
            <intent-filter android:priority="1000">
                <action android:name="android.provider.Telephony.SMS_RECEIVED" />
            </intent-filter>
        </receiver>
    </application>

</manifest>

public class MessageReciver extends BroadcastReceiver {

private final static String tag = "MessageReciver";
@Override
public void onReceive(Context context, Intent intent) {

Log.i(tag, "hello");

/*Object[] pdus = (Object[]) intent.getExtras().get("pdus");

for(Object pdu:pdus){

SmsMessage message = SmsMessage.createFromPdu((byte[]) pdu);
System.out.println(message.getOriginatingAddress());
System.out.println(message.getMessageBody());
}*/
}

}

可是把程序部署到模拟器上,用DDMS模拟给模拟器发送短信,却在控制台上打印不出hello,就更别说message了。
我在模拟器的应用程序管理器里确实也找到了该应用,但却没有该进程存在。
版本用的4.2的,求大神支招。


[解决办法]
package com.demo;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;

/**
 * 
 * @author Gu歌哥
 * 
 */
public class DemoReceiver extends BroadcastReceiver {

String tag = this.getClass().getSimpleName();

public void onReceive(Context context, Intent intent) {

String action = "android.provider.Telephony.SMS_RECEIVED";

if (intent != null) {
if (intent.getAction().equals(action)) {

Log.e(tag, "action-->" + intent.getExtras());
this.abortBroadcast();
}
}

}

}


<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.demo"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="15" />

    <uses-permission android:name="android.permission.RECEIVE_SMS" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <receiver android:name="com.demo.DemoReceiver" >
            <intent-filter android:priority="1000" >
                <action android:name="android.provider.Telephony.SMS_RECEIVED" />
            </intent-filter>
        </receiver>
    </application>

</manifest>

热点排行