1

I am having a bit of an issue here, my code seems right, but its not doing what I am expecting it too.

I have an app and I would like to run when the device boots up. But when the device boots up, it application does not run, which is what I am expecting.

First in my Manifest, I added the uses-permission for RECEIVE_BOOT_COMPLETED:

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

Then also in my Manifest I added a receiver and activity inside the application:

<application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <receiver
            android:name=".BootReceiver"
            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>

        <activity
            android:name=".BootReceiver"
            android:label="@string/title_activity_boot_receiver" >
        </activity>
    </application>

And then I created a BootReceiver Activity that looks like this:

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;

public class BootReceiver extends BroadcastReceiver{

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

When I install the app, boot the device, the app does not run right when the device boots up.

I do not understand what I am doing wrong here. Is my code wrong? Anyone got any ideas?

James Suske
  • 91
  • 2
  • 13
  • What do you mean, "right when the device boots"? Do you mean it starts later? – Wildcopper Sep 19 '14 at 12:53
  • http://stackoverflow.com/questions/6391902/how-to-start-an-application-on-startup – krishnakumarp Sep 19 '14 at 12:54
  • If this was possible on an unmodified Android system, all hell would break loose. So I hope it's not. – fdreger Sep 19 '14 at 12:56
  • It's definitely possible in at least two ways, on a perfectly normal non-rooted device. The method described in the answer below, and also making your application a "launcher". – Wildcopper Sep 19 '14 at 13:06

4 Answers4

2

BootReceiver is not your Activity it is class which extends broadcast receiver.

update your manifest, this will be enough

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


<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
    <activity
        android:name=".MainActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

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


</application>
Naveed Ali
  • 2,609
  • 1
  • 22
  • 37
1

Step #1: Get rid of <activity android:name=".BootReceiver" ..., as you do not have an Activity named BootReceiver.

Step #2: Get rid of <category android:name="android.intent.category.DEFAULT" />, as categories are not usually used on broadcasts, and definitely is not used here.

Step #3: Get rid of android:permission="android.permission.RECEIVE_BOOT_COMPLETED", as that indicates that the sender of the broadcast must hold that permission, which may not be true.

You will be looking to have a manifest that looks more like:

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

    <uses-sdk
        android:minSdkVersion="7"
        android:targetSdkVersion="11"/>

    <supports-screens
        android:largeScreens="false"
        android:normalScreens="true"
        android:smallScreens="false"/>

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

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name">
        <receiver android:name=".OnBootReceiver">
            <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED"/>
            </intent-filter>
        </receiver>

        <activity
            android:name="BootstrapActivity"
            android:theme="@android:style/Theme.NoDisplay">
            <intent-filter>
                <action android:name="android.intent.action.MAIN"/>

                <category android:name="android.intent.category.LAUNCHER"/>
            </intent-filter>
        </activity>
    </application>

</manifest>

(from this sample app)

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
0

That's strange 'cause all you mentioned with Manifest and code seems good... So do you mean no MainActivity is running after Boot?

BTW I used this in my Manifest and works fine:

   <receiver
        android:name="com.example.BootMeUp"
        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>
Ciro Rizzo
  • 492
  • 4
  • 8
0

Try this: This code works for me....

<receiver
        android:name="your.package.name.BootReceiver"
        android:permission="android.permission.RECEIVE_BOOT_COMPLETED">

        <intent-filter>
            <action android:name="android.intent.action.BOOT_COMPLETED" />
        </intent-filter>

    </receiver>
raja
  • 368
  • 2
  • 13