猿问

BroadcastReceiver没有收到BOOT_COMPLETED

我到处都是类似的问题,但是由于某种原因,我的BroadcastReceiver从未收到过android.intent.action.BOOT_COMPLETED Intent。


这是我的(相对)Android.Manifest文件:


<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"></uses-permission>    

<receiver android:name=".BootReceiver"

        android:enabled="true"

        android:exported="true"

        android:label="BootReceiver">

        <intent-filter>

            <action android:name="android.intent.action.BOOT_COMPLETED"></action>


        </intent-filter>

    </receiver>

这是实际的接收方。


public class BootReceiver extends BroadcastReceiver {

private static final String TAG="BootReceiver";


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

    try{

        context.startService(new Intent(context,ConnectivityListener.class));

        Log.i(TAG,"Starting Service ConnectivityListener");

    }catch(Exception e){

        Log.e(TAG,e.toString());

    }

}

}

谢谢!任何帮助是极大的赞赏


跃然一笑
浏览 1059回答 3
3回答

慕盖茨4494581

这似乎是解决此问题的最前线,因此我想为我的C#同事添加解决方案。在尝试了这里的所有内容后,我绞尽脑汁想弄清楚自己在做什么,但无济于事。我终于弄清楚了什么地方出了问题,这和这里有关C#Mono开发的建议有些不同。基本上,可以归结为我刚刚学到的困难方法。使用C#不要手动修改AndroidManifest.xml!请参阅此指南以供参考: Xamarin:使用AndroidManifest.xml对于这个问题,更直接的方法是完成此操作。首先,在项目属性的“清单”选项卡下,有一个复选框列表,用于选择要提供的权限,其中之一是RECEIVE_BOOT_COMPLETED。检查以提供这些权限。其次,您需要在BroacastReceiver类上放置适当的标签。[BroadcastReceiver][IntentFilter(new String[]{ Intent.ActionBootCompleted }, Priority = (int)IntentFilterPriority.LowPriority)]public class MyBootReceiver : BroadcastReceiver{&nbsp; &nbsp;public override void OnReceive(Context context, Intent intent)&nbsp; &nbsp;{&nbsp; &nbsp; &nbsp; // Do your boot work here, set alarms, show toasts, whatever&nbsp; &nbsp;}}不需要[IntentFilter()]的最后一部分来处理优先级,它只是让其他更高优先级的东西在启动时首先完成,如果您的应用程序不是高优先级的事情,这是个好习惯。正如您将在链接的文章中看到的那样,在代码中使用这些标记将导致在构建时以应有的方式创建AndroidManifest.xml文件。我发现的是,当手动修改清单以包含接收者标签时,系统导致它在一级上搜索的类太深,从而引发了ClassNotFound异常。它试图实例化[Namespace]。[Namespace]。[BroadcastReceiver],这是错误的。之所以这样做,是因为手动进行了清单编辑。无论如何,希望这会有所帮助。另外,使用adb工具的另一个快速技巧。如果要获取更易于阅读的日志版本,请尝试以下操作:C:\ Android \ platform-tools \ adb logcat >> C:\ log.txt这会将logcat转储到一个文本文件中,您可以打开该文本文件,并且比在命令提示符窗口中阅读还容易一些。使事物的剪切和粘贴也更加容易。
随时随地看视频慕课网APP

相关分类

Android
我要回答