Service Intent.replace Extras(bundle)无法按预期工作

时间:2011-07-16 00:23:25

标签: android service notifications android-intent

我有一个服务,在本次讨论中,持有一个数字和一个字符串,以呈现给一个活动。可以把它想象成面向服务的复制缓冲区。启动后,服务启动(使用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”)。

当用户触摸通知时,将启动一个活动,并且该活动等更新setNbr和setMsg,然后将这些更新回服务。该服务反过来用新消息更新通知,例如, “MESSAGE2”(也可以正常工作)。

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

所以我质疑我更新通知的方法。我相信它是“通过书本”,但作为一个相对的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);
}

由于我似乎无法找到调试服务的方法,这一切都变得更加复杂。关于调试服务的任何指针也将非常感激!

1 个答案:

答案 0 :(得分:6)

发现它!问题在于缺少一面旗帜。显然即使我们使用updateExtras(),除非你在getActivity()方法上使用特殊标志(FLAG_UPDATE_CURRENT),否则Android不会引起注意。

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

...如果描述的PendingIntent已经存在,那么保留它,但用这个新Intent中的内容替换它的额外数据。如果您要创建只有附加内容更改的意图,并且不关心接收到您之前的PendingIntent的任何实体将能够使用您的新附加功能启动它,即使它们未明确赋予它,也可以使用此功能。

所以新代码如下:

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