2016-09-26 30 views
0

重啓後有接收廣播的問題嗎?Xamarin表單:Android在重啓後沒有收到廣播

我有一個類接收重新啓動後這樣的廣播。

[BroadcastReceiver(Enabled = true, Exported = true, Permission = "RECEIVE_BOOT_COMPLETED")] 
[IntentFilter(new string[] { Android.Content.Intent.ActionBootCompleted })] 
public class StartupBootReceiver : BroadcastReceiver 
{ 
    public override void OnReceive(Context context, Intent intent) 
    { 
     var startupIntent = new Intent(Application.Context, typeof(StartupService)); 
     Application.Context.StartService(startupIntent); 
    } 
} 

清單中的權限。當我用ADB命令發送引導廣播,接收器didnt」叫接收器已設定Boot_Completed

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

adb shell am broadcast -a android.intent.action.BOOT_COMPLETED com.jet.pro 

我在這裏失蹤了嗎?

+0

幾個q's ...是從'Android.App.Application.Context'的'Application.Context'?在重新啓動設備或發送廣播之前,您是否強制退出您的應用程序? – matthewrdev

+0

是的,Android.App.Application.Context,但我已經將Console.WriteLine放在這些代碼行之前,沒有打印出來。是否有另一個我應該使用。 – LittleFunny

+0

對於哪個問題是肯定的? – matthewrdev

回答

0

如果您強制停止應用程序,您的應用程序將不會再收到ActionBootCompleted意圖,直到用戶再次運行您的應用程序或設備重新啓動。

這是一種惡意軟件重新生成預防功能,允許用戶和/或反惡意軟件服務禁用和卸載應用程序,而不必追逐永無止境的流程鏈啓動。

因此,如果您正在調試並在調試器中點擊「停止」,應用程序將被終止(強制關閉)。

shell1> adb shell reboot 
shell2> adb logcat | grep FOOBAR 

~~~~~~~~~~~~~~~~~ I FOOBAR : ActionBootCompleted 

如果沒有重新啓動後手動啓動您的應用程序:

shell1> adb shell am broadcast -a android.intent.action.BOOT_COMPLETED com.sushihangover.notifysound 
shell2> adb logcat | grep FOOBAR 
Broadcasting: Intent { act=android.intent.action.BOOT_COMPLETED pkg=com.sushihangover.notifysound } 
Broadcast completed: result=0 

~~~~~~~~~~~~~~~~~ I FOOBAR : ActionBootCompleted 

假設一些像這樣的日誌輸出:

[BroadcastReceiver(Name = "com.sushihangover.notifysound.StartUpBootReceiver", Enabled = true, Exported = true, Permission = "RECEIVE_BOOT_COMPLETED")] 
[IntentFilter(new string[] { Android.Content.Intent.ActionBootCompleted })] 
public class StartupBootReceiver : BroadcastReceiver 
{ 
    public override void OnReceive(Context context, Intent intent) 
    { 
     Log.Info("FOOBAR", "ActionBootCompleted"); 
    } 
} 

更新:調試艙單審覈:

的只有我手動設置的是所需權限部分下的ReceiveBootCompleted。其餘的是根據類的屬性自動生成的:

<?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="com.sushihangover.notifysound"> 
    <!--suppress UsesMinSdkAttributes--> 
    <uses-sdk android:minSdkVersion="16" /> 
    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" /> 
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> 
    <application android:allowBackup="true" android:icon="@mipmap/icon" android:label="@string/app_name" android:name="android.app.Application" android:debuggable="true"> 
    <activity android:icon="@mipmap/icon" android:label="NotifySound" android:name="md548aa2626c31e1cf4d8bbaaddb36911dd.MainActivity"> 
     <intent-filter> 
     <action android:name="android.intent.action.MAIN" /> 
     <category android:name="android.intent.category.LAUNCHER" /> 
     </intent-filter> 
    </activity> 
    <receiver android:enabled="true" android:exported="true" android:name="com.sushihangover.notifysound.StartUpBootReceiver" android:permission="RECEIVE_BOOT_COMPLETED"> 
     <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="com.sushihangover.notifysound.mono.MonoRuntimeProvider.__mono_init__" /> 
    <!--suppress ExportedReceiver--> 
    <receiver android:name="mono.android.Seppuku"> 
     <intent-filter> 
     <action android:name="mono.android.intent.action.SEPPUKU" /> 
     <category android:name="mono.android.intent.category.SEPPUKU.com.sushihangover.notifysound" /> 
     </intent-filter> 
    </receiver> 
    </application> 
</manifest> 
+0

我試圖重新啓動設備,但接收器仍然沒有被調用。我有一個Console.WriteLine但沒有打印。 – LittleFunny

+0

@Simon檢查實際生成的清單,假設調試構建,所以它將在'~~~/obj/Debug/android/AndroidManifest.xml'下 – SushiHangover

+0

是的我在接收器部件中具有完全相同的功能,其中 LittleFunny