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
}