1

I have an application without any activity but just a receiver. I regiter the receiver and then install the apk using adb. However, I never reach this receiver. Why Do I never reach the receiver (for example while rebooting)?

Manifest.xml

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

<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">
    <receiver android:name=".DeviceOwnerReceiver">
    </receiver>
</application>

</manifest>

DeviceOwnerReceiver.java:

package amiin.bazouk.application.com.doproject;


import android.app.Activity;
import android.app.admin.DeviceAdminReceiver;
import android.app.admin.DevicePolicyManager;
import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

public class DeviceOwnerReceiver extends DeviceAdminReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();
        if(action.equals("android.intent.action.BOOT_COMPLETED")){
            System.out.println("ADRIEN");
        }

        if(action.equals("android.intent.action.BATTERY_CHANGED")){
            System.out.println("ADRIEN");
        }
    }

    @Override
    public void onProfileProvisioningComplete(Context context, Intent intent) {
        if (Util.isDeviceOwner(context)){
            //start enforcing policies: example if kioskModeEnabled->startKiostMode
        }
    }

    @Override
    public void onLockTaskModeEntering(Context context,Intent intent,String pkg) {
        if (Util.isDeviceOwner(context)){
            //start enforcing kiosk mode policies like this example when this property is done:
            DevicePolicyManager dpm = (DevicePolicyManager) context.getSystemService(Activity.DEVICE_POLICY_SERVICE);;
            dpm.setStatusBarDisabled(new ComponentName(context.getApplicationContext(), DeviceOwnerReceiver.class),false);
        }
    }

    @Override
    public void onLockTaskModeExiting(Context context,Intent intent) {
        //exit kioskmode
    }
}
Bazouk55555
  • 557
  • 6
  • 24
  • Most answers have indicated that you need to declare the appropriate `` for the `BroadcastReceiver` in the manifest. Please also realize that you cannot distribute an app that does not have at least one `Activity`, because when your app is installed it is in the "stopped state". In this state, Android will not deliver any broadcast `Intent`s to it. The only way to get the app out of the "stopped state" is for the user to manually launch it. For this to happen the app needs to have at least one `Activity`. – David Wasser Apr 02 '19 at 14:01

3 Answers3

1

I think you need to setup RECEIVE_BOOT_COMPLETED permission, and also configure intent filter to your service like BOOT_COMPLETED, QUICKBOOT_POWERON.

Look at this, they answer explain very well the issue https://stackoverflow.com/a/46294732/7774671

1

There are few things missing in the receiver in the manifest. Try this.

<receiver 
        android:name=".DeviceOwnerReceiver"
        android:enabled="true"
        android:exported="true">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
<action android:name="android.intent.action.BATTERY_CHANGED"/>
<category android:name="android.intent.category.DEFAULT"/>
</intent-filter>
</receiver>
amitava
  • 505
  • 1
  • 5
  • 10
0

A broadcast receiver will not be invoked if it is not registered with an intent. This association receiver / intent can be declared in the manifest using intent filter.

<receiver android:name=".DeviceOwnerReceiver">
  <intent-filter>
   <action android:name="android.intent.action.BOOT_COMPLETED"></action>
<action android:name="android.intent.action.BATTERY_CHANGED"></action>
  </intent-filter>
</receiver>
Hichem BOUSSETTA
  • 1,791
  • 1
  • 21
  • 27