广播接收器的onReceive不解雇 [英] BroadcastReceiver onReceive is not fired

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

问题描述

我试图以应对短信的Andr​​oid接受funcions。
我阅读了大量的相关主题#2和其他网站,我试图用一个非常简单的类,它仅仅是一个消息已收到控制台和消息上打印,但我想不出为什么它不工作。

我看到了类似的问题,在过去仍未得到答复(见<​​a href=\"http://stackoverflow.com/questions/6966902/android-broadcast-receiver-not-being-fired\"> Android的 - 广播接收机不被解雇)

希望有人能找到的地方就是我的code中的问题。

code:

 包com.storassa.android.smsapp;进口android.content.BroadcastReceiver;
进口android.content.Context;
进口android.content.Intent;
进口android.os.Bundle;
进口android.telephony.SmsMessage;公共类SmsReceiver扩展广播接收器{私有静态最后弦乐SMS_RECEIVED =android.provider.Telephony.SMS_RECEIVED;
私有静态最后弦乐TAG =SMSBroadcastReceiver;@覆盖
公共无效的onReceive(上下文的背景下,意图意图){
的System.out.println(意图收到:+ intent.getAction());如果(intent.getAction()。等于(SMS_RECEIVED)){
捆绑包= intent.getExtras();
如果(捆绑!= NULL){
[对象]的PDU =(对象[])bundle.get(的PDU);
最后SmsMessage [] =邮件新SmsMessage [pdus.length]
的for(int i = 0; I&LT; pdus.length;我++){
消息[I] = SmsMessage.createFromPdu((字节[])的PDU [I]);
}
如果(messages.length -1个){
的System.out.println(消息收到+消息[0] .getMessageBody());
        }
    }
}
     }
}

和清单

 &LT;表明包=com.storassa.android.smsapp
    安卓版code =1
    机器人:=的versionName1.0的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android&GT;    &LT;采用-SDK安卓的minSdkVersion =6机器人:targetSdkVersion =6/&GT;
    &LT;使用许可权的android:NAME =android.permission.RECEIVE_SMS&GT;&LT; /使用许可权&GT;    &lt;应用机器人:标签=@字符串/ APP_NAME机器人:权限=android.permission.RECEIVE_SMS&GT;
        &lt;接收机器人:名字=com.storassa.android.smsapp.SmsReceiver
            机器人:启用=真
            机器人:出口=真正的&GT;
            &LT;意向过滤器的android:优先=999&GT;
                &lt;作用机器人:名字=android.provider.Telephony.SMS_RECEIVED&GT;&LT; /作用&gt;
            &所述; /意图滤光器&gt;
        &LT; /接收器&GT;    &LT; /用途&gt;
&LT; /清单&GT;


解决方案

这是发生在你还安装了具有高优先级则当前的应用程序等应用程序,所以增加你的优先级就会开始工作,我也有这问题很多的时候,在一个应用程序我的首要任务是100,这是行不通的,但是当我改成了999,然后开始工作,
因此,尝试不同的设置和大型无优先权并重新进行检查。

I'm trying to cope with SMS receiving funcions in Android. I read a lot of related topics on Stackoverflow and other sites and I tried with a very simple Class that simply print on the console that a message has been received and the message, but I cannot figure out why it doesn't work.

I see that similar questions remained unanswered in the past (see Android - Broadcast Receiver not being fired)

Hope someone could find where's the problem with my code.

Code:

package com.storassa.android.smsapp;

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

public class SmsReceiver extends BroadcastReceiver {

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

@Override
public void onReceive(Context context, Intent intent) {
System.out.println("Intent recieved: " + intent.getAction());

if (intent.getAction().equals(SMS_RECEIVED)) {
Bundle bundle = intent.getExtras();
if (bundle != null) {
Object[] pdus = (Object[])bundle.get("pdus");
final SmsMessage[] messages = new SmsMessage[pdus.length];
for (int i = 0; i < pdus.length; i++) {
messages[i] = SmsMessage.createFromPdu((byte[])pdus[i]);
}
if (messages.length > -1) {
System.out.println("Message recieved: " + messages[0].getMessageBody());
        }
    }
}
     }
}

And Manifest

<manifest package="com.storassa.android.smsapp"
    android:versionCode="1"
    android:versionName="1.0" xmlns:android="http://schemas.android.com/apk/res/android">

    <uses-sdk android:minSdkVersion="6" android:targetSdkVersion="6"/>
    <uses-permission android:name="android.permission.RECEIVE_SMS"></uses-permission>

    <application android:label="@string/app_name" android:permission="android.permission.RECEIVE_SMS">
        <receiver android:name="com.storassa.android.smsapp.SmsReceiver"
            android:enabled="true"
            android:exported="true">
            <intent-filter android:priority="999">
                <action android:name="android.provider.Telephony.SMS_RECEIVED"></action>
            </intent-filter>
        </receiver>

    </application>
</manifest>

解决方案

This is happens when you also have installed other application which has high priority then the current one apps, so increase your priority it will start working, i also got this problem many time, in one app my priority was 100 and it was not working but when i changed it to 999, then it starts working, So try to set different and large no priority and check it again.

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

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