13

Code like this works well.

    Intent configIntent = new Intent (context, WidgetConfigActivity.class);
    configIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
    PendingIntent pIntent = PendingIntent.getActivity(context, 0, configIntent, PendingIntent.FLAG_UPDATE_CURRENT);
    remoteView.setOnClickPendingIntent(R.id.btn, pIntent);

But I want to hide that button befor activity will appear, so I'm triing to send intent to the widget itself, perform hiding components in onReceive() method and then start activity. Problem is that I can't use startActivity() function in AppWidget.

Is there any solution ?

4

4 に答える 4

19

問題は、AppWidget で startActivity() 関数を使用できないことです。

はい、できます。あなたはあなたの-- 呼び出しの(または)Contextオブジェクトに渡されます。onUpdate()onReceive()AppWidgetProviderstartActivity()

于 2012-06-29T22:58:29.457 に答える
16

ありがとう 2 CommonsWare

もう1つやるべきことがあります。この場合context.startActivity();は投げます。RuntimeException

Activity コンテキストの外部から startActivity() を呼び出すには、FLAG_ACTIVITY_NEW_TASK フラグが必要です。これは本当にあなたが望むものですか?

したがって、フラグを設定する必要があります

intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

前。

于 2012-06-30T09:14:49.297 に答える
6
   // on receive function use this for new activity start
               Intent intent = new Intent (context, AppWdget.class);
               intent.setFlags (Intent.FLAG_ACTIVITY_NEW_TASK);
               context.startActivity (intent);
于 2015-05-12T11:11:12.343 に答える