0

I'm attempting to create an app, that will start when I turn on my phone and will always run on the background (like whatsapp for example, it listens to the server and when a message comes in you get notify). I'm not sure whereas it is called a service or process but I wish that my app will run indefinitely. I began by creating an activity and a class as described: How to start an Application on startup?

The problem is that the the manifest file declares the activity as the launcher and so the class won't run.

<?xml version="1.0" encoding="utf-8"?>

<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:enabled="true" android:exported="true"
        android:name="StartMyServiceAtBootReceiver"
        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>
   <activity
        android:name=".MainActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

public class StartMyServiceAtBootReceiver extends BroadcastReceiver {
static String  myDate="";
@Override
public void onReceive(Context context, Intent intent) {
    if (Intent.ACTION_BOOT_COMPLETED.equals(intent.getAction())) {
        Intent serviceIntent = new Intent(context, MainActivity.class);
        context.startService(serviceIntent);
    }
}

}

and i'm not sure what my problems is and how to proceed from here. Thanks.

Community
  • 1
  • 1
Itai.S.
  • 144
  • 13

1 Answers1

0

You need to start background Service from your StartMyServiceAtBootReceiver on phone boot, not Activity, for this you create separate class inheriting from Service class.

Max77
  • 1,466
  • 13
  • 19