Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Start activity on boot

I'd like to start my app just after the phone boot. Apparently the app is started after the boot but it immediately crashes (just to be clear the app normally works fine). I have already read and tried different solutions (link1, link2) and actually the same code works fine with another app I was developing. Here's the code:

AndroidManifest.xml:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.bluetoothx10y"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="6"
        android:targetSdkVersion="15" />

    <uses-feature android:name="android.hardware.usb.accessory"/>
    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
    <uses-permission android:name="android.permission.WRITE_INTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.BLUETOOTH"/>
    <uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>
    <uses-permission android:name="android.permission.DISABLE_KEYGUARD"/>

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >

        <receiver
            android:name=".StartMyActivityAtBootReceiver"
            android:enabled="true"
            android:exported="true">
            <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED" />
            </intent-filter>
        </receiver>


        <activity
            android:name=".MainActivity"
            android:label="@string/app_name"
            android:screenOrientation="landscape" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>

            <intent-filter>
               <action android:name="android.hardware.usb.action.USB_ACCESSORY_ATTACHED"/>
            </intent-filter>

            <meta-data 
                android:name="android.hardware.usb.action.USB_ACCESSORY_ATTACHED"
                android:resource="@xml/accessory_filter">
            </meta-data>

        </activity>

        <activity android:name=".DeviceListActivity"
              android:label="@string/app_name"
              android:theme="@android:style/Theme.Dialog"
              android:screenOrientation="landscape" />        
    </application>

</manifest>

StartMyActivityAtBootReceiver.java:

    public class StartMyActivityAtBootReceiver extends BroadcastReceiver {    
    @Override
    public void onReceive(Context context, Intent intent) {

        if ("android.intent.action.BOOT_COMPLETED".equals(intent.getAction())) {    

                Intent myStarterIntent = new Intent(context, MainActivity.class);
                myStarterIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                context.startActivity(myStarterIntent);

            }    
    }   
}

Could it be related the fact that I'm using the a lot of user permissions?

like image 454
STiLLeN Avatar asked Jul 12 '13 08:07

STiLLeN


People also ask

What is boot receiver in mobile phone?

Boot receiver is just broadcast receiver which responds to intent with action android.

How we can start activity and service in Android?

Start a service. An Android component (service, receiver, activity) can trigger the execution of a service via the startService(intent) method. // use this to start and trigger a service Intent i= new Intent(context, MyService. class); // potentially add data to the intent i.

Which method is use for start activity in Android?

To start an activity, use the method startActivity(intent) . This method is defined on the Context object which Activity extends. The following code demonstrates how you can start another activity via an intent. # Start the activity connect to the # specified class Intent i = new Intent(this, ActivityTwo.

What is restrict to launch in android?

Android 10 (API level 29) and higher place restrictions on when apps can start activities when the app is running in the background. These restrictions help minimize interruptions for the user and keep the user more in control of what's shown on their screen.


1 Answers

Try this:

1] In AndroidManifest.xml file:

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

 <application 
 ...
    <receiver
        android:name=".StartMyActivityAtBootReceiver"
        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>
 </application>

2] Inside BroadcastReciever class with StartMyActivityAtBootReceiver as class name.

@Override
public void onReceive(Context context, Intent intent) {

    Intent i = new Intent(context, MainActivity.class);  
    i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    context.startActivity(i); 

} 

This worked for me. The difference in code is as follows:

  • android:permission="android.permission.RECEIVE_BOOT_COMPLETED" inside receiver.
  • included "category android:name="android.intent.category.DEFAULT" " inside intent filter.
  • I am not checking the intent in onRecieve, as i know that code will be executed only if its true
like image 80
SKK Avatar answered Oct 21 '22 15:10

SKK