我正在尝试创建一个将在启动时启动的服务。
我在adb中看到以下错误:
05-31 10:14:24.060 E/AndroidRuntime( 1583):java.lang.RuntimeException: Unable to instantiate receiver MyNameSpace.MyClass.MyBootReceiver: java.lang.ClassNotFoundException: Didn't find class "MyNameSpace.MyClass.MyBootReceiver" on path: /data/app/MyNameSpace.MyClass-1.apk
05-31 10:14:24.060 E/AndroidRuntime( 1583): Caused by: java.lang.ClassNotFoundException: Didn't find class "MyNameSpace.MyClass.MyBootReceiver" on path: /data/app/MyNameSpace.MyClass-1.apk
问题是,我肯定看到MyBootReceiver
类存在于MyNameSpace
中,作为MyClass
的子类,所以它看起来像< em>应能够找到它。
我的清单定义为:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
android:versionCode="1"
android:versionName="1.0"
package="MyNamespace.MyClass">
<uses-sdk />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
<application>
<receiver android:name=".MyBootReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
<activity android:name=".MyActivity"></activity>
</application>
</manifest>
,这是代码:
namespace MyNamespace
{
[Service]
[IntentFilter(new String[] { "com.xamarin.MyClass" })]
public class MyClass : IntentService
{
private IBinder binder;
public const string MyUpdatedAction = "MyUpdated";
protected override void OnHandleIntent(Intent intent)
{
var myIntent = new Intent(MyUpdatedAction);
SendOrderedBroadcast(myIntent, null);
}
public override IBinder OnBind(Intent intent)
{
binder = new MyServiceBinder(this);
return binder;
}
[BroadcastReceiver]
[IntentFilter(new string[] { MyUpdatedAction }, Priority = (int)IntentFilterPriority.LowPriority)]
public class MyBootReceiver : BroadcastReceiver
{
public override void OnReceive(Context context, Android.Content.Intent intent)
{
Log.Info("MyTrace", "MyReceiver OnReceive action: " + intent.Action);
if (intent.Action.Contains("BOOT_COMPLETED"))
{
var pushIntent = new Intent(MyUpdatedAction);
context.StartService(pushIntent);
}
}
}
}
}
我需要做些什么来使这件事有效?
答案 0 :(得分:0)
我通常在描述清单文件中的项目时添加整个类路径。所以在你的情况下我会写:
<application>
<receiver android:name="MyNamespace.MyBootReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
<activity android:name="MyNamespace.MyActivity"></activity>
答案 1 :(得分:0)
我遇到了同样的问题并找到了解决方案。按照以下步骤使“内部”BroadcastReceiver正常运行,不会发生任何崩溃:
1)让你的mainActivity看起来像这样:
public class MainActivity extends AppCompatActivity
implements SomeListener {
MyReceiver myRecvr;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
myRecvr = new MyReceiver();
.......
}
...onResume() code ... onPause() code ... yourBroadcastReceiver Class{} ...
}
2)在onResume()中注册接收器并在onPause()中取消注册接收器,如下所示:
@Override
public void onResume() {
Log.e("DEBUG", "onResume()");
final IntentFilter filter = new IntentFilter();
filter.addAction("MyBroadcast");
registerReceiver(myRecvr, filter);
super.onResume();
}
@Override
public void onPause() {
Log.e("DEBUG", "OnPause()");
unregisterReceiver(myRecvr);
super.onPause();
}
3)现在在onPause()下面或者在mainActivity类结束之前添加你的内部BroadcastReceiver:
public static class MyReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Bundle extras = intent.getExtras();
if (extras != null) {
if(extras.containsKey("value")){
Log.e("BroadcastedValue",extras.get("value").toString());
}
}
}
}
4)最后,确保已将内部BroadcastReceiver路径添加到清单中,如下所示:
<receiver android:name="com.example.yourproject.YourMainActivityName$MyReceiver" android:enabled="true">
<intent-filter>
<action android:name="MyBroadcast" >
</action>
</intent-filter>
</receiver>