2016-09-20 72 views
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; 
} 

我把記錄在我的WidgetProvideronReceive它具有正確的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); 
    } 

如何解決這個問題?先謝謝你。

回答

3

我在我的onReceive裏添加了setData(),我在那裏傳遞數據。

@Override 
public void onReceive(Context context, Intent intent) { 
super.onReceive(context, intent); 

    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); 
    goToDetails.setData(Uri.parse(goToDetails.toUri(Intent.URI_INTENT_SCHEME))); 
    context.startActivity(goToDetails); 



} 

} 

但我遇到崩潰,如果我未安裝的應用程序,然後安裝並不會首先打開應用程序,然後添加一個widget並點擊ListView

找到了它爲什麼崩潰,因爲我的數據庫它仍然不存在。因爲我在我的SplashActivity

中創建我的數據庫
相關問題