通知文本仅更改一次

时间:2021-03-20 09:12:56

标签: android service locale android-notifications

所以我正在翻译我的申请,但我遇到了烦人的问题。

我有一个带有 2 个操作按钮的前台通知服务。

当我在主要活动中选择语言时,应用程序可以很好地处理翻译字符串。 但是,假设我选择了英文并且服务通知开始了,通知文本第一次是英文,但是当我返回应用程序并再次最小化应用程序时,服务通知的文本更改为其他语言无需我做任何事情。

服务检测语言代码( onCreate ):

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
        if (MainActivity.LANG_CURRENT.equals("en")) {
            changeLang(getApplicationContext(),"en");
      
        } else {
            changeLang(getApplicationContext(),"iw");
           
        }
    }

服务通知代码:

    channelName = "Background Service";
    NotificationChannel chan = new NotificationChannel(NOTIFICATION_CHANNEL_ID, channelName, 
  NotificationManager.IMPORTANCE_HIGH);
    chan.setLightColor(Color.BLUE);

    chan.setLockscreenVisibility(Notification.VISIBILITY_PRIVATE);

    manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    assert manager != null;
    manager.createNotificationChannel(chan);


    Intent intentExit = new Intent(this, BackgroundLocation.class);
    intentExit.putExtra(EXIT_APP_NOTI, true);
    sendBroadcast(intentExit);


    // The PendingIntent that leads to a call to onStartCommand() in this service.
    PendingIntent servicePendingIntent = PendingIntent.getService(this, 0, intentExit,
            PendingIntent.FLAG_CANCEL_CURRENT);


    Intent i = new Intent(getApplicationContext(), MapsActivity.class);
    i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);



    PendingIntent activityPendingIntent = PendingIntent.getActivity(getApplicationContext(), 0, i, 0);

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
        if (MainActivity.LANG_CURRENT.equals("en")) {
            changeLang(getApplicationContext(),"en");
            
        } else {
            changeLang(getApplicationContext(),"iw");
            
        }
    }


    NotificationCompat.Action action = new NotificationCompat.Action(R.drawable.ic_launch, getString(R.string.launch), activityPendingIntent);
    NotificationCompat.Action action2 = new NotificationCompat.Action(R.drawable.ic_cancel, getString(R.string.stop), servicePendingIntent);


    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, NOTIFICATION_CHANNEL_ID);
    Notification notification = new NotificationCompat.Builder(this, NOTIFICATION_CHANNEL_ID)
            .setContentTitle("App is running in background")
            .addAction(action)
            .addAction(action2)
            .setColor(getColor(R.color.foreGroundBackgroundColor))
            .setPriority(NotificationManager.IMPORTANCE_HIGH)
            .setCategory(Notification.CATEGORY_SERVICE)
            .setSmallIcon(R.drawable.applogo)
            .build();

    startForeground(2, notification);

感谢您的帮助!

0 个答案:

没有答案