gpt4 book ai didi

android - 服务的 Intent.replaceExtras(bundle) 未按预期工作

转载 作者:太空狗 更新时间:2023-10-29 16:24:03 24 4
gpt4 key购买 nike

我有一个服务,对于本次讨论,它包含一个数字和一个字符串以呈现给 Activity 。将其视为面向服务的复制缓冲区。启动后,服务将像这样启动(使用 startService):

Intent srvIntent = new Intent(this, SetManager.class);
Bundle bundle = new Bundle();
bundle.putLong(getString(R.string.intent_key_setnbr), setNbr);
bundle.putString(getString(R.string.intent_key_setmsg), setMsg);
srvIntent.putExtras(bundle);
startService(srvIntent);
return true;

一切正常。服务启动,在通知中显示由 setMsg 标识的消息(例如“MESSAGE1”)。

当用户触摸 Notification 时,将启动一个 Activity,该 Activity 会更新 setNbr 和 setMsg,并将它们发送回服务。该服务反过来用新消息更新通知,例如“MESSAGE2”(也可以正常工作)。

但问题在于:当用户触摸此通知时,Activity 显示的是原始 setMsg(“MESSAGE1”),而不是“MESSAGE2”。我已经放入 Log.d() 东西来查看过程哪里出了问题,但我没有看到它。该服务很好地记录了传入的“MESSAGE2”,并在 replaceExtras() 之后命中了代码。事实上,传入的值必须是正确的,否则通知不会更新为“MESSAGE2”。因此,Notification Intent 的 extras bundle 似乎没有正确更新,因为所有其他往返的东西都工作正常。或者,不太可能, Activity 仅在第一次尝试时正常工作?

所以我质疑我更新通知的方法。我相信它是“按照书本”,但作为一个相对的 android 新手,有很多书我还没有读过。感谢您的指点。这是服务代码:

package yada.yada.boom.Setlines;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.IBinder;
import android.text.ClipboardManager;
import android.util.Log;

public class NoteManager extends Service {

private NotificationManager mNM = null;
private Notification notification = null;
private Context context;
private final CharSequence contentTitle = "Setlines";
private CharSequence contentText;
private Intent notificationIntent;
private PendingIntent pendingIntent;
private int notificationId = 0;
private final String MY_TAG = "SetlinesSvc";

@Override
public void onCreate() {
super.onCreate();
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
long setNbr = 0L;
String setMsg = null;

super.onStartCommand(intent, flags, startId);
context = getApplicationContext();
if (intent != null) {
Bundle extras = intent.getExtras();
if (extras != null) {
setNbr = extras.getLong(getString(R.string.intent_key_setnbr));
setMsg = extras
.getString(getString(R.string.intent_key_setmsg));
}
}
if ((setNbr == 0L) || (setMsg == null)) {
Log.d(MY_TAG, "Setting default message.");
setNbr = 0L;
setMsg = "Click to view setMsg.";
} else {
Log.d(MY_TAG, "Got set number (" + setNbr + ") and string ("
+ setMsg.substring(0, 10) + "...) from intent.");
}
if (notification == null) {
createNotification(setNbr, setMsg);
} else {
updateNotificationText(setNbr, setMsg);
}
return START_STICKY;
}

@Override
public void onDestroy() {
// Log.d(MY_TAG, "onDestroy() called");
mNM.cancel(notificationId);
super.onDestroy();
}

@Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
return null;
}

// Create a status bar notification
void createNotification(long setNbr, String setMsg) {
CharSequence tickerText = null;
int icon = 0;

// Log.d(MY_TAG, "createNotification called");
mNM = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

icon = R.drawable.ic_notification;
tickerText = "Setlines";

// Instantiate the Notification

long when = System.currentTimeMillis();

notification = new Notification(icon, tickerText, when);

context = getApplicationContext();
contentText = setMsg;
notificationIntent = new Intent(this, SetServiceMenu.class);
Bundle bundle = new Bundle();
bundle.putLong(getString(R.string.intent_key_setnbr), setNbr);
if (setNbr != 0L) {
bundle.putString(getString(R.string.intent_key_setmsg), setMsg);
Log.d(MY_TAG, "Added extras: SetNbr(" + setNbr + ") SetMsg("
+ setMsg.substring(0, 10) + "...)");
notificationIntent.putExtras(bundle);
}
pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent,
PendingIntent.FLAG_ONE_SHOT);
notification.setLatestEventInfo(context, contentTitle, contentText,
pendingIntent);
notificationId += 1; // Bump the notification ID number
// Pass the Notification to the NotificationManager:
Log.d(MY_TAG, "createNotification() ... passing notification");
mNM.notify(notificationId, notification);
startForeground(notificationId, notification);
}

