I want to create a widget for an app and have already written widget provider
and configuration
classes. I just have one image in widget layout and when it is clicked, I want it to open MyActivity
Activity class. On configuration layout there is one red image, when I click that image, widget image gets changed. The Problem is when I click the widget, it does not open MyActivity
.
Where should I put views.setOnClickPendingIntent
? Into appWidgetProvider
? Into appWidgetConfiguration
? Into both?
Question 2 is: Do I have to put this code into MyActivity
in order to open it with widget click? : sendBroadcast(new Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_HOME));
Here is what I have:
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) It turns out that i have to put views.setOnClickPendingIntent(R.id.imageWidget, pendingIntent);
to both appWidgetProvider
and appWidgetConfiguration
before the line appWidgetManager.updateAppWidget(mAppWidgetId, views);
. Below is what i have written for click listener:
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) I can open my activity which has xml layout without putting this code in target activity: sendBroadcast(new Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_HOME))
. But MyActivity.java
does not have xml layout and i had to put this code (into Oncreate
) in order to open it with widget click. Second option is instead of putting this in target activity, write intent.addCategory(Intent.CATEGORY_LAUNCHER);
when opening that activity as above.