Android broadcastreceiver似乎已经停止工作了

时间:2010-08-30 19:57:36

标签: android broadcastreceiver

我目前正在开发一个使用broadcastreceiver来检查传入短信的应用,但突然间它似乎停止了工作,我甚至写了一个小测试应用程序,至少对我来说,似乎语法正确,但也没有运作。

以下是测试项目的代码:

清单:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.AGApplications.test"
      android:versionCode="1"
      android:versionName="1.0">
    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".test"
                  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=".MsgMon"> 
            <intent-filter> 
                <action android:name=
                    "android.provider.Telephony.SMS_RECEIVED" /> 
            </intent-filter> 
        </receiver>

    </application>

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

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

BroadcastReceiver:

package com.AGApplications.test;

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

public class MsgMon extends BroadcastReceiver{

   @Override
   //Called when new message is received
   public void onReceive(Context context, Intent intent) {
      Log.d("PHONE", "Message Received");
   }
}

主要活动:

package com.AGApplications.test;

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

public class test extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }
}

据我所知,我没有做错任何事,但显然我是!

2 个答案:

答案 0 :(得分:1)

所以我发现了这个问题。您不能使用“PHONE”作为调试标记。不要问我为什么,因为我似乎无法弄清楚,但在使用多个不同的标签后,“PHONE”是唯一一个不会注册的标签。

我的小虫似乎躺在别处,我又在路上炙手可热!谢谢大家的帮助!

答案 1 :(得分:0)

对我来说似乎没事,我做了同样的例子

这是我的代码:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.infostretch.broadcastex"
      android:versionCode="1"
      android:versionName="1.0">
    <application android:icon="@drawable/icon" android:label="@string/app_name">


    <receiver android:name="broadcastex" android:label="@string/app_name"><intent-filter><action android:name="android.provider.Telephony.SMS_RECEIVED"></action>
</intent-filter>
</receiver>
</application>



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


public class broadcastex extends BroadcastReceiver {
    /** Called when the activity is first created. */
    private static final String TAG = "smsfwd";
    private static final String SMS_RECEIVED = "android.provider.Telephony.SMS_RECEIVED";

public void onReceive(Context context, Intent intent) {

    Log.i(TAG, "Intent recieved: " + intent.getAction());

            Log.i(TAG, "Message recieved: " + messages[0].getMessageBody());
            if (messages.length > -1) {
                Log.i(TAG, "Message recieved: " + messages[0].getMessageBody());

            }
        }

这个代码在android 2.1中为我工作试试我认为一切都和你一样