Android的BroadcastReceiver在开机触发时报错
转成简体中文再发一次
今天在尝试使用BOOT_COMPLETED来触发BroadcastReceiver
但是找了网络上的范例一个一个下去try都没有办法成功
都是出现
java.lang.RuntimeException: Unable to instantiate receiver MyName.MyName.BootReceive:
java.lang.ClassNotFoundException: MyName.MyName.BootReceive
从字义上判断是呼叫不到我自定义的Class
项目结构如下
AndroidManifest.xml内容如下
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
<uses-sdk android:minSdkVersion="15" android:targetSdkVersion="15" />
<application>
<receiver android:name=".BootReceive" android:exported="true">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
</application>
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
</manifest>
BootReceiver程序代码内容如下
namespace MyName
{
[BroadcastReceiver]
public class BootReceiver : BroadcastReceiver
{
public override void OnReceive(Context context, Intent intent)
{
Toast.MakeText(context, "Received intent!", ToastLength.Short).Show();
}
}
}
目前问题应该是卡在AndroidManifest中的设定
目前已经试过
<receiver android:name=".BootReceive" android:exported="true">
<receiver android:name="BootReceive" android:exported="true">
<receiver android:name=".BootReceive">
<receiver android:name="BootReceive">
<receiver android:name="MyName.BootReceive" android:exported="true">
<receiver android:name="MyName.BootReceive">
<receiver android:name="MyName.MyName.BootReceive">
<receiver android:name="MyName.MyName.BootReceive" android:exported="true">
也参考了
http://forums.xamarin.com/discussion/2998/crash-with-boot-completed-receiver
的问题解决方式
在BootReceiver加上
[IntentFilter(new string[] { Intent.ActionBootCompleted }, Priority = (int)IntentFilterPriority.LowPriority)]
也是没办法正常运行
有没有人有相关的处理经验可以帮帮忙
[解决办法]
xml里面名称错了,少了个r,是BootReceiver 不是BootReceive
<receiver android:name=".BootReceiver" android:exported="true">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>