2012-02-25 59 views
0

我有一些奇怪的行为,我只能假设是因为我正在使用的等待意图。如何取消Android Widget中的待处理内容?

情景

我有具有4个按键的微件(4×1)。在小部件的onUpdate中,我为每个按钮添加一个待定的意图。我的意图用一个bundeled参数触发一个Service,并根据这个参数启动一些东西。我附上意图,因为这:

RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widget_layout); 

Bundle b = new Bundle(); 
b.putString("myVariable", someVariable); 

Intent intent = new Intent(context, AppStarterService.class); 
intent.putExtras(b); 

PendingIntent pendingIntent = PendingIntent.getService(context, buttopnPosition, intent, 0); 
views.setOnClickPendingIntent(R.id.btnOne, pendingIntent); 

问题

代码工作得很好,直到用户决定更新按钮的内容。然后,完成一个新的待定意向。所以,当我再次按下按钮时,有时它仍然执行旧的意图,而不是新的意图。我不知道如何更好地解释这一点。假设我的第一个意图参数是“TestOne”,在我更新后,新意图的参数是“TestX”。当用户点击按钮时,在我的服务中,我仍然使用“TestOne”代替“TestX”。所以,我的猜测是,不知何故,当widget按钮内容发生变化时,我需要取消先前的意图。 这是问题吗?难道我做错了什么 ?我如何取消旧的意图,我需要重新创建它,然后取消它?

谢谢你的时间。

+0

你试过了* pendingIntent.cancel(); * before * views.setOnClickPendingIntent(R.id.btnOne,pendingIntent); *? – Dharmendra 2012-02-25 09:02:57

回答

1

你一直有这个问题,我甚至有FLAG_UPDATE_CURRENT,尝试定义每个时间不同requestCode,像这样的东西:

private static int request = 0; 
... 
PendingIntent pendingIntent = PendingIntent.getService(context, request++, intent, 0); 

所以每次创建一个新的PendingIntent时,至少在类的生命期间会使用一个新的requestCode。

我希望它有帮助。

+0

它的工作原理!谢谢你节省了我的时间! – pjanecze 2012-03-20 17:57:24