0

I'm trying to start my background service when the device boots, but nothing is happening. This is my code:

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="it.test"
    android:permission="android.permission.RECEIVE_BOOT_COMPLETED">

    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <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="true"
        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>
    </application>
</manifest>

Autostart.java

package it.test;

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

public class Autostart extends BroadcastReceiver {
    @Override
    public void onReceive(Context arg0, Intent arg1) {
        Intent intent = new Intent(arg0, AppService.class);
        arg0.startService(intent);
        Log.e("it.Test", "***** SERVER STARTING CALLED *****");

    }
}

AppService.java

package it.test;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;
import android.widget.Toast;

public class AppService extends Service {

    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public void onDestroy() {
        Toast.makeText(this, "My AppService Stopped", Toast.LENGTH_LONG).show();
        Log.e("it.Test", "***** SERVICE STOPPED *****");
    }

    @Override
    public void onStart(Intent intent, int startid) {
        Toast.makeText(this, "My AppService Started", Toast.LENGTH_LONG).show();
        Log.e("it.Test", "***** SERVICE STARTED *****");
        /*
        MainActivity.calc();
        MainActivity.save();
        */
    }
}

When my phone boots I don't see any toast (I don't see either the "My AppService Started" nor the "My AppService Stopped" toast) and nothing gets logged by the application... Anyone knows what I am doing wrong?? Thanks!

EDIT: Solution to the problem After seeing the post Trying to start a service on boot on Android linked in an answer and after many debugging I found the solution to my problem :) Thank to everyone!

Modification to AndroidManifest.xml

Added inside <application>:

<receiver android:name="it.test.Autostart">
    <intent-filter>
        <action android:name="android.intent.action.BOOT_COMPLETED" />
    </intent-filter>
</receiver>

<service
    android:name="it.test.AppService"
    android:enabled="true" />

Added also:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    [...]
    android:installLocation="internalOnly">

Modifications to AppService.java To make the service work, I had to override in the AppService class onStartCommand instead of onStart, so I completely removed the method onStart from my class. New onStartCommand:

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    Toast.makeText(this, "My AppService Started", Toast.LENGTH_LONG).show();
    Log.e("it.Test", "***** SERVICE STARTED ***** from onStartCommand");
    MainActivity.calc(this);
    MainActivity.save();
    return Service.START_REDELIVER_INTENT;//todo I'm not sure about what I have to return
}
Community
  • 1
  • 1
adario
  • 138
  • 1
  • 7

5 Answers5

2

Your broadcast receiver is not registered at AndroidManifest.xml

You need to register the broadcast receiver:

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

If your app is installed to external storage it won't receive BOOT_COMPLETE broadcast message. To prevent this you can install your application in internal storage. you can do this just adding this line in AndroidManifest.xml

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
android:installLocation="internalOnly"
... >

Source: https://stackoverflow.com/a/32938071/1549700

Community
  • 1
  • 1
Machado
  • 14,105
  • 13
  • 56
  • 97
  • The tag `` must be added, or the Sevice won't be called. You also must override `Service#onStartCommand` and not `Service#onStart` (witch is deprecated) – adario Aug 11 '16 at 21:24
1

You have you register your broadcast receiver and service under the <application> tag

 <!-- [START service_listener] -->
    <service
        android:name=".it.test.AppService"
        android:enabled="true" />
    <!-- [END service_listener] -->

    <!-- [START broadcast_receiver] -->
    <receiver android:name=".it.test.Autostart">
        <intent-filter>
            <action android:name="android.intent.action.BOOT_COMPLETED" />
        </intent-filter>
    </receiver>
    <!-- [END broadcast_receiver] -->
adario
  • 138
  • 1
  • 7
CodeDaily
  • 756
  • 4
  • 17
  • Adding the `` tag and the `` tags helped, but it isn't enought: `Service#onStartCommand` and not `Service#onStart` (witch is deprecated) must be overridden – adario Aug 11 '16 at 21:22
0

You need to register the broadcast receiver:

<receiver android:name=".Autostart">
        <intent-filter>
            <action android:name="android.intent.action.BOOT_COMPLETED" />
        </intent-filter>
</receiver>
fernandospr
  • 2,976
  • 2
  • 22
  • 43
0

You need this in your manifest file:

<receiver android:name=".it.test.Autostart">
    <intent-filter>
        <action android:name="android.intent.action.BOOT_COMPLETED"/>
    </intent-filter>
</receiver>
Denny Weinberg
  • 2,492
  • 1
  • 21
  • 34
0

Have you register broadcast receiver in manifest file and also also give boot completed permission in your broadcast receiver, if any problem then ask in comment, your code is perfect.

Yogesh Rathi
  • 6,331
  • 4
  • 51
  • 81