广播接收器不适用于SMS [英] Broadcast Receiver not working for SMS

查看:58
本文介绍了广播接收器不适用于SMS的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,我已经搜索了可能的解决方案,尝试了所有方法,但仍然无法正常工作.我一定想念一些东西.

First of all I already searched for possible solutions, tried everything and it still didn't work. I must be missing something.

我正在尝试创建一个接收/读取和写入SMS的应用程序. 写部分工作正常,我的广播接收器无法接收广播.

I am trying to create an app that receives/reads and writes SMS. the write part is working just fine, my broadcast receiver just doesn't catch broadcast.

    <uses-permission android:name="android.permission.RECEIVE_SMS" />
    <uses-permission android:name="android.permission.SEND_SMS" />
    <uses-permission android:name="android.permission.WRITE_SMS" /> 
    <uses-permission android:name="android.permission.READ_SMS" /> 
...
        <receiver android:name=".SmsReceiver"
            android:permission="android.permission.BROADCAST_SMS">
        <intent-filter>
            <action android:name="android.provider.Telephony.SMS_RECEIVED" />
        </intent-filter>
        </receiver>

SmsReceiver.java

public class SmsReceiver extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {

    Toast.makeText(context, "ON RECEIVE BROADCAST", Toast.LENGTH_LONG).show();
    Log.d("ON ","RECEIVE");
     Bundle bundle = intent.getExtras();
        Object[] messages = (Object[]) bundle.get("pdus");
        SmsMessage[] sms = new SmsMessage[messages.length];
        // Create messages for each incoming PDU
        for (int n = 0; n < messages.length; n++) {
            sms[n] = SmsMessage.createFromPdu((byte[]) messages[n]);
        }
        for (SmsMessage msg : sms) {
            Log.e("RECEIVED MSG",":"+msg.getMessageBody());
            // Verify if the message came from our known sender

        }
}

没有解雇原木或吐司.

试图将清单上的动作更改为android.intent.action.AIRPLANE_MODE只是为了测试声明并接收到广播,但这对SMS无效.

Tried changing action on manifest to android.intent.action.AIRPLANE_MODE just to test the declaration and the broadcast was received, is just not working for the SMS.

尝试在其他手机上使用,效果很好.一定是因为我使用的是Handcent SMS,以及某些如何阻止广播.无论哪种方式,我都需要它可以在每部电话上正常工作,而与安装的应用程序无关.

Tried on a different phone and it worked. Must be because I am using Handcent SMS, and some how its blocking the broadcast. Either way I need it to be working on every phone independent of the applications installed.

推荐答案

找到了一个可以回答我的疑问的主题:

Found a topic that answers my doubt: Suppress / Block BroadcastReceiver in another app.

即使将优先级设置为

Even with the priority set to the maximum possible (999), if another app has the same priority, in this case the Handcent SMS app, the first application that will receive the broadcast is the one that was first installed by the user.

在我的情况下是Handcent SMS,因为它在接收广播时中止广播,所以我的应用程序什么也没收到.

In my case was the Handcent SMS and because it aborts the broadcast when receiving it, my app doesn't receive anything.

这篇关于广播接收器不适用于SMS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