Marco Rocchi
Marco Rocchi

Reputation: 150

Autostart of a service when phone is powered on

I explain what I want my app does:

The user powers on the phone, my service starts and executes his code. The problem is that at the moment the service doesn't start...I can't see logs or toast.

this is the manifest:

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

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

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

...

        <service
            android:name=".Sip.SipService"
            android:enabled="true"
            android:exported="true"></service>

        <receiver android:name=".Sip.Receiver"
            android:process=":remote">
            <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED"/>
                <action android:name="android.intent.action.REBOOT"/>
            </intent-filter>
        </receiver>
    </application>

</manifest>

The broadcastReceiver

public class Receiver extends BroadcastReceiver {

    public void onReceive(Context context, Intent intent) {
        Log.d("MIO", "abcde");
        Intent i = new Intent(context, MainActivity.class);
        context.startService(new Intent(context, SipService.class));
    }
}

and the service

public class SipService extends Service {

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

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Toast.makeText(this, "Service Started", Toast.LENGTH_LONG).show();
        Log.d("BeVoip", "service partito");
        startActivity(new Intent(this, MainActivity.class));
        return Service.START_STICKY;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        Toast.makeText(this, "Service Destroy", Toast.LENGTH_LONG).show();
    }
}

Upvotes: 0

Views: 99

Answers (1)

Ghulam Moinul Quadir
Ghulam Moinul Quadir

Reputation: 1648

Your BroadcastReceiver is not getting called because Apps targeting Android 7.0 (API level 24) and higher must register the following broadcasts with registerReceiver(BroadcastReceiver, IntentFilter).

Declaring a receiver in the manifest does not work

You will have to register receiver dynamically in the following way.

    Receiver  myReceiver = new Receiver ();
    IntentFilter intentFilter = new IntentFilter();
    intentFilter.addAction(Intent.ACTION_BOOT_COMPLETED);
    intentFilter.addDataScheme("package");
    getApplicationContext().registerReceiver(myReceiver, intentFilter);

About android:process=":remote"

The process in which the broadcast receiver should run. All components of an application run in the default process created for the application. It has the same name as the application package.

If the name assigned to this attribute begins with a colon (':'), a new process, private to the application, is created when it's needed and the broadcast receiver runs in that process.

You can read about receiver attributes here: https://developer.android.com/guide/topics/manifest/receiver-element

You can refer documentation of BroadcastReceiver here: https://developer.android.com/guide/components/broadcasts

Upvotes: 2

Related Questions