gpt4 book ai didi

Android 小部件配置打开 Activity

转载 作者:搜寻专家 更新时间:2023-11-01 08:42:15 25 4
gpt4 key购买 nike

我想为应用程序创建一个小部件,并且已经编写了小部件提供程序配置 类。我在小部件布局中只有一张图片,单击它时,我希望它打开 MyActivity Activity 类。在配置布局上有一个红色图像,当我单击该图像时,小部件图像会发生变化。 问题 是当我单击小部件时,它不会打开 MyActivity

我应该把 views.setOnClickPendingIntent 放在哪里?进入 appWidgetProvider ?进入 appWidgetConfiguration?两者兼顾?

问题 2 是:我是否必须将此代码放入 MyActivity 才能通过点击小部件打开它? : sendBroadcast(new Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_HOME));

这是我的:

appwidget.xml

<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">


<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@mipmap/ic_launcher"
android:id="@+id/imageWidget" />
</LinearLayout>

appWidgetProvider.java

public class OffAppWidgetProvider extends AppWidgetProvider {

@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
final int N = appWidgetIds.length;
for (int i=0; i<N; i++) {
int appWidgetId = appWidgetIds[i];

Intent intent = new Intent(context, MyActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.appwidget);
views.setOnClickPendingIntent(R.id.imageWidget, pendingIntent);

appWidgetManager.updateAppWidget(appWidgetId, views);
}
}

appWidgetConfiguration.java

public class AppWidgetConfiguration extends Activity{
private int mAppWidgetId;
private ImageView image;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setResult(RESULT_CANCELED);
setContentView(R.layout.activity_appwidget_configuration);

Intent intent = getIntent();
Bundle extras = intent.getExtras();
if (extras != null) {
mAppWidgetId = extras.getInt(
AppWidgetManager.EXTRA_APPWIDGET_ID,
AppWidgetManager.INVALID_APPWIDGET_ID);
}

image=(ImageView)findViewById(R.id.imageWidgetRed);
image.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Context context= AppWidgetConfiguration.this;
AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
RemoteViews views = new RemoteViews(getPackageName(),R.layout.appwidget);
views.setImageViewResource(R.id.imageWidget,R.mipmap.widgetred);

appWidgetManager.updateAppWidget(mAppWidgetId, views);

Intent resultValue = new Intent();
resultValue.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, mAppWidgetId);
setResult(RESULT_OK, resultValue);
finish();
}
});

}

最佳答案

1) 结果是我必须将 views.setOnClickPendingIntent(R.id.imageWidget, pendingIntent); 放到 appWidgetProviderappWidgetConfiguration 在行 appWidgetManager.updateAppWidget(mAppWidgetId, views); 之前。以下是我为点击监听器编写的内容:

RemoteViews views = new RemoteViews(getPackageName(),R.layout.appwidget);
Intent intent = new Intent(context, MyActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.addCategory(Intent.CATEGORY_LAUNCHER);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
views.setOnClickPendingIntent(R.id.imageWidget, pendingIntent);
appWidgetManager.updateAppWidget(mAppWidgetId, views);

2) 我可以打开具有 xml 布局的 Activity ,而无需将此代码放入目标 Activity 中:sendBroadcast(new Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_HOME))。但是 MyActivity.java 没有 xml 布局,我不得不将此代码(放入 Oncreate)以便通过单击小部件打开它。第二种选择不是将其放入目标 Activity 中,而是编写 intent.addCategory(Intent.CATEGORY_LAUNCHER); 如上所述打开该 Activity 时。

关于Android 小部件配置打开 Activity ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31426135/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com