我有一个没有任何活动的应用程序,只是一个接收者。我重新启动接收器,然后使用adb安装apk。但是,我从未到达此接收器。为什么我永远都无法到达接收器(例如在重启时)?
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
}
}
答案 0 :(得分:1)
我认为您需要设置RECEIVE_BOOT_COMPLETED
权限,并为BOOT_COMPLETED
,QUICKBOOT_POWERON
之类的服务配置意图过滤器。
看看这个,他们的回答很好地解释了这个问题 https://stackoverflow.com/a/46294732/7774671
答案 1 :(得分:1)
清单中的接收器中缺少一些东西。试试这个。
<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>
答案 2 :(得分:0)
如果广播接收器没有故意注册,则不会被调用。此关联接收者/意图可以使用意图过滤器在清单中声明。
<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>