Android的 - 广播接收器不会被解雇 [英] Android - Broadcast Receiver not being fired

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

问题描述

我知道这已被要求ALOT在这里,但我一直在淘interwebs几个小时,我甚至重新使用我的一些previous code,用以接收短信'和我...什么都没有。

I know this has been asked ALOT on here, but I have been scouring the interwebs for hours and I have even reused some of my previous code for receiving sms' and I got...nothing.

所以,在这里不用,基本的应用程序,以接收短信,但应用程序从未收到的意图。我想如果文字是来自同一个手机发送的,但这似乎不是如此,因为其他应用程序拿起文本精细的意图可能会被忽略。

So, here goes, basic app to receive SMS but the app never receives the intent. I thought the intent may be ignored if the text is sent from the same phone but that does not seem to be the case, as other apps pick up the text fine.

下面是我的清单:

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

和,这里是接收器,似乎没有什么是新来的,所以我不知道是什么问题:

And, here is the receiver, nothing seems to be new here, so I have no idea what the problem is:

package com.encima.smsreceiver;

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

public class MessageReceiver extends BroadcastReceiver {

    private static final String TAG = "Message recieved";
    @Override
    public void onReceive(Context context, Intent intent) {
         Bundle pudsBundle = intent.getExtras();
         Object[] pdus = (Object[]) pudsBundle.get("pdus");
         SmsMessage messages =SmsMessage.createFromPdu((byte[]) pdus[0]);    
         Log.i(TAG,  messages.getMessageBody());
         Toast.makeText(context, "SMS Received : "+messages.getMessageBody(),
         Toast.LENGTH_LONG).show();
    }

}

调试手机,我现在用的就是运行2.2.2,我有其他应用程序的运行来检测手机短信,包括一些我自己的。

The debug phone I am using is running 2.2.2 and I have other apps running that detect sms, including some of my own.

任何洞察到这将是AP preciated!

Any insight into this would be appreciated!

感谢

推荐答案

由于短信广播的意图是通过发送 <一href="http://developer.android.com/reference/android/content/Context.html#sendBroadcast%28android.content.Intent%29"相对=nofollow> Context.sendOrderedBroadcast(...), 如果任何其他应用程序注册的BroadcastReceiver,并呼吁abortBroadcast,其他的接收器将无法获得播出。

Because the SMS broadcast intent is sent by Context.sendOrderedBroadcast(...), if any other app registers the BroadcastReceiver and calls abortBroadcast, the other receiver will not get the broadcast.

要提高你的应用程序的概率接收广播创建一个IntentFilter的,使用的 IntentFilter.setPriority

To increase the probability of your app receiving the broadcast create an IntentFilter, use IntentFilter.setPriority.

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

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