How to launch fragment with app Widget click? - java

How can I open the fragment in app widget? here is my AppWidgetProvider class code when i tried one more time again it is working for activities but not working for fragments is there any way to open fragment with widget clicked:
public class ApprovalWidgetProvider extends AppWidgetProvider {
private Gson gson;
#Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
gson = new Gson();
for (int appWidgetId : appWidgetIds) {
Intent intent = new Intent(context, SplashActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.approval_widget);
views.setOnClickPendingIntent(R.id.example_widget_button, pendingIntent);
appWidgetManager.updateAppWidget(appWidgetId, views);
Intent intent2 = new Intent(context, MainActivity.class);
PendingIntent pendingIntent2 = PendingIntent.getActivity(context, 0, intent2, 0);
views = new RemoteViews(context.getPackageName(), R.layout.approval_widget);
views.setOnClickPendingIntent(R.id.example_widget_button2, pendingIntent2);
appWidgetManager.updateAppWidget(appWidgetId, views);
}
}

Related

onReceive not called for pendingIntent in android home screenwidget

I've been searching for a while on here, but can't find a solution to this. I have a home screen widget with a configuration screen that saves some parameters inside the preferences. The configuration part works flawlessly, but I cannot for the life of me get the setOnClickPendingIntent to register. No error messages.
AndroidManifest.xml
<receiver android:name=".TrunkWidget">
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
<action android:name="ActionTrunkButtonClicked" />
</intent-filter>
<meta-data
android:name="android.appwidget.provider"
android:resource="#xml/trunk_widget_info" />
</receiver>
My App widget provider:
public class TrunkWidget extends AppWidgetProvider {
private SharedPreferences pref;
private final String clickAction = "ActionTrunkButtonClicked";
// UPDATE FUNCTION
static void updateAppWidget(Context context, AppWidgetManager appWidgetManager,
int appWidgetId) {
// Construct the RemoteViews object
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.trunk_widget);
// Instruct the widget manager to update the widget
appWidgetManager.updateAppWidget(appWidgetId, views);
}
#Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
// There may be multiple widgets active, so update all of them
for (int appWidgetId : appWidgetIds) {
RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.trunk_widget);
// Register an onClickListener
Intent intent = new Intent(context, TrunkWidget.class);
intent.setAction(clickAction);
intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
//Set pendingIntent
PendingIntent pendingIntent = PendingIntent.getBroadcast(context,0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
remoteViews.setOnClickPendingIntent(R.id.my_btn, pendingIntent);
appWidgetManager.updateAppWidget(appWidgetId, remoteViews);
updateAppWidget(context, appWidgetManager, appWidgetId);
}
}
#Override
public void onReceive(Context context, Intent intent) {
super.onReceive(context, intent);
String action = intent.getAction();
String actionName = clickAction;
if (actionName.equals(action)) {
//DO STUFF
}
}
I'm pulling my hair on this...if anyone has any idea, I'd be grateful.
Thanks!
Nevermind. I moved this part :
// Register an onClickListener
Intent intent = new Intent(context, TrunkWidget.class);
intent.setAction(clickAction);
intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
//Set pendingIntent
PendingIntent pendingIntent = PendingIntent.getBroadcast(context,0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
remoteViews.setOnClickPendingIntent(R.id.my_btn, pendingIntent);
appWidgetManager.updateAppWidget(appWidgetId, remoteViews);
From onUpdate() to updateAppWidget() and now it works. I'm leaving this here in case someone needs it.

Widget Wont Update / Redraw

I have a widget that loaded image from SQLite. It updated whenever I add or delete item but the image is not updated. I already try to send a broadcast to AppWidgetProvider class but it didn't work too.
class that extend AppWidgetProvider
private static void updateAppWidget(Context context, AppWidgetManager appWidgetManager, int appWidgetId) {
Intent intent = new Intent(context, StackWidgetService.class);
intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
intent.setData(Uri.parse(intent.toUri(Intent.URI_INTENT_SCHEME)));
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.fav_movie_widget);
views.setRemoteAdapter(R.id.stack_view, intent);
views.setEmptyView(R.id.stack_view, R.id.empty_view);
Intent toastIntent = new Intent(context, FavMovieWidget.class);
toastIntent.setAction(FavMovieWidget.TOAST_ACTION);
toastIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
intent.setData(Uri.parse(intent.toUri(Intent.URI_INTENT_SCHEME)));
PendingIntent toastPendingIntent = PendingIntent.getBroadcast(context, 0, toastIntent, PendingIntent.FLAG_UPDATE_CURRENT);
views.setPendingIntentTemplate(R.id.stack_view, toastPendingIntent);
appWidgetManager.updateAppWidget(appWidgetId, views);
Log.e(TAG, "onUpdate2: start" );
}
#Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
Log.e(TAG, "onUpdate: start" );
for (int appWidgetId : appWidgetIds) {
updateAppWidget(context, appWidgetManager, appWidgetId);
}
}
activity for update
Context context = getApplicationContext();
AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
ComponentName thisWidget = new ComponentName(context, FavMovieWidget.class);
int[] appWidgetIds = appWidgetManager.getAppWidgetIds(thisWidget);
appWidgetManager.notifyAppWidgetViewDataChanged(appWidgetIds, R.id.stack_view);
what i already try
Intent intent = new Intent(this, FavMovieWidget.class);
intent.setAction(appWidgetManager.ACTION_APPWIDGET_UPDATE);
int[] ids = appWidgetManager.getAppWidgetIds(new ComponentName(getApplication(), FavMovieWidget.class));
intent.putExtra(appWidgetManager.EXTRA_APPWIDGET_IDS, ids);
sendBroadcast(intent);
the widget image not update
preview widget
Done by adding this on updateAppWidget.
this will triger onGetViewFactory on my service.
Random random = new Random();
intent.setType(String.valueOf(random.nextInt(1000)));
and use sendbroadcast in my activity
Intent intent = new Intent(this, FavMovieWidget.class);
intent.setAction(appWidgetManager.ACTION_APPWIDGET_UPDATE);
int[] ids = appWidgetManager.getAppWidgetIds(new ComponentName(getApplication(), FavMovieWidget.class));
intent.putExtra(appWidgetManager.EXTRA_APPWIDGET_IDS, ids);
sendBroadcast(intent);

