Madmagic
Madmagic

Reputation: 75

Android - can't get broadcastreceiver to work

I try to launch a service at boot, but it never starts the service. I added <action android:name="android.intent.action.ACTION_POWER_CONNECTED"/> to the intent-filter of the receiver, but when I connect my android 8.0 phone to power, it also doesn't work.

manifest:

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

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.PACKAGE_USAGE_STATS" 
tools:ignore="ProtectedPermissions" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />

<application
    android:usesCleartextTraffic="true"
    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">

    <activity android:name=".MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <action android:name="android.intent.action.VIEW" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

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

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

</application>
</manifest>

StartAtBoot:

public class StartAtBoot extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {

    Toast.makeText(context, "received", Toast.LENGTH_LONG).show(); //this never shows up when connecting to power

    Intent i = new Intent(context, MainService.class);
    context.startService(i);
}
}

MainService:

public class MainService extends Service {

public static boolean isRunning = false;

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

@Override
public void onCreate() {
    isRunning = true;

    Toast.makeText(this, "Service started", Toast.LENGTH_LONG).show();
    ConnectionChecker.run(this); //when device has wifi connection, this will run connected() method
}

@Override
public void onDestroy() {
    isRunning = false;
}

public void connected() {
    //things to do when it has wifi connection
}
}

In my MainActivity class, I start this service using the same way as in my StartAtBoot class, and there it works fine when I open the application. So the service is working fine, its just that the StartAtBoot class doesn't run the code.

Upvotes: 0

Views: 107

Answers (1)

CommonsWare
CommonsWare

Reputation: 1007544

ACTION_POWER_CONNECTED is not a listed exception to the limits on implicit Intent broadcasts. Your app cannot register for it in the manifest.

If your goal is to do work periodically, but only if the device has power, use JobScheduler or WorkManager.

Upvotes: 2

Related Questions