0

I am trying to launch my application when I turn my android device on. Currently I have a BroadcastReciever class and a Service class however the app does not seem to launch when I reboot my device.

My BroadCast Receiver Class

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

        if (intent.getAction().equalsIgnoreCase(Intent.ACTION_BOOT_COMPLETED)) {
        Toast.makeText(context, "Reboot completed! Starting your app!!!.", Toast.LENGTH_LONG).show();

            Intent i = new Intent(context, AutoStart.class);
            i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            context.startService(i);
        }

    }
}

My Service class

public class AutoStart extends Service {

    @Override
    public void onCreate() {
        super.onCreate();
        Toast.makeText(this, "Service Started", Toast.LENGTH_LONG).show();
    }

    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }
}

my manifest

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.test"
android:sharedUserId="android.uid.system">

<uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="19" />

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

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

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

    <service android:name=".AutoStart">
        <intent-filter>
            <action android:name="com.example.test.AutoStart"></action>
        </intent-filter>
    </service>

    <activity
        android:name=".MyActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity
        android:name=".Result"
        android:label="@string/title_activity_result"
        android:parentActivityName=".MyActivity" >
        <meta-data
            android:name="android.support.PARENT_ACTIVITY"
            android:value="com.example.test.MyActivity" />
    </activity>

    <service
        android:name=".functionality"
        android:enabled="true" />
</application>

</manifest>

Could someone point me in the right direction. I am not sure what I am doing wrong.

user3404290
  • 45
  • 1
  • 2
  • 10

4 Answers4

2

Your application needs to be manually run at least once before boot receiver start working, also boot intent might be send after quite some time and maybe even intercepted and killed by other apps. I encountered such behaviour with few third party sms apps.

Adam Fręśko
  • 1,064
  • 9
  • 14
0

I'm not entirely sure about this, but try removing the android:exported="false" from the BootBroadcastReceiver. android:exported="false" means no other app can call it, and the system is just as an app in here.

F43nd1r
  • 7,690
  • 3
  • 24
  • 62
  • hmm so I got rid of the Service class and in my receiver class I changed startService to startActivity(context, MainActivity.class); and this works. But there is a 30 second delay before this application is launched. Any idea why? – user3404290 Aug 05 '15 at 22:10
  • 1
    This might be a security policy in Android, so "bad" apps can't block the device immediately. However you shouldn't start an activity on boot anyway. – F43nd1r Aug 05 '15 at 22:16
0

Try using context.startActivity(i); instead of context.startService(i);.

Saurabh
  • 457
  • 2
  • 8
  • 26
0

You should correct this line in your BroadCast Receiver Class: Use: context.startActivity(i); Instead of: context.startService(i);

M.Najmi
  • 3
  • 3