BOOT_COMPLETED意图行动广播不能正常工作 [英] broadcasting of BOOT_COMPLETED intent action does not work properly

查看:288
本文介绍了BOOT_COMPLETED意图行动广播不能正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有几个动作的接收器类监听,但它不能赶上 android.intent.action.BOOT_COMPLETED 的行动。我做错了什么?这里是我的清单文件:

 <使用-SDK安卓的minSdkVersion =7/>
<使用-权限的Andr​​oid:名称=android.permission.RECEIVE_BOOT_COMPLETED/>
    !<  - <接收器的Andr​​oid版本:NAME =OtherReceiver。>
        <意向滤光器>
            <作用机器人:名称=android.intent.action.BOOT_COMPLETED/>
    &所述; /意图滤光器>
    &所述; /接收机GT  - →;
    <接收器的Andr​​oid版本:NAME =com.myApp.Ap preceiver安卓权限=android.permission.RECEIVE_BOOT_COMPLETED>
        <意向滤光器>
            <作用机器人:名称=android.intent.action.BOOT_COMPLETED/>
             <作用机器人:名称=android.intent.action.PACKAGE_ADDED/>
             <作用机器人:名称=com.myApp.wifitimer/>
            <作用机器人:名称=android.intent.action.PACKAGE_REPLACED/>
            <数据机器人:计划=包机器人:路径=com.myApp/>
    &所述; /意图滤光器>
    < /接收器>
 

,因为它可以看出我再次加入许可的接收器内,并在接收器的名称获得的类的全名作为这样的答案建议。
这里是广播接收器类:

  @覆盖
公共无效的onReceive(背景为arg0,意图ARG1){

    串动作1 = arg1.getAction();

    如果(action1.equals(Intent.ACTION_BOOT_COMPLETED)){
        Log.d(接收器,动作:启动);
    }
    如果(action1.equals(android.intent.action.PACKAGE_REPLACED)){
        Log.d(接收器,动作是:包);
    }
}
 

当我运行的应用程序的接收器捕获 android.intent.action.PACKAGE_REPLACED 但是当我重新启动手机,接收器不赶 BOOT_COMPLETED
然而,当我在 .OtherReceiver 在注释中的 Mainfest 的文件,它可以抓住它!
这里是这个类的code:

  OtherReceiver延伸的BroadcastReceiver {public类

@覆盖
公共无效的onReceive(背景为arg0,意图ARG1){

    串动= arg1.getAction();

    如果(action.equals(Intent.ACTION_BOOT_COMPLETED)){
        Log.d(新的接收器,动作:启动);
    }

}
}
 

正好相同的另一个。所以我的问题是,为什么我需要定义的 BOOT_COMPLETED 操作单独的接收?
修改我也试图通过亚洲开发银行根据这个,并且没有任何许可,我可以用的鸭preceiver抓住它的类:

  AM广播-a android.intent.action.BOOT_COMPLETED -c android.intent.category.HOME -n com.blubuk / .AP preciever
 

解决方案

首先,删除安卓权限=android.permission.RECEIVE_BOOT_COMPLETED<接收器GT; 元素

二,你的<数据> 部分的<意向滤光器> 正在申请的所有<作用>内元素 <意向滤光器> ,你不想。没有乌里 ACTION_BOOT_COMPLETED

不过,而不是创建一个单独的<接收器> 元素,你可以只创建一个单独的<意向滤光器> 元素的原<接收器> 元素。将你的<作用机器人:名称=android.intent.action.BOOT_COMPLETED/> <意向滤光器> (也许这 com.myApp.wifitimer 一个了),使他们不受<数据> 你的第一个<意向滤光器>

I have a receiver class listening on several actions but it can not catch the android.intent.action.BOOT_COMPLETED action. What I am doing wrong? here is my manifest file:

<uses-sdk android:minSdkVersion="7" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
    <!--<receiver android:name=".OtherReceiver">
        <intent-filter>
            <action android:name="android.intent.action.BOOT_COMPLETED" />
    </intent-filter>
    </receiver>-->
    <receiver android:name="com.myApp.AppReceiver" android:permission="android.permission.RECEIVE_BOOT_COMPLETED">
        <intent-filter>
            <action android:name="android.intent.action.BOOT_COMPLETED" />
             <action android:name="android.intent.action.PACKAGE_ADDED"/>
             <action android:name="com.myApp.wifitimer"/>
            <action android:name="android.intent.action.PACKAGE_REPLACED" />
            <data android:scheme="package" android:path="com.myApp" />
    </intent-filter>
    </receiver>

as it can be seen I added the permission again inside the receiver and the name of the receiver gets the full name of the class as this answer suggests.
here is the broadcast receiver class:

@Override
public void onReceive(Context arg0, Intent arg1) {

    String action1 = arg1.getAction();

    if(action1.equals(Intent.ACTION_BOOT_COMPLETED)) {
        Log.d("receiver","action is: boot");
    }
    if(action1.equals("android.intent.action.PACKAGE_REPLACED")) {
        Log.d("receiver","action is: package");
    }
}

When I run the app the receiver catches the android.intent.action.PACKAGE_REPLACED but when I restart the phone the receiver does not catch the BOOT_COMPLETED.
However when I comment in the .OtherReceiver in the Mainfest file it can catch it!
here is the code of this class:

public class OtherReceiver extends BroadcastReceiver {

@Override
public void onReceive(Context arg0, Intent arg1) {

    String action = arg1.getAction();

    if(action.equals(Intent.ACTION_BOOT_COMPLETED)) {
        Log.d("new receiver","action is: boot");
    }

}   
}

just the same as the other one. So my question is why I need define a separate receiver for the BOOT_COMPLETED action?
Edit: I also tried to send the action via adb according to this, and without any permission I could catch it with the AppReceiver class:

am broadcast -a android.intent.action.BOOT_COMPLETED -c android.intent.category.HOME -n com.blubuk/.AppReciever

解决方案

First, remove android:permission="android.permission.RECEIVE_BOOT_COMPLETED" from your <receiver> element.

Second, your <data> portion of your <intent-filter> is applying to all <action> elements within that <intent-filter>, which you do not want. There is no Uri on ACTION_BOOT_COMPLETED.

However, rather than creating a separate <receiver> element, you could just create a separate <intent-filter> element on your original <receiver> element. Move your <action android:name="android.intent.action.BOOT_COMPLETED" /> to the new <intent-filter> (and perhaps that com.myApp.wifitimer one too), so that they are unaffected by the <data> of your first <intent-filter>.

这篇关于BOOT_COMPLETED意图行动广播不能正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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