I would like my Appwidget in addition to the main function nor (Widget)images changed.
When calling the function PendingIntent pendingIntent = PendingIntent.getActivity(context, 1001, new Intent(context, Counter.class), 0); should also change the widget to the new image.
Then, when the requested activity exits, the widget will switch to the old image. The Counter.class itself has no layout - so I can still see the homescreen with the widget when activity is running. Why would that the widget displays through changing the picture that the activity is running or rather not.
But I have no idea how to do it.
My WidgetProvider code:
#Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
super.onUpdate(context, appWidgetManager, appWidgetIds);
for (int i = appWidgetIds.length; --i >= 0;) {
int appWidgetId = appWidgetIds[i];
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, new Intent(context, Counter.class), 0);
RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.count_widget);
remoteViews.setOnClickPendingIntent(R.id.countWidget, pendingIntent);
appWidgetManager.updateAppWidget(appWidgetId, remoteViews);
}
}
P.S. Sorry for my English :)
Related
This is the first time that I need to create an home widget for android.
I need to define buttons in my widget_layout to launch some activities so I have created a class WidgetProvider that extends AppWidgetProvider and shows the Widget in the Home, but I have noticed that there isn't onCreate, so I haven't understod how to exactly link the button in my layout xml with a listener that can open the desired activity.
There are many widget on the market that have buttons of this kind.
how should I do?
In widgets you have to use RemoteViews instead of View, and there is no findViewById(), and therefore no setOnClickListener(). What you can do however is use remoteViews.setOnClickPendingIntent(id, pendingIntent). Here's a minimal way to open the activity using this:
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
for (int i = 0; i < appWidgetIds.length; i++) {
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widget_layout);
Intent appIntent = new Intent(context, MyActivity.class);
appIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent appPendingIntent = PendingIntent.getActivity(context, REQUEST_CODE, appIntent, PendingIntent.FLAG_CANCEL_CURRENT);
views.setOnClickPendingIntent(R.id.widget_button, appPendingIntent);
appWidgetManager.updateAppWidget(appWidgetIds[i], views);
}
super.onUpdate(context, appWidgetManager, appWidgetIds);
}
I newbie at programming Android and I try to do a widget which has be able get some data from ISP about my account. There are a lot of unknown things how to do it, but I have did a few things - I've got a widget with configure activity, where user should type login and password. Widget stores the data in SharedPerferences, and when it's time to update widget I use a Service to start an AsyncTask to getting it from ISP an account data. Now I want to do start an activity by click on widget. I've tried all advice which I found on this site and widget can't start activity. My widget based on another widget which placed here https://github.com/Arturus/MetrikaWidget. I dont understand what and where I should change to start activity by clicking on my widget. Thanks.
UPDATE:
My update function, where I suggest I should place PendingIntent
#Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds)
{
Log.d(TAG, "onUpdate");
for (int appWidgetId : appWidgetIds)
{
updateAppWidget(context, appWidgetManager, appWidgetId);
}
/* An updateAppWidget functions looks like as:
Intent intent = new Intent(context, UpdateWidgetService.class);
intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, appWidgetIds);
context.startService(intent);
*/
Intent intent = new Intent(context, DetailedStatActivity.class);
PendingIntent pendingIntent = PendingIntent.getService(context, 0, intent, 0);
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.layout);
views.setOnClickPendingIntent(R.id.layout, pendingIntent);
ComponentName componentName = new ComponentName(context.getPackageName(), WidgetProvider.class.getName());
appWidgetManager.updateAppWidget(componentName, views);
}
Use this snippet in onUpdate() method of your widget AppWidgetProvider class:
#Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.widgetlayout);
Intent configIntent = new Intent(context, Activity.class);
PendingIntent configPendingIntent = PendingIntent.getActivity(context, 0, configIntent, 0);
remoteViews.setOnClickPendingIntent(R.id.widget, configPendingIntent);
appWidgetManager.updateAppWidget(appWidgetIds, remoteViews);
}
Here widgetlayout is name of your widget layout and R.id.widget is it's parent layout id.
Edit:
Now,I see your code that you added to your question.You would to do:
PendingIntent.getActivity(context, 0, configIntent, 0);
(that start's activity) instead of
PendingIntent.getService(...);
that attempt to starts service.Good luck.
References:
doityourselfandroid.com
helloandroid.com
Intent inet = new Intent(your_action);
inet.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent pIntentNetworkInfo = PendingIntent.getActivity(context, 2,
inet, Intent.FLAG_ACTIVITY_NEW_TASK);
remoteViews.setOnClickPendingIntent(you_component_when_the_user_pressing_this_activity_should_start, pIntentNetworkInfo);
I don't know about "Creating widget from another widget". This is out of my knowledge but I suggest you to build your own widget.
Apart from that, calling activity from widget should be using PendingIntent
Here is simple example to do it
Intent iSetting = new Intent(this, MyConfig.class);
PendingIntent piSetting = PendingIntent.getActivity(this, 0, iSetting, 0);
views.setOnClickPendingIntent(R.id.IdComponent, piSetting);
Or you might need to see this link and this link
In order to initiate the launcher activity, I have integrated the addCategory() and setAction() methods into the onUpdate() method.
Intent configIntent = new Intent(context, MainActivity.class);
configIntent.addCategory("android.intent.category.LAUNCHER");
configIntent.setAction("android.intent.action.MAIN");
PendingIntent configPendingIntent = PendingIntent.getActivity(context, Values.REQ_CODE, configIntent, PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_IMMUTABLE);
views.setOnClickPendingIntent(R.id.btn_widget, configPendingIntent);
I make a widget. I want to start with a widget for the first time start up
activity (gallery) which can be used select theme widget. And after selecting disappeared, and appeared widget as the widget HTC clock or HTC calendar. Does anyone know how to do this?
Thank
update your onUpdate method of AppWidgetProvider:
#Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager,
int[] appWidgetIds) {
super.onUpdate(context, appWidgetManager, appWidgetIds);
for (int appWidgetId : appWidgetIds) {
appid = appWidgetIds;
rview = new RemoteViews(context.getPackageName(),
R.layout.widget_layout);
Intent launchAppIntent = new Intent(context,Activityselecttheme.class);
launchAppIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID,
appWidgetId);
PendingIntent pendingActivity = PendingIntent.getActivity(context,
0, launchAppIntent, 0);
rview.setOnClickPendingIntent(R.id.widget, pendingActivity);
appWidgetManager.updateAppWidget(appWidgetId, rview);
}
}
I use the following:
#Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
for (int appWidgetId : appWidgetIds) {
RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.main);
Intent configIntent = new Intent(context, ConfigureActivity.class);
configIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
configIntent.setAction(ACTION_UPDATE);
PendingIntent configPendingIntent = PendingIntent.getActivity(context, 0, configIntent, PendingIntent.FLAG_CANCEL_CURRENT);
remoteViews.setOnClickPendingIntent(R.id.config_button, configPendingIntent);
Intent smsIntent = new Intent(context, TurboSMSWidget.class);
smsIntent.putExtra(APP_ID, appWidgetId);
smsIntent.setAction(ACTION_SEND);
PendingIntent smsPendingIntent = PendingIntent.getBroadcast(context, 0, smsIntent, PendingIntent.FLAG_CANCEL_CURRENT);
remoteViews.setOnClickPendingIntent(R.id.send_button, smsPendingIntent);
appWidgetManager.updateAppWidget(appWidgetId, remoteViews);
}
}
The widget creation works great, but every time i run a new widget the old one is disabled. I can't use buttons anymore, it is like frozen on the home screen. What's wrong with this code?
I think the problem is in PendingIntent. The new instance of PendingIntent overwrite the old one. You should use unique Intent for inialization of PendingIntent. Here is the discussions that may help you: Multiple Instances Of Widget Only Updating Last widget, How can I correctly pass unique extras to a pending intent?, android pending intent notification problem
I created a widget that works great until I restart the phone, then the widget doesn't display it is invisible but if i hold and click i can throw it in the garbage
I have a function that is called from my configure activity in my widgetprovider that does the following:
static void updateAppWidget(Context context, AppWidgetManager appWidgetManager,
int appWidgetId, int version)
{
if(savedType == -1)
savedType = version;
// android.os.Debug.waitForDebugger();
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.main);
Intent configIntent = new Intent(context, Configure.class);
configIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
Uri data = Uri.withAppendedPath(
Uri.parse(appWidgetId + "://widget/id/")
,String.valueOf(appWidgetId));
configIntent.setData(data);
PendingIntent pendingIntent = PendingIntent.getActivity
(context, 0, configIntent,
PendingIntent.FLAG_UPDATE_CURRENT);
views.setOnClickPendingIntent(R.id.MainImage,pendingIntent);
views.setImageViewResource(R.id.MainImage, lv_images[version]);
appWidgetManager.updateAppWidget(appWidgetId, views);
}
I had my onUpdate not doing anything because there is never anything to update but thinking this would be called after a phone restart i copied the code i had in the function
for(int i = 0; i < appWidgetIds.length; ++i)
{
updateAppWidget(context, appWidgetManager, appWidgetIds[i], savedType);
}
but this didn't seem to do much either.... suggestions?
When phone restart, it will call onEnable instead of onUpdate.
--update--
int[] allids = AppWidgetManager
.getInstance(Context)
.getAppWidgetIds(new ComponentName(Context, YourAppWidgetProvider.class);
This will get all ids that is under the control of YourAppWigetProvider. Read more on this