SMS_RECEIVED onReceive android 2.3.5 未触发

发布于 2024-12-26 04:58:54 字数 3305 浏览 3 评论 0原文

我最近一直对此感到困惑,在 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 是不同的,但也尝试合并这些修复程序,这也没有什么区别。

提前致谢!

I've been puzzling over this one recently, prior to 2.3.5 this seems to work fine (on my colleagues devices). However on mine it now never triggers.

I've stripped the code right back and made a very simple test application to see what's going on. Basically the onReceive code doesn't ever appear to trigger, even though adb/logcat does seem to show the register of the BroadcastReveiver does take place.

Here's the simple code I've gone for:

<?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>

Then:

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");
}
}

And finally

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");
     }
}
}

So it is a fairly simple app that should just log and tell me when the BroadcastReceiver is triggered, but it just won't fire at all.

Can anyone suggest whats wrong, I've checked various tutorials, checked as I know IceCreamSandwich is different, but tried to incorporate those fixes too and this doesn't make a difference either.

Thanks in advance!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

蔚蓝源自深海 2025-01-02 04:58:54

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

if (intent.getAction() == SMS_RECEIVED)

应更改为:

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

The main problem is that this line in "mysmstestcall" is wrong:

if (intent.getAction() == SMS_RECEIVED)

should be changed to this:

if (intent.getAction().equals(SMS_RECEIVED)) 
人间☆小暴躁 2025-01-02 04:58:54

您有两个接收器是有原因的吗?您有一个程序化侦听器和一个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>

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

至于我看到的另一个问题是这一行当

if (intent.getAction() == SMS_RECEIVED)

比较字符串时,您比较他们就像这样,而不是你会得到这样的东西:

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

Is there a reason that you have two recievers? You have a programatic listener and you have an XML listener

Programatic:

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

XML Listener:

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

Are you sure you want these two? If you have two you will RECEIVE the same broadcast 2 times...

As for another ISSUE that I see is this line

if (intent.getAction() == SMS_RECEIVED)

When comparing strings you DO NOT compare them like this instead you'd have something like this:

if (intent.getAction().equalsIgnoreCase(SMS_RECEIVED))
魂ガ小子 2025-01-02 04:58:54

我就遇到过一次这个可能是网络问题。这些是我的问题和答案。您可以添加发送意图并捕获结果代码。就我而言,它是 RESULT_ERROR_GENERIC_FAILURE 。我尝试了几个月寻找其他解决方案,但没有成功,所以我接受了这一点,尽管我不想:-(

I've got this once. It can be network's issue. These were my question and answer. You can add a sending intent and catch the result code. In my case, it was RESULT_ERROR_GENERIC_FAILURE. I tried to find other solutions for months but no luck, so I accepted that even though I don't want to :-(

莫多说 2025-01-02 04:58:54
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" />
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" />
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文