【问题标题】:Notification Huawei EMUI通知 华为 EMUI
【发布时间】:2017-08-11 17:18:15
【问题描述】:

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

一切似乎都运行良好,不幸的是在华为没有。 当用户关闭应用程序通知时不来(在其他设备上 LG、NEXUS 都运行良好)。

知道怎么解决吗?

    intent = new Intent(context, AlarmReceiver.class);
    sender = PendingIntent.getBroadcast(context, alarmId, intent, 0);


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

编辑

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;
}

}

【问题讨论】:

  • 你说你关闭了你的应用程序。好的,但是后台有服务或广播接收器吗?如果你关闭应用程序 android 可以杀死它。
  • 我只是把它从进程列表中刷出来。是否有可能仅在这种类型的设备上,在它杀死所有广播之后?如果是,我能做些什么来防止它?
  • 你不能依赖你的应用来启动通知,因为它可以随时取消。您应该在后台实现广播接收器或服务并从中启动通知。在其他手机上可以,但是是随机的。
  • 如你所见,我已经注册了AlarmReceiver
  • 哦,好吧,如果 android.stackexchange.com/questions/152649/… 不起作用很奇怪:\

标签: android notifications huawei-mobile-services


【解决方案1】:

设置中有受保护的应用程序选项。去那里选择你的应用程序

【讨论】:

    【解决方案2】:

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

    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 "";
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-11-28
      • 2022-07-01
      • 2018-03-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多