I want to open the launcher's widget picker (for example, the one we get when we long press on home screen) from my Activity. What I want to achieve is, I want to take the user to my widget so that there are more chances that he will consider adding it.
Programmatically adding the widget to home screen will be the best case. But, because that is not possible, I want to go as closer as possible to make user add the widget.
I tried the following but that only opens a dialog (not the launcher's) with all the widgets and by selecting one nothing happens.
Intent pickIntent = new Intent(AppWidgetManager.ACTION_APPWIDGET_PICK);
pickIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, 0);
startActivityForResult(pickIntent, 1234);
How to make this work? Any suggestions on this scenario are much appreciated.
It's possible to pin widget to homescreen (official doc) on API 26+:
AppWidgetManager mAppWidgetManager =
context.getSystemService(AppWidgetManager.class);
AppWidgetProviderInfo myWidgetProviderInfo = new AppWidgetProviderInfo();
ComponentName myProvider = myWidgetProviderInfo.provider;
if (mAppWidgetManager.isRequestPinAppWidgetSupported()) {
Intent pinnedWidgetCallbackIntent = new Intent( ... );
PendingIntent successCallback = PendingIntent.createBroadcast(context, 0,pinnedWidgetCallbackIntent);
mAppWidgetManager.requestPinAppWidget(myProvider, null,
successCallback.getIntentSender());
}
Related
So, for few last hours I was trying to make a proper Android widget, so far without success.
Issue is that my Intents aren't getting updated.
So basically, I have a configuration activity, when the widget is created, it works as it's supposed to be.
I also wanted to make a settings button, which will allow user to open the configuration activity and change some settings.
In particular I want to allow user to open some pdf files through the widget, when it's being set during right after creation of widget it's fine, but when it's being changed through the settings button only UI of widget (TextView showing which file is going to be opened), but the intent to open the file is not changed.
I was debugging code, file is properly chosen (that's why TextView get's updated properly) intent is supposed to be properly set, but it still opens the file chosen at beginning.
That's the part of my code:
Intent intent = getIntent(); //intent to get the widget ID
Bundle extras = intent.getExtras();
if (extras != null) {
mAppWidgetId = extras.getInt(AppWidgetManager.EXTRA_APPWIDGET_ID, AppWidgetManager.INVALID_APPWIDGET_ID);
RemoteViews views = new RemoteViews(this.getPackageName(), R.layout.wi_widget_layout); //widget layout
views.setTextViewText(R.id.button_plan, fileCleanName); //setting text of the TextView showing pdf's name, works just fine
File pdfFile = StorageModel.getFileForName(fileName, this); //method to get the file, while debugging, proper file is taken
Intent openFile = StorageModel.openFile(pdfFile, this); // Intent method, returns intent to open pdf application for the given file
openFile.putExtra("Random", Math.random() * 1000);
//I've read somewhere Android is caching intents, so I tried it but didn't help
PendingIntent pendingFile = PendingIntent.getActivity(this, 0, openFile, 0);
views.setOnClickPendingIntent(R.id.button_plan, pdfFile);
Intent settings = new Intent(this, WidgetConfigure.class).setAction(AppWidgetManager.ACTION_APPWIDGET_CONFIGURE);
//remaking intent for the settings button
settings.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID,mAppWidgetId);
PendingIntent settingsPending = PendingIntent.getActivity(this, 0, settings, 0);
views.setOnClickPendingIntent(R.id.button_settings, settingsPending);
appWidgetManager.updateAppWidget(mAppWidgetId, views);
setResult(RESULT_OK);
finish();
}
If anyone happens to come to similar issue, apparently this part of code was ok, the issue was in the method that was returning the intent to open file, even though it was very simple.
The intent to open my pdf file was made by calling
createChooser
method on my final intent, after return just normal intent everything is working as it should. I guess Android doesn't like to give a choice :D
I have made a working clock widget app in android but would like it so that the user could change just the clock face by clicking on it and going to a settings page. Is there a simple way of doing this? I am fairly new to programming for android.
You could try to use an Intent to start the settings Activity like this:
Intent intent = new Intent(context, SettingsActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);
remoteViews.setOnClickPendingIntent(R.id.widget, pendingIntent);
If you use this code in the onUpdate method of the AppWidgetProvider you wrote, it should do the trick for you.
Ofcourse, SettingsActivity.class and R.id.widget need to be replaced by whatever you have called them.
If you would also like to have the settings Activity pop up automatically when first adding the widget to the home screen, you could incorporate this in you app widget provider information XML file:
android:configure="com.example.yourapplication.SettingsActivity"
In order for this to work well, you should take a look at the Android Developer pages.
Well this is driving me crazy. I have developed an App-widget. Everything is working fine.
I have a configuration activity which launches every time a widget is added on the home screen and works beautiful. I save the user settings per widget id etc.
The widget has some buttons, one of them launches an activity with about information, the "About Activity".
The "About Activity" has a button which I want to use to launch the configuration activity for the widget id that launched the "About Activity". The reason I want to do that is because I want the user to be able to configure the contents of any instance of my widget without having it removed and added again (in order to launch the configuration activity).
The configuration activity needs the AppWidgetManager.EXTRA_APPWIDGET_ID in order to make the job (save the user settings for this specific widgetid) so I must somehow pass this extra when I 'm calling it from another activity. The obvious think to do is this:
startActivity(new Intent(context,act_configure.class).putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, ??? ));
Now my question is where is the widgetid? I found a million ways to get the widgetids (the array) but not a single clue on how to get the specific widgetid which launched the "About Activity"
Any help about this will make the hours I spent to find a solution, worth something. Thank you in advance.
p.s. Please forgive my English as they are not my native language...
Thanks to Cory Chaltron here is the solution to my problem.
In the widget provider onUpdate method I should create a "unique" intent to pass to the pending intent which handles the launch of the about activity. Because of the way Android compares Intents, passing the WidgetID in the extras IS NOT ENOUGH, you should also pass it as data to the intent in order to be unique. So here is the code:
Intent aboutIntent = new Intent(cx, act_about.class);
aboutIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, widgetIds[i]);
// Make this unique for this appWidgetId
aboutIntent.setData(Uri.withAppendedPath(Uri.parse("customuri://widget/id/"), String.valueOf(widgetID)));
PendingIntent aboutPendingIntent = PendingIntent.getActivity(cx, 0, aboutIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK), PendingIntent.FLAG_UPDATE_CURRENT);
views.setOnClickPendingIntent(R.id.cmdabout, aboutPendingIntent)
Although I answer my own question I am not accepting it because it is based on Cory's answer. Thank you all for the help...
How are you setting your widget views? I have an app where I iterate over the active widgets and configure set the RemoteView there. You could set your widget id in the onClick you are attaching to the "About" button.
final AppWidgetManager widgetManager = AppWidgetManager.getInstance(this);
final ComponentName widgetName = new ComponentName(this, WidgetProvider.class);
final int[] widgetIds = widgetManager.getAppWidgetIds(widgetName);
for (int widgetId : widgetIds) {
final RemoteViews remoteViews = new RemoteViews(getPackageName(), R.layout.widget);
// This is the important part :-D
remoteViews.findViewById(R.id.your_about_button).setOnClickListener(... a listener to start your about activity that puts the widget id in the extra like you suggest in your question ...);
widgetManager.updateAppWidget(widgetId, remoteViews);
}
I have a widget that shows various images with text below, and the same UI set up in the app itself. I want the widget to not only be able to open the app, but to open the app based on which picture is showing in the widget and then show that same image in the app. However, I am having a tough time getting this to work.
Thanks.
Let's say there are several images in the ui, you can set different Intent for each image, and these intents each will target a different activity.
This post: http://rxwen.blogspot.com/2012/10/communication-between-android-widget.html may give you some hints.
Add this code in widget.java file to launch your application. instead of MainActivity.class you can call any activity
static void updateAppWidget(Context context, AppWidgetManager appWidgetManager,
int appWidgetId) {
RemoteViews views = new RemoteViews(context.getPackageName(),R.layout.mywidget_provider);
Intent openApp = new Intent(context,MainActivity.class);
PendingIntent pIntent = PendingIntent.getActivity(context,0,openApp,0);
views.setOnClickPendingIntent(R.id.btnOpenApp,pIntent);
appWidgetManager.updateAppWidget(appWidgetId,views);
}
I'm having the hardest time figuring out how to remove home screen AppWidget's programmatically (i.e. without the user actually dragging one into the trash). As an example, consider an app that can have multiple accounts, with any number of widgets for each account - once an account is removed, widget should be deleted as well.
I've tried following an obscure example from http://www.netmite.com/android/mydroid/cupcake/frameworks/base/services/java/com/android/server/AppWidgetService.java, but that doesn't seem to even trigger OnDeleted, much less remove the AppWidget from the home screen.
Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_DELETED);
intent.setComponent(info.componentName); // references AppWidgetProvider's class
intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, widgetId);
sendBroadcast(intent);
Does anyone have any advice on how this can be accomplished? An example would be the bee's knees. Thanks.
You cannot add or remove app widgets from the home screen. Only the user can do that.
Any app widgets tied to a deleted account could show a different account, or adopt some "(account deleted)" look that would trigger the user to get rid of the app widget or reconfigure it.
I'm pretty sure this should work:
int[] appWidgetIds = appWidgetManager.getAppWidgetIds(new ComponentName("com.example",
"com.example.Widget"));
AppWidgetHost host = new AppWidgetHost(ctx, 0);
host.deleteAppWidgetId(appWidgetIds[0]);