1

I've made my first android service, I want that service to be launched at boot but doesn't work

Here is the service class

 package service.com.serviceboot;

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


public class ServiceBoot extends Service {
    @Override
    public IBinder onBind(Intent intent) {
    return null;
}

@Override
public void onCreate() {
    super.onCreate();

    Toast.makeText(this, "Servicio creado", Toast.LENGTH_LONG).show();
    Log.d("SERVICEBOOT", "Servicio creado");
}

@Override
public void onDestroy(){
    super.onDestroy();

    Toast.makeText(this, "Servicio destruido", Toast.LENGTH_LONG).show();
    Log.d("SERVICEBOOT", "Servicio destruido");
}
}

This is the broadcast receiver

package service.com.serviceboot;

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


 public class ReceiverBoot extends BroadcastReceiver {
  @Override
     public void onReceive(Context context, Intent intent) {

     Intent serviceIntent = new Intent(context, ServiceBoot.class);
     context.startService(serviceIntent);
 }
}

and the manifest

<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>

<application android:allowBackup="true" android:label="@string/app_name"
    android:icon="@mipmap/ic_launcher" android:theme="@style/AppTheme">

    <service
        android:name=".ServiceBoot">
     </service>


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

I rebbot the phone but the service is not lanched

AFS
  • 1,433
  • 6
  • 28
  • 52

2 Answers2

1

Try giving the service & receiver class name along with package name inside manifest.Everything else looks perfect.it might be the issue.

<application android:allowBackup="true" 
    android:label="@string/app_name"
    android:icon="@mipmap/ic_launcher" 
    android:theme="@style/AppTheme">

<service
    android:name="service.com.serviceboot.ServiceBoot">
 </service>


<receiver android:name="service.com.serviceboot.ReceiverBoot" >
    <intent-filter>
        <action android:name="android.intent.action.BOOT_COMPLETED" />
    </intent-filter>
</receiver>
</application>
Nirav Tukadiya
  • 3,367
  • 1
  • 18
  • 36
0

What device are you using for tests? if it's htc than add in your receiver

<action android:name="android.intent.action.QUICKBOOT_POWERON" />
Etun
  • 403
  • 3
  • 8