0

I've read some of the threads here explaining about launching an app after startup such as this one but it didn't work for me.

Here is MainActivity.java

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
}

AfterBootReceiver.java:

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


public class AfterBootReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        if (intent.getAction().equals("Intent.ACTION_BOOT_COMPLETED")) {
            Toast.makeText(context, "AfterBootReceiver - boot", Toast.LENGTH_SHORT).show();
        }
    }
}

Here is the manifest with the permission code:

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

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

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

        <receiver android:name=".AfterBootReceiver">
            <intent-filter>
                <action android:name= "android.intent.action.BOOT_COMPLETED" />
            </intent-filter>
        </receiver>
    </application>
</manifest>

My questions is: does any of you see any problem with this code? how to make the receiver receive the boot action from within the main activity?

Thanks!

Community
  • 1
  • 1
Lior L
  • 49
  • 1
  • 1
  • 7
  • 1
    Possible duplicate of [How to start an Application on startup?](http://stackoverflow.com/questions/6391902/how-to-start-an-application-on-startup) – Onik Oct 25 '16 at 08:21
  • No, I wrote above the the thread you mentioned didn't solve my problem – Lior L Oct 25 '16 at 08:30

2 Answers2

1

You will need to register the receiver outside of your main Activity (in your Manifest), Android will not know that your BroadcastReceiver exists until your Activity is started. You will also need to create your own BroadcastReceiver implementation for this purpose.

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

You may start yourt main Activity from inside your newly created BroadcastReceiver (BootCompletedReceiver).

Your application will also need to request the RECEIVE_BOOT_COMPLETED permission.

<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
Jonas Köritz
  • 2,606
  • 21
  • 33
0

Your launcher is not listening for the BOOT_COMPLETED broadcast.

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

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

In your apps current state - the main activity needs to be running to listen to the BOOT_COMPLETED broadcast. Your manifest needs to have something registered that tells the ecosystem to activate the activity / receiver.

Quintin Balsdon
  • 5,484
  • 10
  • 54
  • 95
  • Thank you, but I changed according to your suggestion, uploaded the apk, rebooted my device and nothing happened :-( – Lior L Oct 25 '16 at 08:52
  • Sorry about that - I'll see if I can review some code later and get something running – Quintin Balsdon Oct 25 '16 at 09:15
  • I have tried looking into my code, but that code was signed with the system signature, giving the app a higher security level. I have been looking here http://stackoverflow.com/questions/37272823/android-permission-receive-boot-completed-does-not-work and http://stackoverflow.com/questions/2784441/trying-to-start-a-service-on-boot-on-android - I will keep on looking for you as well – Quintin Balsdon Oct 27 '16 at 07:49