2016-09-26 60 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