Android widget listview handle click

I have seen related questions for this issue but I'am unable to figure out why this is not working for me.
I am expecting for onReceive() method in Widget Provider class to be called as the list item is clicked, but nothing happens.
Some comments are included, and they show what I have been trying as well.
Relevant code:
Main app layout has ListView defined like this:
<ListView
android:id="#+id/events_list"
style="#android:style/TextAppearance.Small"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
Row is defined with RelativeLayout with two nested Linear layouts, not sure if this is important to share.
getViewAt() is implemented through view factory and here's the function:
#Override
public RemoteViews getViewAt(int position) {
if (events_ack == null) {
Log.e("LIST", "getViewAt (null) events_ack");
return null;
}
RemoteViews row = new RemoteViews(context.getPackageName(), R.layout.row);
row.setTextViewText(R.id.event_name, events_ack[position].name);
row.setTextViewText(R.id.description, events_ack[position].description);
row.setTextViewText(R.id.publicTV, (events_ack[position].is_public == 0) ? "Public: False" : "Public: True");
row.setTextViewText(R.id.timeTV, events_ack[position].time);
row.setViewVisibility(R.id.price, View.GONE);
// download and set image BONUS QUESTION- does not work through AsyncTask using future insted (images are downloaded but not shown).
row.setImageViewBitmap(R.id.image, pic);
Bundle extras = new Bundle();
extras.putInt(CasusWidgetProvider.EXTRA_WORD, events_ack[position].id);
Intent fillInIntent = new Intent();
fillInIntent.putExtras(extras);
// Make it possible to distinguish the individual on-click
// action of a given item
row.setOnClickFillInIntent(R.id.events_list, fillInIntent);
//extras.putInt(CasusWidgetProvider.EXTRA_WORD, events_ack[position].id);
//i.putExtras(extras);
//i.setAction(CasusWidgetProvider.ITEM_CLICK);
//row.setOnClickFillInIntent(R.layout.casus_widget, i); //event_name?
// ??
AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
RemoteViews rv = new RemoteViews(context.getPackageName(), R.layout.casus_widget);
appWidgetManager.updateAppWidget(appWidgetId, rv);
//appWidgetManager.updateAppWidget(appWidgetId, row);
return row;
}
Widget Provider class has onUpdate() method:
#Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
Log.w("CASUS", "UPDATE function called");
for (int i = 0; i < appWidgetIds.length; i++) {
Intent intent = new Intent(context, WidgetService.class);
intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetIds[i]);
intent.setData(Uri.parse(intent.toUri(Intent.URI_INTENT_SCHEME)));
RemoteViews rv = new RemoteViews(context.getPackageName(), R.layout.casus_widget);
rv.setRemoteAdapter(appWidgetIds[i], R.id.events_list, intent);
Intent clickIntent=new Intent(context, WidgetService.class);
clickIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetIds[i]);
clickIntent.setData(Uri.parse(intent.toUri(Intent.URI_INTENT_SCHEME)));
clickIntent.setAction(ITEM_CLICK);
PendingIntent clickPI = PendingIntent.getActivity(context, 0, clickIntent, PendingIntent.FLAG_UPDATE_CURRENT);
rv.setPendingIntentTemplate(R.id.events_list, clickPI);
appWidgetManager.notifyAppWidgetViewDataChanged(appWidgetIds[i], R.id.events_list); //?
clickIntent = new Intent(context, CasusWidgetProvider.class);
clickIntent.setAction(UPDATE_LIST);
clickIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, appWidgetIds);
PendingIntent pendingIntentRefresh = PendingIntent.getBroadcast(context, 0, clickIntent, 0);
rv.setOnClickPendingIntent(R.id.update, pendingIntentRefresh);
appWidgetManager.updateAppWidget(appWidgetIds[i], rv);
}
fetchData(context, appWidgetIds);
super.onUpdate(context, appWidgetManager, appWidgetIds);
}
I have figured it out. onUpdate() function in widget provider class was modified to look like this:
#Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
for (int i = 0; i < appWidgetIds.length; i++) {
Intent intent = new Intent(context, WidgetService.class);
intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetIds[i]);
intent.setData(Uri.parse(intent.toUri(Intent.URI_INTENT_SCHEME)));
RemoteViews rv = new RemoteViews(context.getPackageName(), R.layout.casus_widget);
rv.setRemoteAdapter(appWidgetIds[i], R.id.events_list, intent);
appWidgetManager.updateAppWidget(appWidgetIds[i], rv);
}
fetchData(context, appWidgetIds);
super.onUpdate(context, appWidgetManager, appWidgetIds);
}
In getViewAt() function on widget factory this line:
row.setOnClickFillInIntent(R.id.events_list, fillInIntent);
Was modified to match correct ID:
row.setOnClickFillInIntent(R.id.row_layout, fillInIntent);
The click handler template is set in class that does the HTTP request, basically:
for (int widgetId : allWidgetIds) {
RemoteViews remoteViews = new RemoteViews(this.getApplicationContext().getPackageName(), R.layout.casus_widget);
Intent svcIntent = new Intent(this.getApplicationContext(), WidgetService.class);
svcIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, widgetId);
svcIntent.putExtra("events", events);
svcIntent.setData(Uri.parse(svcIntent.toUri(Intent.URI_INTENT_SCHEME)));
remoteViews.setRemoteAdapter(widgetId, R.id.events_list, svcIntent);
Intent clickIntent = new Intent(this.getApplicationContext(), CasusWidgetProvider.class);
clickIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, allWidgetIds);
clickIntent.setData(Uri.parse(intent.toUri(Intent.URI_INTENT_SCHEME)));
clickIntent.setAction(CasusWidgetProvider.ITEM_CLICK);
PendingIntent toastPendingIntent = PendingIntent.getBroadcast(this.getApplicationContext(), 0, clickIntent, PendingIntent.FLAG_UPDATE_CURRENT);
remoteViews.setPendingIntentTemplate(R.id.events_list, toastPendingIntent);
appWidgetManager.updateAppWidget(widgetId, remoteViews);
}
Hope this might help..

