Forum Xamarin.Android

Open an app on startup/after booting - Not Working

Hi Xamarin Teams,

I have faced an issue, that I need to open my app as my device boots up. I have implement a way by this link.

This code in my AndroidManifest.xml file

<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
    <receiver android:enabled="true"
        android:exported="true" 
        android-permission="android.permission.RECEIVE_BOOT_COMPLETED"
        android:name="com.mycompany.test.BootComplete" >

        <intent-filter >
            <action android:name="android.intent.action.SCREEN_ON" />
            <action android:name="android.intent.action.BOOT_COMPLETED" />
            <action android:name="android.intent.action.QUICKBOOT_POWERON" />
            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </receiver>

Then I create a class BootComplete

public class BootComplete : BroadcastReceiver
    {
        #region implemented abstract members of BroadcastReceiver
        public override void OnReceive(Context context, Intent intent)
        {
            if (intent.Action.Equals(Intent.ActionBootCompleted))
            {
                var serviceIntent = new Intent(context, typeof(MainActivity));
                context.StartService(serviceIntent);
            }
        }

        #endregion
    }

Best Answer

Answers

  • ChristopheBERNARDChristopheBERNARD BEMember ✭✭✭✭✭

    Hello @MohammedYoonis.4050

    are you sure about your manifest file ?
    i 'm not an expert with mobile but i think the name of your receiver would be rather than

    <receiver android:name=".BootComplete"/>

    If not work try to make perhpas just a little sample who detect when the device is charging or not make sure your broadcast is perfect and after try the action boot completed ...

    i've look your code and for me normally all will be work ...

    have a nice day

  • Thank you @ChristopheBERNARD ,

    I have changed receiver name as per your suggestion. And also changed my BootComplete class with below code

    if (intent.Action.Equals(Intent.ActionBootCompleted))
    {
        Toast.MakeText(context, "Action Boot Completed!", ToastLength.Long).Show();
        var serviceIntent = new Intent(context, typeof(MainActivity));
        serviceIntent.SetFlags(ActivityFlags.ClearTop);
        context.StartActivity(serviceIntent);
    }
    

    Now the Toast message working properly. Then it showed a message 'Unfortunately, MyApp has stopped'.

    Please give me a solution.

  • ChristopheBERNARDChristopheBERNARD BEMember ✭✭✭✭✭

    @MohammedYoonis try this

    `
    [BroadcastReceiver]
    [IntentFilter(new[] { Intent.ActionBootCompleted })]
    public class BootComplete : BroadcastReceiver
    {
    public override void OnReceive(Context context, Intent intent)
    {
    Intent serviceStart = new Intent(context, typeof(YOUR ACTIVITY));
    serviceStart.AddFlags (ActivityFlags.NewTask);
    context.StartActivity(serviceStart);
    }
    }

    `

  • @ChristopheBERNARD,
    Thanks a lot.
    Its working fine.

  • ChristopheBERNARDChristopheBERNARD BEMember ✭✭✭✭✭

    @MohammedYoonis

    is it possible to publish all the code here as this if someone have the same problem he can find your code ..

    have a nice day

  • sreccesrecce ARMember ✭✭

    First thank you for your help. I'm using this code and it's working like a charm. It is a gps app based on the background service provided by xamarin.

    Just one thing. Is possible to start the app in the background after boot?

    I mean, after the boot complete my app comes to the screen.

    Thank you !

  • jackrajjackraj USMember ✭✭
    edited January 2018

    Hi all,
    I have created a sample application for Open an app on startup/after booting My application showed a message 'Unfortunately, MyApp has stopped'. after that, the Toast message "Received intent!" properly.I have pasted my code below

    MainActivity .cs

    [Activity(Label = "RebootApplication", MainLauncher = true, Icon = "@drawable/icon")]
    public class MainActivity : Activity
    {
    protected override void OnCreate(Bundle bundle)
    {
    base.OnCreate(bundle);
    SetContentView(Resource.Layout.Main);

        }
        protected override void OnPause()
        {
            base.OnPause();
        }
        protected override void OnStart()
        {
            base.OnStart();           
        }
        protected override void OnStop()
        {
            base.OnStop();
        }
    }
    

    BootReceiver.cs

    [BroadcastReceiver(Enabled =true)]   
    [IntentFilter(new[] { Intent.ActionBootCompleted })]
    public class BootReceiver : BroadcastReceiver
    {
        #region implemented abstract members of BroadcastReceiver
        public override void OnReceive(Context context, Intent intent)
        {
            try
            {                
                    if (intent.Action.Equals(Intent.ActionBootCompleted))
                    {
                        Toast.MakeText(context, "Received intent!", ToastLength.Long).Show();                        
                        Intent i = new Intent(context, typeof(MainActivity));                       
                        i.AddFlags(ActivityFlags.NewTask);
                        context.StartActivity(i);
                    }             
    
            }
            catch(Exception ex)
            {                
                throw ex;
            }
        }
        #endregion 
    

    And also android manifest.xml

    <uses-sdk android:targetSdkVersion="22" />
    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <application android:label="RebootApplication" android:icon="@drawable/Icon">
    <activity
       android:name="rebootApplication.MainActivity"
       android:label="@string/ApplicationName">
      <intent-filter>
        <action android:name="android.intent.action.BOOT_COMPLETED" />
      </intent-filter>
    </activity>
    <receiver android:name="rebootApplication.BootReceiver" android:enabled="true" android:exported="true" android:permission="android.permission.RECEIVE_BOOT_COMPLETED">
      <intent-filter>
        <action android:name="android.intent.action.BOOT_COMPLETED" />
        <action android:name="android.intent.action.QUICKBOOT_POWERON" />
        <category android:name="android.intent.category.DEFAULT" />
      </intent-filter>
    


    while reboot devices after that My app through error are 'Unfortunately, MyApp has stopped'. after that, the Toast message "Received intent!" then application launch perfectly.so please share your idea or piece of code.

  • GeorgiZGeorgiZ BGMember ✭✭

    On some phones declaring the permission within the receiver will make the receiver not working. I faced exactly the same problem. I declared the android.permission.RECEIVE_BOOT_COMPLETED permission at 2 places. First in the AndroidManifest.xml and second as attribute to my receiver:

    [BroadcastReceiver(Permission = "android.permission.RECEIVE_BOOT_COMPLETED", DirectBootAware = true, Enabled = true, Exported = true)]
    [IntentFilter(new[] { Intent.ActionBootCompleted, "android.intent.action.QUICKBOOT_POWERON", Intent.ActionScreenOn })]
    public class MyBootCompletedReceiver : BroadcastReceiver
    {
        public override void OnReceive(Context context, Intent intent)
        {
                // bla bla...
        }
    }
    

    I have 2 different phones - one LG with android 7.1 (API 25) and one LG running 6.0 (API 23). On the first one the receiver worked fine, but on the second one it didn't. After I checked the device log I saw a warning describing a permission issue with my broadcast receiver:

    Warning 1741 BroadcastQueue Appop Denial: broadcasting Intent { act=android.intent.action.BOOT_COMPLETED flg=0x8000010 (has extras) } from null (pid=1741, uid=1000) requires appop android:boot_complete due to registered receiver

    After I deleted the Permission = "android.permission.RECEIVE_BOOT_COMPLETED" attribute from the receiver it worked on both devices. A link to the same problem: https://stackoverflow.com/questions/46156770/w-broadcastqueue-appop-denial-broadcasting-intent-act-android-intent-action?rq=1

Sign In or Register to comment.