如何传递数据并从Widget打开活动? [安卓] [英] How to pass data and open an Activity from Widget? [Android]

查看:81
本文介绍了如何传递数据并从Widget打开活动? [安卓]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在实现RemoteViewsFactoryListProvider类中,我在下面放置了以下代码:

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

我将日志放在WidgetProvideronReceive中,当我单击时,它具有正确的ID,但是在打开活动后,它在extras中的位置没有正确的ID.也有一段时间它不会打开我提供的Activity而只是打开我的MainActivity.当我从recently open app中删除我的应用程序,然后使用widget时,会发生这种情况.

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.

这是我在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);

    }

这就是我在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.

推荐答案

我在onReceive中添加了传递数据的setData().

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



}

}

但是,如果我先卸载该应用程序然后又安装但又没有打开该应用程序,则会遇到崩溃,然后添加widget并单击ListView.

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.

找出了为什么由于我的数据库而导致崩溃的原因,它仍然不存在.因为我正在SplashActivity

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

这篇关于如何传递数据并从Widget打开活动? [安卓]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