-2

My AndroidManifest, Broadcastreceiver and Service classes are shown below.

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)));
        }

    }

When the device is restarted, I want the processes inside the services class to be executed. But it gives an error.Why ? When I run the application it works fine, but I want it to be executed automatically when the device is restarted. Probably there is an error on the receiver but I cant find where.

Jon Goodwin
  • 9,053
  • 5
  • 35
  • 54
oflu1071
  • 5
  • 4

1 Answers1

0

After Android 3.1 "the system adds FLAG_EXCLUDE_STOPPED_PACKAGES to all broadcast intents." This means you, after 3.1, all apps are stopped on boot. Why ? For security reasons.

There are RULES to turn the flag off FLAG_EXCLUDE_STOPPED_PACKAGES .

(1) Your app needs to needs in phone storage, NOT sdcard otherwise the flag set. BOOT_COMPLETE is sent before external storage is mounted. if the app is installed to external storage it won't receive BOOT_COMPLETE broadcast message.

(2) If the user presses "Force close" from settings OR "unresponsive app" button, the flag is set.

(3) If the application has never been run, the flag is set (never is relative to current boot state ;O) NEVER means in THIS boot OR you invalidated the flag in the last boot state).

Your Receiver will run on boot up (flag not set), if you follow the rules.

hackjutsu
  • 8,336
  • 13
  • 47
  • 87
Jon Goodwin
  • 9,053
  • 5
  • 35
  • 54
  • Can I force the application to be saved in the phone memory when the application is installed? If this is possible, how is this done? – oflu1071 Nov 25 '16 at 10:45
  • I don't understand "saved in the phone memory", if it's in the memory (lets call it RAM, even if it is not), then it is not "permanently saved" i.e. not in storage. To be clear the Android OS keeps track of the FLAGS, not the app. – Jon Goodwin Nov 25 '16 at 12:34
  • Can I solve this problem like this? To write this code in AndroidManifest.xml. – oflu1071 Nov 25 '16 at 16:47