我創建了一個簡單的示例來測試BroadcastReceiver
對動作BOOT_COMPLETED
的反應。下面,但它不起作用。啓動平板電腦後,沒有活動/應用程序正在運行,並且在標誌中沒有任何內容。我可能是在設置一個錯誤,但我不能找出Xamarin項目中使用BOOT_COMPLETED操作的BroadcastReceiver的正確設置是什麼?
我用什麼平板阿爾卑斯山874v3的是Android 4.4.2和Visual Studio 2010與Xamarin編寫Android應用程序在.NET
在SO我發現了一些附加信息: 1註冊BroadcastReceiver
不在AndroidManifest.xml中,但必須使用類屬性。 2應用程序必須包含BroadcastReceiver
和activity
否則將無法在更高版本的Android上運行(爲了安全) 3一旦安裝應用程序處於停止狀態,所以我啓動它(系統驗證用戶想要應用程序),然後殺死並然後我嘗試重新啓動。
[BroadcastReceiver(Enabled = true, Exported = true, Permission = "RECEIVE_BOOT_COMPLETED")]
[IntentFilter(new string[] { "android.intent.action.BOOT_COMPLETED"})]
public class BootBroadcastReceiver : BroadcastReceiver
{
public BootBroadcastReceiver()
{
}
public override void OnReceive(Context context, Intent intent)
{
Log.Debug("TestBoot", "BootBroadcastReceiver.OnReceive()");
context.StartActivity(typeof(UsbMainActivity));
Log.Debug("TestBoot", "BootBroadcastReceiver.OnReceive() after start activity");
}
}
[Activity(Label = "UsbMainActivity", Icon = "@drawable/icon", MainLauncher = true, Permission = "RECEIVE_BOOT_COMPLETED")]
[IntentFilter(new string[] { "android.intent.action.BOOT_COMPLETED" })]
public class UsbMainActivity : Activity
{
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
SetContentView(Resource.Layout.Main);
Utils.MyLog("TestBoot", 1, "UsbMainActivity.OnCreate()");
}
}
有這是由xamarin產生AndroidMainfest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="TestBoot.TestBoot" android:versionCode="1" android:versionName="1.0">
<uses-sdk android:minSdkVersion="15" android:targetSdkVersion="19" />
<application android:label="TestBoot" android:icon="@drawable/icon" android:name="mono.android.app.Application" android:debuggable="true">
<activity android:icon="@drawable/icon" android:label="UsbMainActivity" android:permission="RECEIVE_BOOT_COMPLETED" android:name="md5e98891b9b152ca725e5cab653b1387f3.UsbMainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</activity>
<receiver android:enabled="true" android:exported="true" android:permission="RECEIVE_BOOT_COMPLETED" android:name="md5e98891b9b152ca725e5cab653b1387f3.BootBroadcastReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
<provider android:name="mono.MonoRuntimeProvider" android:exported="false" android:initOrder="2147483647" android:authorities="TestBoot.TestBoot.mono.MonoRuntimeProvider.__mono_init__" />
<receiver android:name="mono.android.Seppuku">
<intent-filter>
<action android:name="mono.android.intent.action.SEPPUKU" />
<category android:name="mono.android.intent.category.SEPPUKU.TestBoot.TestBoot" />
</intent-filter>
</receiver>
</application>
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
</manifest>
即使在設備重新啓動後,這也可以讓用戶真正殺死一個應用。感謝adb測試命令! – mvandillen