void updateNotificationText(long setNbr, String setMsg) {
contentText = setMsg;
notificationIntent = new Intent(this, SetServiceMenu.class);
Bundle bundle = new Bundle();
bundle.putLong(getString(R.string.intent_key_setnbr), setNbr);
bundle.putString(getString(R.string.intent_key_setmsg), setMsg);
// notificationIntent.putExtras(bundle);
notificationIntent.replaceExtras(bundle);
Log.d(MY_TAG, "Replaced extras: SetNbr(" + setNbr + ") SetMsg("
+ setMsg.substring(0, 10) + "...)");
pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent,
PendingIntent.FLAG_ONE_SHOT);
notification.setLatestEventInfo(context, contentTitle, contentText,
pendingIntent);

mNM.notify(notificationId, notification);
}

}

...这是被调用的 Activity 的 onCreate:

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
context = this.getApplicationContext();
Bundle extras = getIntent().getExtras();
String setMsg = "";

dbAdapter = new SetDbAdapter(context);

dbAdapter.openReadable();
if (savedInstanceState != null) {
setNbr = savedInstanceState.getLong("setNbr");
// Log.d(MY_TAG, "Retrieved set# (" + setNbr + ") from intent.");
setMsg = savedInstanceState.getString("setMsg");
// Log.d(MY_TAG, "Retrieved text (" + setMsg.substring(0, 10)
// + "...) from intent.");
setMsg = dbAdapter.getSetById(setNbr);
} else if (extras != null) {
setNbr = extras.getLong(getString(R.string.intent_key_setnbr));
// Log.d(MY_TAG, "Retrieved set# (" + setNbr + ") from extras.");
setMsg = extras.getString(getString(R.string.intent_key_setline));

if ((setMsg == null) && (setNbr != 0)) {
setMsg = dbAdapter.getSetById(setNbr);
}
}
if ((setNbr == 0) || (setMsg == null)) {
setNbr = nextMessageNbr();
setMsg = messageText(setNbr);
}
dbAdapter.close();
svcTagNbr = setNbr;
svcTagline = setMsg;
d = new myDialog(this);
d.show();
notifyService(false);
}

最后,这里是notifyService()

private void notifyService(boolean getNew) {

// Here we get a new setMsg, and notify the server to create
// notification with it.
long mySetNbr = svcSetNbr;
String mySetMsg = svcSetMsg;

if (getNew) {
mySetNbr = nextMessageNbr();
mySetline = messageText(mySetNbr);
}

Intent srvIntent = new Intent(context, SetManager.class);
Bundle bundle = new Bundle();
bundle.putLong(getString(R.string.intent_key_setnbr), mySetNbr);
bundle.putString(getString(R.string.intent_key_setmsg), mySetMsg);
srvIntent.putExtras(bundle);
startService(srvIntent);
}

这一切都因为我似乎无法找到调试服务的方法而变得更加复杂。任何有关调试服务的指示也将不胜感激!

最佳答案

找到了!问题出在丢失的标志上。显然,即使我们使用 updateExtras(),Android 也不会注意,除非您在 getActivity() 方法上使用特殊标志 (FLAG_UPDATE_CURRENT)。

来自 http://developer.android.com/reference/android/app/PendingIntent.html 的文档:

...如果所描述的 PendingIntent 已经存在,则保留它,但用这个新 Intent 中的内容替换它的额外数据。如果您正在创建只有 extras 发生变化的 intent,并且不关心收到您之前的 PendingIntent 的任何实体将能够使用您的新 extras 启动它,即使它们没有明确提供给它,也可以使用它。

所以新代码看起来像:

void updateNotificationText(long setNbr, String setMsg) {
contentText = setMsg;
Bundle bundle = new Bundle();
bundle.putLong(getString(R.string.intent_key_setnbr), setNbr);
bundle.putString(getString(R.string.intent_key_setmsg), setMsg);
notificationIntent.replaceExtras(bundle);
pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent,
PendingIntent.FLAG_ONE_SHOT
+ PendingIntent.FLAG_UPDATE_CURRENT);
notification.setLatestEventInfo(context, contentTitle, contentText,
pendingIntent);
mNM.notify(notificationId, notification);
}

关于android - 服务的 Intent.replaceExtras(bundle) 未按预期工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6714443/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com