我有这个接收器:
public class DeviceOwnerReceiver extends DeviceAdminReceiver {
private static final String DEFAULT_CHANNEL_ID = "dpc_channel";
private static final int NOTIFICATION_ID = 1;
private static final String DPC_SERVICE = "dpc";
@Override
public void onProfileProvisioningComplete(Context context, Intent intent) {
Log.i("TAG","Before onProfile");
showNotification(context,context.getString(R.string.notification_title),context.getString(R.string.notification_content));
DpcService dpcService = new DpcService(context);
ServiceManager.addService(DPC_SERVICE,dpcService);
Log.i("TAG","After onProfile");
}
@Override
public void onReceive(Context context, Intent intent) {
switch (intent.getAction()) {
case Intent.ACTION_BOOT_COMPLETED:
Log.i("TAG","Before BOOT_COMPLETED");
DevicePolicyManager mDpm = (DevicePolicyManager) context.getSystemService(Context.DEVICE_POLICY_SERVICE);
if(mDpm.isDeviceOwnerApp(getComponentName(context).getPackageName())){
Log.i("TAG","in the of BOOT_COMPLETED");
DpcService dpcService = new DpcService(context);
ServiceManager.addService(DPC_SERVICE, dpcService);
}
Log.i("TAG","AFTER BOOT_COMPLETED");
break;
}
}
private void showNotification(
Context context, String title, String msg) {
NotificationManager notificationManager =
(NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = getNotificationBuilder(context)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle(title)
.setContentText(msg)
.setStyle(new NotificationCompat.BigTextStyle().bigText(msg))
.build();
if (notificationManager != null) {
notificationManager.notify(NOTIFICATION_ID, notification);
}
}
private NotificationCompat.Builder getNotificationBuilder(Context context) {
createDefaultNotificationChannel(context);
return new NotificationCompat.Builder(context, DEFAULT_CHANNEL_ID);
}
private void createDefaultNotificationChannel(Context context) {
NotificationManager notificationManager =
(NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
String appName = "Dpc";
NotificationChannel channel = new NotificationChannel(DEFAULT_CHANNEL_ID,
appName, NotificationManager.IMPORTANCE_LOW);
if (notificationManager != null) {
notificationManager.createNotificationChannel(channel);
}
}
public static ComponentName getComponentName(Context context) {
return new ComponentName(context, DeviceOwnerReceiver.class);
}
}
带有以下清单:
<manifest xmlns:tools="http://schemas.android.com/tools"
package="com.dpc.deviceowner"
xmlns:android="http://schemas.android.com/apk/res/android"
android:versionCode="1"
android:versionName="1.0"
android:sharedUserId="android.uid.system">
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<application
android:allowBackup="true"
android:label="@string/app_name"
android:icon="@drawable/ic_launcher">
<receiver
android:name=".DeviceOwnerReceiver"
android:permission="android.permission.BIND_DEVICE_ADMIN">
<meta-data
android:name="android.app.device_admin"
android:resource="@xml/device_owner_receiver"/>
<intent-filter>
<action android:name="android.app.action.PROFILE_PROVISIONING_COMPLETE"/>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
</intent-filter>
</receiver>
</application>
</manifest>
当我配置设备时,它将转到接收方的onReceive()的启动操作,而不是onProfileProvisioningComplete(上下文上下文,Intent intent)。但是,当我删除所有有关完成操作启动的代码时,在配置设备时,它将转到onProfileProvisioningComplete(上下文上下文,Intent目的)。为什么会这样?