首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >通知华为EMUI

通知华为EMUI
EN

Stack Overflow用户
提问于 2017-02-15 21:09:23
回答 2查看 1.4K关注 0票数 0

我正在写通知应用程序。要设置通知,我使用AlarmManager。

一切似乎都很顺利,但不幸的是,华为的情况并非如此。当用户关闭应用程序时,通知不会发出(在其他设备LG上,NEXUS all运行良好)。

你知道怎么修复它吗?

代码语言:javascript
复制
    intent = new Intent(context, AlarmReceiver.class);
    sender = PendingIntent.getBroadcast(context, alarmId, intent, 0);


    am.set(android.app.AlarmManager.RTC_WAKEUP, timeToAlarm, sender);

编辑

代码语言:javascript
复制
public class AlarmReceiver extends BroadcastReceiver {
private static final String TAG = "AlarmReceiver";
private PowerManager.WakeLock wakeLock;

@Override
public void onReceive(Context context, Intent intent) {

    PowerManager pm = (PowerManager) context
            .getSystemService(Context.POWER_SERVICE);
    wakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, TAG);

    wakeLock.acquire();

    new NotificationUtils(context, null).postNotification(context.getString(R.string.notification_title), context.getString(R.string.notification_message));

    unlock();
}

private void unlock() {
    if (wakeLock != null)
        if (wakeLock.isHeld())
            wakeLock.release();

    wakeLock = null;
}

}

EN

回答 2

Stack Overflow用户

发布于 2017-02-15 21:12:45

设置中有受保护的应用程序选项。转到那里并选择应用程序

票数 0
EN

Stack Overflow用户

发布于 2017-08-11 17:18:16

在测试我的通知时发现了此解决方案。编写完这段代码后,在OnCreate()中调用ifHuaweiAlert(),看看会发生什么。

代码语言:javascript
复制
private void ifHuaweiAlert() {
    final SharedPreferences settings = getSharedPreferences("ProtectedApps", MODE_PRIVATE);
    final String saveIfSkip = "skipProtectedAppsMessage";
    boolean skipMessage = settings.getBoolean(saveIfSkip, false);
    if (!skipMessage) {
        final SharedPreferences.Editor editor = settings.edit();
        Intent intent = new Intent();
        intent.setClassName("com.huawei.systemmanager", "com.huawei.systemmanager.optimize.process.ProtectActivity");
        if (isCallable(intent)) {
            final AppCompatCheckBox dontShowAgain = new AppCompatCheckBox(this);
            dontShowAgain.setText("Do not show again");
            dontShowAgain.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                    editor.putBoolean(saveIfSkip, isChecked);
                    editor.apply();
                }
            });

            new AlertDialog.Builder(this)
                    .setIcon(android.R.drawable.ic_dialog_alert)
                    .setTitle(R.string.huawei_protected_apps)
                    .setMessage(String.format("%s" + " "requiers to be enabled in Protected Apps) , getString(R.string.app_name)))
                    .setView(dontShowAgain)
                    .setPositiveButton("Protected Apps", new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int which) {
                            huaweiProtectedApps();
                        }
                    })
                    .setNegativeButton(android.R.string.cancel, null)
                    .show();
        } else {
            editor.putBoolean(saveIfSkip, true);
            editor.apply();
        }
    }
}

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

private void huaweiProtectedApps() {
    try {
        String cmd = "am start -n com.huawei.systemmanager/.optimize.process.ProtectActivity";
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
            cmd += " --user " + getUserSerial();
        }
        Runtime.getRuntime().exec(cmd);
    } catch (IOException ignored) {
    }
}

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 ignored) {
    }
    return "";
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/42250324

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档