SMS_RECEIVED onReceive android 2.3.5不会触发

时间:2012-01-10 12:12:57

标签: java android broadcastreceiver telephony android-2.3-gingerbread

我最近对这个问题感到困惑,在2.3.5之前,这似乎工作正常(在我的同事设备上)。然而,在我看来它现在永远不会触发。

我已经剥离了代码,并制作了一个非常简单的测试应用程序来查看正在发生的事情。基本上onReceive代码似乎没有触发,即使adb / logcat似乎确实显示了BroadcastReveiver的寄存器确实发生。

这是我去过的简单代码:

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

<uses-sdk android:minSdkVersion="9" />

<application
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name" >
    <activity
        android:label="@string/app_name"
        android:name=".TestSMSReceiveActivity" >
        <intent-filter >
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <receiver android:name=".mysmstestcall">
    <intent-filter>
        <action android:name="android.provider.Telephony.SMS_RECEIVED"></action>
    </intent-filter>
</receiver>
</application>
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
<uses-permission android:name="android.permission.RECEIVE_SMS"></uses-permission>
</manifest>

然后:

package com.broadcasttech.testsmsreceive;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.IntentFilter;
import android.os.Bundle;
import android.util.Log;

public class TestSMSReceiveActivity extends Activity {
private BroadcastReceiver receiver;
private static final String TAG = "TestSMSApp";

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    Log.i(TAG, " App has started up");
    IntentFilter filter = new IntentFilter();
    filter.addAction("android.provider.Telephony.SMS_RECEIVED");
    Log.i(TAG, " Filter SMS_RECEIVED has been added");
    //Extends BroadcastReceiver
    receiver = new mysmstestcall();
    registerReceiver(receiver,filter);
    Log.i(TAG, " registerReceiver sorted");
}

//Also, to save headaches later
@Override
protected void onDestroy() {
  Log.i(TAG, " unregistering Receiver");
  unregisterReceiver(receiver);
  Log.i(TAG, " done");
}
}

最后

package com.broadcasttech.testsmsreceive;

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

public class mysmstestcall extends BroadcastReceiver {

private static final String SMS_RECEIVED = "android.provider.Telephony.SMS_RECEIVED";
private static final String TAG = "TestSMSApp";

@Override
public void onReceive(Context context, Intent intent) {
     Log.i(TAG, "Intent recieved: " + intent.getAction());
     if (intent.getAction() == SMS_RECEIVED) {
         //any action you want here..
         Log.i(TAG, "SMS received has triggered");
     }
}
}

所以这是一个相当简单的应用程序,只需记录并告诉我何时触发BroadcastReceiver,但它根本不会触发。

任何人都可以提出错误的建议,我已经检查了各种教程,检查过,因为我知道IceCreamSandwich是不同的,但也试图加入这些修补程序,这也没有什么区别。

提前致谢!

4 个答案:

答案 0 :(得分:2)

主要问题是“mysmstestcall”中的这一行是错误的:

if (intent.getAction() == SMS_RECEIVED)

应改为:

if (intent.getAction().equals(SMS_RECEIVED)) 

答案 1 :(得分:0)

你有两个接收器的原因吗?你有一个程序员监听器,你有一个 XML监听器

<强>程序化

filter.addAction("android.provider.Telephony.SMS_RECEIVED");
Log.i(TAG, " Filter SMS_RECEIVED has been added");
//Extends BroadcastReceiver
receiver = new mysmstestcall();

XML侦听器

<receiver android:name=".mysmstestcall">
<intent-filter>
    <action android:name="android.provider.Telephony.SMS_RECEIVED"></action>
</intent-filter>

你确定要这两个吗?如果你有两个,你将收到相同的广播2次......

至于另一个 ISSUE ,我看到的就是这一行

if (intent.getAction() == SMS_RECEIVED)

在比较字符串时,您不要比较它们,而不是像这样:

if (intent.getAction().equalsIgnoreCase(SMS_RECEIVED))

答案 2 :(得分:0)

我有过一次。这可能是网络的问题。这些是我question and answer。您可以添加发送意图并捕获结果代码。就我而言,它是RESULT_ERROR_GENERIC_FAILURE。我试图找到其他解决方案几个月但没有运气,所以我接受了,即使我不想: - (

答案 3 :(得分:0)

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.SmsMessage;
import android.widget.Toast;

public class SmsBroadcastReceiver extends BroadcastReceiver{
    public static final String SMS_BUNDLE = "pdus";
    @Override
    public void onReceive(Context context, Intent intent) {
        Bundle intentExtras = intent.getExtras();
        if (intentExtras != null) {
            Object[] sms = (Object[]) intentExtras.get(SMS_BUNDLE);
            String smsMessageStr = "";
            for (int i = 0; i < sms.length; ++i) {
                SmsMessage smsMessage = SmsMessage.createFromPdu((byte[]) sms[i]);

                String smsBody = smsMessage.getMessageBody().toString();
                String address = smsMessage.getOriginatingAddress();

                smsMessageStr += "SMS From: " + address + "\n";
                smsMessageStr += smsBody + "\n";
            }
            Toast.makeText(context, "A new message is added to the SMS List!!!\n"+smsMessageStr, Toast.LENGTH_SHORT).show();

            //this will update the UI with message
            InboxMain inst = InboxMain.instance();
            inst.updateList(smsMessageStr);

    }

}
}


manifest::
<receiver android:name=".SmsBroadcastReceiver" android:permission="android.permission.BROADCAST_SMS" android:exported="true">
    <intent-filter android:priority="2147483647" >
        <action android:name="android.provider.Telephony.SMS_RECEIVED" />
    </intent-filter>
 </receiver>

permission::
<uses-permission android:name="android.permission.SEND_SMS"></uses-permission>
<uses-permission android:name="android.permission.RECEIVE_SMS"></uses-permission>
<uses-permission android:name="android.permission.VIBRATE"/>
<uses-permission android:name="android.permission.WRITE_SMS" />
    <uses-permission android:name="android.permission.READ_SMS" />
    <uses-permission android:name="android.permission.RECEIVE_SMS" />