我想为应用创建一个小部件,并且已经编写了widget provider
和configuration
个类。我只是在窗口小部件布局中有一个图像,当它被单击时,我希望它打开MyActivity
活动类。在配置布局上有一个红色图像,当我单击该图像时,窗口小部件图像会发生变化。 问题是我点击小部件时,它不会打开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();
}
});
}
答案 0 :(得分:3)
1)事实证明,我必须在行set a
之前将views.setOnClickPendingIntent(R.id.imageWidget, pendingIntent);
放到appWidgetProvider
和appWidgetConfiguration
。以下是我为点击监听器编写的内容:
appWidgetManager.updateAppWidget(mAppWidgetId, views);
2)我可以打开包含xml布局的活动,而无需将此代码放在目标活动中: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);
。但sendBroadcast(new Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_HOME))
没有xml布局,我必须将此代码(放入MyActivity.java
)以便通过窗口小部件单击打开它。第二个选项不是将其置于目标活动中,而是在打开上述活动时写入Oncreate
。