Android не будет получать при получении - PullRequest
0 голосов
/ 17 апреля 2019

У меня есть этот приемник:

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 (Контекст контекста, намерение намерения). Тем не менее, когда я удаляю весь код о завершении действия загрузки, когда я инициализирую устройство, оно собирается onProfileProvisioningComplete (Контекст контекста, Намерение намерения). Почему это так?

...