有关xiaomi和LeEco设备的抬头通知

时间:2017-09-20 07:51:18

标签: android android-notifications heads-up-notifications

我正在进行抬头通知。我的代码适用于所有设备,但不适用于xiaomi和LeEco之类的Android 5.1以上的设备。 我的代码是:

RemoteViews contentView = null;
    contentView = new RemoteViews(context.getPackageName(), R.layout.demo);

final android.support.v4.app.NotificationCompat.Builder notification =  new NotificationCompat.Builder(context)
            .setSmallIcon(R.mipmap.ic_launcher)
            .setDefaults(Notification.DEFAULT_ALL) 
            .setPriority(NotificationCompat.PRIORITY_HIGH)
            .setCustomHeadsUpContentView(contentView)
            .setVibrate(new long[0])
            .setCategory(Notification.CATEGORY_CALL)
            .setDeleteIntent(createOnDismissedIntent(context, 2222))
            .setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION))

final NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
    notificationManager.notify(1111, notification.build());

可能是什么问题?

1 个答案:

答案 0 :(得分:0)

我认为这个解决方案对您非常有帮助,但我无法为LeEco设备找到任何解决方案:

public class WhiteListUtils {

private static final String SKIP_WHITELIST_APP = "SKIP_WHITELIST_APP";

private static final String XIAOMI = "xiaomi";
private static final String HUAWEI = "huawei";
private static final String OPPO = "oppo";
private static final String VIVO = "vivo";

private static SharedPreferences settings;

private WhiteListUtils() {
}

public static void showWhiteListingApps(Context context) {
    if (settings == null)
        settings = context.getSharedPreferences("WhiteListUtils", MODE_PRIVATE);
    if (!settings.getBoolean(SKIP_WHITELIST_APP, false))
        checkOSCompat(context);
}

private static void checkOSCompat(Context context) {
    try {
        Intent intent = new Intent();
        String manufacturer = android.os.Build.MANUFACTURER;
        if (XIAOMI.equalsIgnoreCase(manufacturer)) {
            intent.setComponent(new ComponentName("com.miui.securitycenter", "com.miui.permcenter.autostart.AutoStartManagementActivity"));
        } else if (OPPO.equalsIgnoreCase(manufacturer)) {
            intent.setComponent(new ComponentName("com.coloros.safecenter", "com.coloros.safecenter.permission.startup.StartupAppListActivity"));
            intent.setComponent(new ComponentName("com.coloros.oppoguardelf", "com.coloros.powermanager.fuelgaue.PowerConsumptionActivity"));
        } else if (VIVO.equalsIgnoreCase(manufacturer)) {
            intent.setComponent(new ComponentName("com.vivo.permissionmanager", "com.vivo.permissionmanager.activity.BgStartUpManagerActivity"));
        } else if (HUAWEI.equalsIgnoreCase(manufacturer)) {
            intent.setComponent(new ComponentName("com.huawei.systemmanager", "com.huawei.systemmanager.optimize.process.ProtectActivity"));
        }

        if (isCallable(context, intent)) {
            showAlertWindow(context, intent);
        } else {
            if (BuildConfig.BUILD_TYPE.contains("release"))
                Crashlytics.log("Intent not callable for whitelisting " + intent.toString());
            Log.e("Intent not callable for whitelisting " + intent.toString());
        }
    } catch (Exception e) {
        if (BuildConfig.BUILD_TYPE.contains("release")) {
            Crashlytics.logException(e);
        }
        Log.e("checkOSCompat Error " + e.getMessage());
    }
}

private static void showAlertWindow(final Context context, final Intent intent) {
    new AlertDialog.Builder(context)
            .setIcon(android.R.drawable.ic_dialog_alert)
            .setTitle("Protected Apps")
            .setMessage(String.format("%s requires to be enabled in 'Protected Apps' to function properly.%n", context.getString(R.string.app_name)))
            .setPositiveButton("Go to Apps", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                    context.startActivity(intent);
                    settings.edit().putBoolean(SKIP_WHITELIST_APP, true).apply();
                }
            })
            .setNegativeButton(android.R.string.cancel, null)
            .show();
}


private static boolean isCallable(Context context, Intent intent) {
    List<ResolveInfo> list = context.getPackageManager().queryIntentActivities(intent,
            PackageManager.MATCH_DEFAULT_ONLY);
    return list.size() > 0;
}

public void hasProtectedApps(String packaGe, String activity) {
    try {
        String replacedActivityName = activity.replace(packaGe, "");
        //String cmd = "am start -n com.huawei.systemmanager/.optimize.process.ProtectActivity";
        String cmd = "am start -n " + packaGe + "/" + replacedActivityName;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
            cmd += " --user " + getUserSerial();
        }
        Runtime.getRuntime().exec(cmd);
    } catch (IOException e) {
        if (BuildConfig.BUILD_TYPE.contains("release"))
            Crashlytics.logException(e);
        Log.e("isProtectedApps Error " + e.getMessage());

    }
}

private String getUserSerial() {
    //noinspection ResourceType
    Object userManager = getSystemService("user");
    if (null == userManager) return "";

    try {
        Method myUserHandleMethod = android.os.Process.class.getMethod("myUserHandle", (Class<?>[]) null);
        Object myUserHandle = myUserHandleMethod.invoke(android.os.Process.class, (Object[]) null);
        Method getSerialNumberForUser = userManager.getClass().getMethod("getSerialNumberForUser", myUserHandle.getClass());
        Long userSerial = (Long) getSerialNumberForUser.invoke(userManager, myUserHandle);
        if (userSerial != null) {
            return String.valueOf(userSerial);
        } else {
            return "";
        }
    } catch (NoSuchMethodException | IllegalArgumentException | InvocationTargetException | IllegalAccessException e) {
        if (BuildConfig.BUILD_TYPE.contains("release"))
            Crashlytics.logException(e);
        Log.e("getUserSerial Error " + e.getMessage());
    }
    return "";
  }
}