1

我正在处理抬头通知。我的代码适用于所有设备,但不适用于 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());

可能是什么问题?

4

1 回答 1

2

我认为这个解决方案对你很有帮助,但是我找不到乐视设备的任何解决方案:

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 "";
  }
}
于 2018-01-29T10:40:18.947 回答