关于这个问题不是互联网上的答案,我找不到。我把所有的一天都花在了searces上,所以我开始思考,没有人知道
我是stackoverflow的新手,英语不是我的本地语言。所以,抱歉我的英语不好。
好的,这是一个问题。我用2个按钮创建一个简单的小部件。其中一个按钮可以通过单击它来启动config.activity。 我怎么能这样做? 我尝试使用Intent,如下所示:
Intent i = new Intent(context, ConfigActivity.class);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);
但是当我收到意图操作时,我会看到下一种情况:config.activity快速启动和关闭,因为appWidgetId无效。
如何从小部件启动配置活动?
答案 0 :(得分:0)
您可以使用此代码
RemoteViews rv = new RemoteViews(context.getPackageName(), R.layout.widget_layout);
Intent launchActivity = new Intent(context, ConfigActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, launchActivity, 0);
titles.setOnClickPendingIntent(R.id.Button, pendingIntent);
ComponentName thisWidget = new ComponentName(context, Widget.class);
AppWidgetManager manager = AppWidgetManager.getInstance(context);
manager.updateAppWidget(thisWidget, rv);
答案 1 :(得分:0)
如果要从Provider类更新,请从onUpdate()方法中获取小部件ID,如下所示:
final int N = appWidgetIds.length;
for (int i=0; i<N; i++) {
int appWidgetId = appWidgetIds[i];
}
或者如果您从ConfigurationActivity更新小部件,请从onCreate方法中获取小部件ID,如下所示:
Intent intent = getIntent();
Bundle extras = intent.getExtras();
if (extras != null) {
mAppWidgetId = extras.getInt(AppWidgetManager.EXTRA_APPWIDGET_ID, AppWidgetManager.INVALID_APPWIDGET_ID);
}
使用widget_layout
中的按钮/ imageview制作小部件 // Create intent pointing to ConfigurationActivity, in this example we are at ConfigurationActivity
Intent configurationIntent = new Intent(WidgetBarConfiguration.this, WidgetBarConfiguration.class);
// Create a extra giving the App Widget Id
configurationIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, mAppWidgetId);
// Create a pending intent giving configurationIntent as parameter
PendingIntent configurationPendingIntent = PendingIntent.getActivity(WidgetBarConfiguration.this, 0, configurationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
// Here we fecth the layout item and give it a action
RemoteViews views = new RemoteViews(WidgetBarConfiguration.this.getPackageName(), R.layout.widget_bar_layout);
// Setting onClick event that will lauch ConfigurationActivity
views.setOnClickPendingIntent(R.id.imageViewSettings, configurationPendingIntent);
// Updating widget
mWidgetManager.updateAppWidget(mAppWidgetId, views);
Intent resultValue = new Intent();
resultValue.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, mAppWidgetId);
setResult(RESULT_OK, resultValue);
finish();
的AndroidManifest.xml
<activity
android:name=".WidgetBarConfiguration"
android:label="@string/title_activity_widget_configuration"
android:launchMode="singleInstance"
android:theme="@style/AppTheme.NoActionBar" >
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_CONFIGURE" />
</intent-filter>
</activity>
看看lauchMode = "singleInstance"
将确保在点击时仅显示ConfigurationActivity布局,而不附加任何其他活动布局。