-2

我的AndroidManifest,Broadcastreceiver和服務類別如下所示。設備啓動時我的BroadcastReceiver不工作

DetectBootUp.cs:

[BroadcastReceiver] 
    [IntentFilter(new[] { Intent.ActionBootCompleted })] 
    public class DetectBootUp : BroadcastReceiver 
    { 
     public override void OnReceive(Context context, Intent intent) 
     { 
      Intent bootUp = new Intent(context, typeof(AndroidService)); 
      context.StartService(bootUp); 
     } 
    } 

AndroidService.cs

[Service] 
    public class AndroidService : Service 
    { 
     public override void OnCreate() 
     { 
      Toast.MakeText(this, "Service Created", ToastLength.Long).Show(); 
      Log.Debug("BroadCastReceiverBoot", "OnCreate"); 
     } 
     public override IBinder OnBind(Intent intent) 
     { 
      return null; 
     } 
     public override void OnDestroy() 
     { 
      Toast.MakeText(this, "Service Destroyed", ToastLength.Long).Show(); 
      Log.Debug("BroadCastReceiverBoot", "onDestroy"); 
      ApplicationContext.StartService(new Intent(ApplicationContext, typeof(AndroidService))); 
     } 
     [return: GeneratedEnum] 
     public override StartCommandResult OnStartCommand(Intent intent, [GeneratedEnum] StartCommandFlags flags, int startId) 
     { 
      Toast.MakeText(this, "Service Started", ToastLength.Long).Show(); 
      Log.Debug("BroadCastReceiverBoot", "OnStart"); 
      return StartCommandResult.Sticky; 
     } 
    } 

的AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android"> 
    <uses-sdk android:minSdkVersion="15" /> 

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

    <application android:label="NotificationExample"> 
    <receiver 
     android:name=".DetectBootUp" 
     android:enabled="true" 
     android:permission="android.permission.RECEIVE_BOOT_COMPLETED"> 
     <intent-filter> 
     <action android:name="android.intent.action.BOOT_COMPLETED"/> 
     <category android:name="android.intent.category.DEFAULT"/> 
     </intent-filter> 
    </receiver> 

    <service android:name=".AndroidService" 
      android:enabled="true" 
      android:exported="false"> 
    </service> 
    </application> 

</manifest> 

MainActivity.cs

public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsApplicationActivity 
    { 
     protected override void OnCreate(Bundle bundle) 
     { 
      base.OnCreate(bundle); 

      global::Xamarin.Forms.Forms.Init(this, bundle); 
      LoadApplication(new App()); 
      StartService(new Android.Content.Intent(Application.Context, typeof(AndroidService))); 
     } 

    } 

當設備重新啓動時,我希望執行services類中的進程。但它給了一個錯誤。爲什麼? 當我運行該應用程序時,它工作正常,但我希望它在設備重新啓動時自動執行。可能在接收器上有錯誤,但我無法找到位置。

+0

什麼是錯誤?你有堆棧跟蹤嗎? – Demitrian

+0

不要堆棧跟蹤。我不知道stacktrace :) – oflu1071

回答

0

在Android 3.1之後「系統將FLAG_EXCLUDE_STOPPED_PACKAGES添加到所有廣播意圖。」這意味着,在3.1之後,所有應用程序在啓動時都會停止。爲什麼?出於安全原因。

有規則可以關閉標誌FLAG_EXCLUDE_STOPPED_PACKAGES。 (1)您的應用需要在電話存儲中使用,而不是sdcard,否則設置標誌。在安裝外部存儲之前發送BOOT_COMPLETE。如果應用程序安裝到外部存儲,它將不會收到BOOT_COMPLETE廣播消息。 (2)如果用戶從設置或「無響應應用程序」按鈕按下「強制關閉」,則標誌被設置。 (3)如果應用程序從未運行過,則標誌被設置(從不相對於當前的啓動狀態; O)從不意味着在本次啓動或者你在最後一次啓動狀態時使標誌無效)。

如果遵循規則,Receiver將在啓動時運行(標誌未設置)。

+0

安裝應用程序時,我可以強制將應用程序保存在手機內存中嗎?如果這是可能的,這是如何完成的? – oflu1071

+0

我不明白「保存在手機內存中」,如果它在內存中(讓我們稱它爲RAM,即使不是),那麼它就不會「永久保存」,即不在存儲器中。要清楚的是Android操作系統會跟蹤FLAGS,而不是應用程序。 –

+0

我可以這樣解決這個問題嗎? 在AndroidManifest.xml中編寫此代碼。 – oflu1071