0

I have Activity

public class I1 extends Activity {

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

}

and BroadcastReceiver

public class I2 extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        Intent mIntent = new Intent(context, I1.class);
        mIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(mIntent);
    }

}

Why don't my activity starts at boot completed? That's code of AndroidManifest.xml

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

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

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

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@android:style/Theme.Holo.NoActionBar.Fullscreen" >

        <activity
            android:name=".I1"
            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=".I2"
            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>

</manifest>

and code of layout

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:keepScreenOn="true"
    android:background="#000000"
    tools:context="com.mypackage.Main" />

Can you help me please, what's wrong with my code?

nick
  • 197
  • 1
  • 2
  • 16

2 Answers2

1

The problem was really unbeliveable. I have two identical phones - Philips Android 4.0.3. One of them, on which I tested my app had bug with BOOT_COMPLETED action. This code is the best I think

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

and Broadcast Receiver

@Override
public void onReceive(Context context, Intent intent) {
    Intent mIntent = new Intent(context, I1.class);
    mIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    context.startActivity(mIntent);
}

and it also works!

Thanks to user @AxelH!


Addition. On some phones (for example, ZTE Blade HN) you need to start the main activity once, because phone don't let Broadcast Receiver to be activated if app is new and has never been opened. Even after updating app you need to do it.

nick
  • 197
  • 1
  • 2
  • 16
0

Try the following

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

Another thing to notice

If your app installed on external storage(SD card), you will never receive Boot Complete action. So you have to specify the following in the manifest tag.

android:installLocation="internalOnly"
Nitesh
  • 3,868
  • 1
  • 20
  • 26
  • Sorry, it still doesn't work. Nothing happened. I removed SD card and reinstalled app on phone memory also. – nick Nov 04 '16 at 09:19
  • 1
    I would only disagree with the `never receive`. This works (well it works when I did the test) but there is a change that it doesn't if the intent is send before the card is mount. – AxelH Nov 04 '16 at 09:19