Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
521 views
in Technique[技术] by (71.8m points)

listview - How to pass data and open an Activity from Widget? [Android]

In my ListProvider class that implements RemoteViewsFactory I have putted the following code below:

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

I put Logs in my onReceive of WidgetProvider it has the correct ID when I clicked, but after opening the activity it doesn't have the correct ID where it is put in extras. There are time as well that it does not open the Activity I provided and just open my MainActivity. This happens when I removed my app from recently open app and then use the widget.

Here is my code for onReceive in my 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);

    }

And this is how I get the ID in my 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);
    }

How to solve this? Thank you in advance.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I added setData() in my onReceive where I pass data.

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



}

}

But I encounter a crash, if I un-install the app and then install and doesn't open the app first then add a widget and click on the ListView.

Found out why it crashes because of my database it still doesn't exist. Because I'm creating my database in my SplashActivity


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...