clickable ImageView in Widget?

I have this widget in android, that displays an image and gets updated periodically by a broadcast receiver. Now I want to give the user the ability to start a configuration screen (preference activity) by clicking on the widget. Here it goes:
The widget provider class:
public class MyProvider extends AppWidgetProvider {
#Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
ComponentName thisWidget = new ComponentName(context.getApplicationContext(), MyProvider.class);
int[] allWidgetIds = appWidgetManager.getAppWidgetIds(thisWidget);
RemoteViews rViews = new RemoteViews(context.getPackageName(), R.layout.widget_layout);
for (int widgetId : appWidgetIds) {
// setting onClickListener - at least i TRY to
Intent onClickIntent = new Intent(context, Preferences.class);
PendingIntent onClickPendingIntent = PendingIntent.getBroadcast(context, 0, onClickIntent, 0);
rViews.setOnClickPendingIntent(R.id.graph, onClickPendingIntent);
appWidgetManager.updateAppWidget(widgetId, rViews);
}
}
}
The updates of the widget are triggered from here:
public class MyReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context.getApplicationContext());
int[] allWidgetIds = intent.getIntArrayExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS);
ComponentName myWidget = new ComponentName(context.getApplicationContext(), MyProvider.class);
int[] allMyWidgetIds = appWidgetManager.getAppWidgetIds(myWidget);
for (int widgetId : allMyWidgetIds)
{
RemoteViews rViews = new RemoteViews(context.getPackageName(), R.layout.widget_layout);
// update the widgets content, i.e. picture
appWidgetManager.updateAppWidget(widgetId, rViews);
}
// schedule next update
Calendar c = Calendar.getInstance();
c.add(Calendar.SECOND, Config.UPDATE_RATE);
Date d = new Date(c.getTimeInMillis());
Intent timerIntent = new Intent(context.getApplicationContext(), TimeIntervalReceiver.class);
timerIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, allWidgetIds);
PendingIntent timerPendingIntent = PendingIntent.getBroadcast(context.getApplicationContext(), 0, timerIntent, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager aManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
aManager.set(AlarmManager.RTC, d.getTime(), timerPendingIntent);
}
}
Just to mention it: the Preference.java-Activity is registered in the Manifest and can be started correctly (I verified this by setting it as the startup activity).
Anyhow, the onclick-functionality does not work at all :(
I also tried to move the code to the "MyReceiver"-class which updates the widgets: nothing :(
I already looked at:
* Android: Clickable imageview widget
* http://www.vogella.de/articles/AndroidWidgets/article.html
However, this didn't help at all :(
anyone here who can tell me where my problem is?
Try changing this line:
PendingIntent onClickPendingIntent = PendingIntent.getBroadcast(context, 0, onClickIntent, 0);
to
PendingIntent onClickPendingIntent = PendingIntent.getActivity(context, 0, onClickIntent, 0);
PendingIntent onClickPendingIntent = PendingIntent.getActivity(context, 0, onClickIntent, 0);
Retrieve a PendingIntent that will start a new activity
http://developer.android.com/reference/android/app/PendingIntent.html#getActivity(android.content.Context, int, android.content.Intent, int)
Plz refer link for full reference

Why doesn't my app widget update in API 3 or 4?

I'm working on an android widget and it works great in API Level 5 or greater. It's not supported at all in API Level 1 or 2. It should work absolutely fine in 3 and 4 but for some reason the widget doesn't update.
The onUpdate method gets called and executes without errors; however, in 3 and 4 it doesn't change the text of the widget. I'm pretty much at a loss. Any thoughts?
#Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widget);
DataAccess helper = new DataAccess(context);
String text = helper.getCurrentText();
helper.close();
if (text != null)
views.setTextViewText(R.id.widget_text, text);
Intent intent = new Intent(context, WidgetDetailsActivity.class);
PendingIntent pending = PendingIntent.getActivity(context, 0, intent, 0);
views.setOnClickPendingIntent(R.id.widget, pending);
appWidgetManager.updateAppWidget(appWidgetIds, views);
}
Try this:
At the end of your update method you need to call the super.
#Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widget);
DataAccess helper = new DataAccess(context);
String text = helper.getCurrentText();
helper.close();
if (text != null)
views.setTextViewText(R.id.widget_text, text);
Intent intent = new Intent(context, WidgetDetailsActivity.class);
PendingIntent pending = PendingIntent.getActivity(context, 0, intent, 0);
views.setOnClickPendingIntent(R.id.widget, pending);
appWidgetManager.updateAppWidget(appWidgetIds, views);
super.onUpdate(context, appWidgetManager, appWidgetIds);
}
You need to start service on update method and put your all code in service also,here I give onReceive and service demo
context.startService(new Intent(context, UpdateService.class));
public static class UpdateService extends Service {
#Override
public void onStart(Intent intent, int startId) {
// Build the widget update for today
RemoteViews updateViews = buildUpdate(this);
// Push update for this widget to the home screen
ComponentName thisWidget = new ComponentName(this,
Widget.class);
AppWidgetManager manager = AppWidgetManager.getInstance(this);
manager.updateAppWidget(thisWidget, updateViews);
}
#Override
public IBinder onBind(Intent intent) {
return null;
}
}
#Override
public void onReceive(Context context, Intent intent) {
final RemoteViews remoteViews = buildUpdate(context);
ComponentName cn = new ComponentName(context, Widget.class);
AppWidgetManager.getInstance(context).updateAppWidget(cn, remoteViews);
super.onReceive(context, intent);
}
where buildUpdate is method for your widget updating code

Categories

Resources