1

Have seen a lot of ex and try ,but useless,here my code

AndroidManifest

    <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
          package="tao800.tuan800.com.teststart"
          android:installLocation="internalOnly">
     <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
    <application
        android:name=".MyApplication"
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name"
            android:theme="@style/AppTheme.NoActionBar">
            <intent-filter>
                <action android:name="android.intent.action.MAIN"/>
                <category android:name="android.intent.category.LAUNCHER"/>
            </intent-filter>
        </activity>
        <receiver
            android:name=".receiver.BootBroadcastReceiver"
            android:enabled="true">
            <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED"/>
                <action android:name="android.intent.action.BOOT_COMPLETED123"/>
<category android:name="android.intent.category.DEFAULT"/>
            </intent-filter>
        </receiver>
        <activity android:name=".Main2Activity">
        </activity>
    </application>
</manifest>

BootBroadcastReceiver

public class BootBroadcastReceiver extends BroadcastReceiver{
    @Override
    public void onReceive(Context context, Intent intent) {
       Log.d("myservice","get boot complete action");
       Intent intent2 = new Intent(context,Main2Activity.class);
       intent2.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
       context.startActivity(intent2);
    }
}

can anybody tell me how to fix this ?

Ps:It works correctly on genymotion, but not work on my android phone.

Lonie
  • 275
  • 2
  • 12
  • Are the android version and Api versions same on your genymotion and Phone? – Kunal Yadav Aug 24 '16 at 07:42
  • 1
    Is the `` outside of the `` tags? Is the `` inside them? Did you run your `Activity` at least once after installation, before rebooting? – Mike M. Aug 24 '16 at 07:45
  • I surmise, this is a faulty piece of code - `` – CodeWalker Aug 24 '16 at 07:54
  • Moreover, it takes a lump sum of 5 minutes for a `BOOT` to `COMPLETE`. – CodeWalker Aug 24 '16 at 07:57
  • `` this is not system broadcast , it's mine – Lonie Aug 24 '16 at 08:02
  • Did you run `MainActivity` at least once after installation, before rebooting? Your title says "service". Is `Main2Activity` an `Activity`, or a `Service`? Does your device have any extra settings/permissions that disallow apps running in the background by default? – Mike M. Aug 24 '16 at 08:05
  • @KunalYadav api different but both above 3.1 ,4.4.4 & 5.0 – Lonie Aug 24 '16 at 08:07
  • @MikeM. have run , firstly i want run a service ,but i find it did't work and i think ther r same , if it can receive broadcast ,it can start an activity or service , but it didn't, after reboot, i can't see the log – Lonie Aug 24 '16 at 08:10
  • these code work on genymotion , but not work when run on my phone... – Lonie Aug 24 '16 at 08:13
  • I get the reason, because my phone has a list that application can start after boot~ , my app is not in ~ now I put it in , it work , – Lonie Aug 24 '16 at 08:58
  • which phone was this? @Lonie – Carlos.V Feb 07 '19 at 15:49

1 Answers1

0

make sure your app is in the list that has permission to start itself after boot

Lonie
  • 275
  • 2
  • 12