user2022323
user2022323

Reputation: 35

Start App when Android Start

AndroidManifest.xml

<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<application
{...}
        <receiver
        android:enabled="true"
        android:exported="true"
        android:name="com.example.richard.Test.StartMyServiceAtBootReciever"
        android:permission="android.permission.RECEIVE_BOOT_COMPLETED">
        <intent-filter>
            <action android:name="android.intent.action.BOOT_COMPLETED" />
            <action android:name="android.intent.action.QUICKBOOT_POWERON" />
            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </receiver>
{...}

StartMyServiceAtBootReciever.java

    package com.example.richard.Test;

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

/**
 * Created by Richard on 23/03/2016.
 */
public class StartMyServiceAtBootReciever extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        if (intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED)) {
            Intent serviceIntent = new Intent(context, MainActivity.class);
            serviceIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            context.startService(serviceIntent);
        }
    }
}

Details: When I restart the phone, the App don't start together. Someone can help me, please?

Upvotes: 0

Views: 77

Answers (1)

Lino
Lino

Reputation: 6160

If you're going to start an Activity then use

if (intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED)) {
       Intent serviceIntent = new Intent(context, MainActivity.class);
       serviceIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
       context.startActivity(serviceIntent);
 }

Upvotes: 3

Related Questions