9
在我ListProvider
類,它實現RemoteViewsFactory
我已經推杆下面的下面的代碼:打開活動它如何傳遞數據並從Widget中打開Activity? [Android]產品
@Override
public RemoteViews getViewAt(int position) {
RemoteViews rv = new RemoteViews(mContext.getPackageName(), R.layout.list_row);
rv.setTextViewText(R.id.heading, merch_name);
rv.setTextViewText(R.id.content, teaser);
Bundle extras = new Bundle();
extras.putInt(WidgetProvider.EXTRA_ITEM, position);
extras.putString(WidgetProvider.MERCHANT_ITEM, mWidgetItems.get(position).merchant_id);
Intent fillInIntent = new Intent();
fillInIntent.putExtras(extras);
rv.setOnClickFillInIntent(R.id.llRow, fillInIntent);
return rv;
}
我把記錄在我的WidgetProvider
的onReceive
它具有正確的ID當我點擊,但經過在extras
中沒有正確的ID。有時間,它不打開我提供的Activity
,只是打開我的MainActivity
。當我從recently open app
中刪除我的應用程序然後使用widget
時會發生這種情況。
這裏是我的onReceive
代碼在我WidgetProvider
public class WidgetProvider extends AppWidgetProvider {
public static final String TOAST_ACTION = "my.packagename.TOAST_ACTION";
public static final String MERCHANT_ITEM = "my.packagename.MERCHANT_ITEM";
@Override
public void onReceive(Context context, Intent intent) {
AppWidgetManager mgr = AppWidgetManager.getInstance(context);
if (intent.getAction().equals(TOAST_ACTION)) {
int appWidgetId = intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID,
AppWidgetManager.INVALID_APPWIDGET_ID);
Intent goToDetails = new Intent(context, DetailsActivity.class);
goToDetails.putExtra(Constants.MERCHANT_ID, intent.getStringExtra(MERCHANT_ITEM));
goToDetails.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(goToDetails);
super.onReceive(context, intent);
}
這就是我如何得到我的ID DetailsActivity
public class DetailsActivity extends Activity {
String merchantID;
@Override
protected void onCreate(Bundle savedState) {
super.onCreate(savedState);
setContentView(R.layout.detailsactivity);
Bundle extras = getIntent().getExtras();
if (extras != null) {
merchantID = extras.getString(Constants.MERCHANT_ID);
}
如何解決這個問題?先謝謝你。