Android: Activity not starting from a widget - android

I am trying to launch an activity from a home screen widget. However, the activity is not starting when I click the widget. Below is my code.
#Override
public void onReceive(Context context, Intent i) {
AppWidgetManager mgr = AppWidgetManager.getInstance(context);
Intent intent = new Intent(context, MyActivity.class);
PendingIntent pendingLayout = PendingIntent.getService(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widgetlayout);
views.setOnClickPendingIntent(R.id.widget, pendingLayout);
ComponentName comp = new ComponentName(context,
RecentTaskWidget.class.getName());
mgr.updateAppWidget(comp, views);
}
Thx!
Rahul.

You need to use PendingIntent.getActivity() instead of PendingIntent.getService(), probably this is why your code is not working. Hope this helps.

Related

How to start an activity by the click on an image in widget?

Intent intent = new Intent( context, ColorConfigure.class);
Intent.putExtras(AppWidgetManager.EXTRA_APPWIDGET_ID, appwidgetId );
PendingIntent pi = PendingIntent.getActivity( context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
RemoteViews r = new RemoteViews( context.getPackageName(), R.layout. activity_widget);
r.setOnClickPendingIntent(R.I'd.pic, pic);
The above code isn't working for the time i.e., when I start the widget for the first time then widget gets loaded easily but when I go onto start a new activity from the widget then nothing happens.
But whenever I re-run my app from the eclipse without removing the widget then my widget starts running successfully without any glitch.
I don't really know what sort of problem is this one?? Or if anyone is able to help me by sending me the code of a widget that start a fresh activity from a button on that activity.
even i have faced similar problem for my app homescreen widget .which is very similar to fb/twitter homescreen widget(show updates if not signed show as "your not signedin").achieved using android service. even you don't need a service. in your case you are not calling manager.updateAppWidget(ids, views);
public class NFWidgetProvider extends AppWidgetProvider {
#Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager,
int[] appWidgetIds) {
final Intent intent = new Intent(context, UpdateService.class);
context.startService(intent);
}
}
Updateservice.class
//my app code..mention service class app manifest
public void onStart(Intent intent, int startId) {
AppWidgetManager manager = AppWidgetManager.getInstance(this);
ComponentName thisWidget = new ComponentName(this,
NFWidgetProvider.class);
int[] ids = manager.getAppWidgetIds(thisWidget);
final int N = ids.length;
for (int i = 0; i < N; i++) {
int awID = ids[i];
RemoteViews views = new RemoteViews(getPackageName(),
R.layout.widget_layout);
Intent intent = new Intent(this, your intented class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
//views.setTextViewText(R.id.widgetcount, visitstext);
PendingIntent pendingIntent = PendingIntent.getActivity(this,
0, intent, 0);
views.setOnClickPendingIntent(R.id.widgetimage, pendingIntent);
manager.updateAppWidget(awID, views);
}

Unable to open activity from Widget

I an trying to open activity from widget but Unable to open activity when i click on widget button,My code is given below
public void onUpdate(Context context, AppWidgetManager appWidgetManager,
int[] appWidgetIds) {
AppWidgetManager mgr = AppWidgetManager.getInstance(context);
// retrieve a ref to the manager so we can pass a view update
Intent i = new Intent();
i.setClassName("com.avion.mychurch", "com.avion.mychurch.Welcome");
PendingIntent myPI = PendingIntent.getService(context, 0, i, 0);
// intent to start service
// Get the layout for the App Widget
RemoteViews views = new RemoteViews(context.getPackageName(),
R.layout.activity_main);
// attach the click listener for the service start command intent
views.setOnClickPendingIntent(R.id.wid_launch, myPI);
// define the componenet for self
ComponentName comp = new ComponentName(context.getPackageName(),
MainActivity.class.getName());
// tell the manager to update all instances of the toggle widget with
// the click listener
mgr.updateAppWidget(comp, views);
}
You never provide any code for actually starting an activity.
Add this and the activity should start when you click it:
Intent intent = new Intent(getApplicationContext(), YourActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(getApplicationContext(), 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
views(R.id.the_id_of_a_view_in_your_activity_main_layout, pendingIntent);

Start activity by clicking on widget

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);

Android AppWidget: new instance freezing old one

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

Update Android Widget From Activity

I have a widget, its setup so that when I click on it, it opens some settings in an activity.
views.setOnClickPendingIntent(R.id.btnActivate, pendingIntent);
This configures some settings for the application. What I want to achieve is to have the widget update its view to reflect the changed settings when the Activity I launch closes. Using the update interval or any other type of polling isn't appropriate for this.
I've seen a couple places here and in the android docs this code used:
appWidgetManager.updateAppWidget(mAppWidgetId, views);
But I don't know how to get the mAppWidgetId value. I tried following the example for a widget configuration activity here http://developer.android.com/guide/topics/appwidgets/index.html, but in the following code,
Intent intent = getIntent();
Bundle extras = intent.getExtras();
if (extras != null) {
mAppWidgetId = extras.getInt(
AppWidgetManager.EXTRA_APPWIDGET_ID,
AppWidgetManager.INVALID_APPWIDGET_ID);
}
extras is always null, so I never get the AppWidgetID.
Ok, now I'm just rambling. What do you think I can do?
I finally found the answer I was looking for, it was in an overload of the updateAppWidget function.
appWidgetManager.updateAppWidget(new ComponentName(this.getPackageName(), Widget.class.getName()), views);
This let me access the widget without having to know the appWidgetID. My final code in my activity is then:
// Create an Intent to launch ExampleActivity
Intent intent = new Intent(this, Settings.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);
views.setOnClickPendingIntent(R.id.btnActivate, pendingIntent);
AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(this);
appWidgetManager.updateAppWidget(new ComponentName(this.getPackageName(), Widget.class.getName()), views);
finish();
I have to do all the same setup stuff I had to do in the onUpdate method of the Widget, but now every time I exit my activity the Widget is displaying the correct state.
There's another way to do it - pass the widget id in the pending intent that you use to start the activity:
Intent clickIntent=new Intent(context, MyConfigActivity.class);
clickIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, widgetId);
// you have the widgetId here, since it's your onUpdate
PendingIntent pendingIntent=PendingIntent
.getActivity(context, 0,
clickIntent,
PendingIntent.FLAG_UPDATE_CURRENT);
views.setOnClickPendingIntent(R.id.btnActivate, pendingIntent);
Moreover, to avoid duplication of code from onUpdate(), you can broadcast an intent back to the AppWidgetProvider:
Intent intent = new Intent(this,MyAppWidgetProvider.class);
intent.setAction("android.appwidget.action.APPWIDGET_UPDATE");
// Use an array and EXTRA_APPWIDGET_IDS instead of AppWidgetManager.EXTRA_APPWIDGET_ID,
// since it seems the onUpdate() is only fired on that:
int[] ids = {widgetId};
intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS,ids);
sendBroadcast(intent);
I know this has been answered and accepted way ago. However while I was searching the same thing I came across an awesomely simple way to update the widget.
For future readers:
The best part, this works for all the instances of the widget and from any context (Activity, Service etc)
Heres the code,
Context context = this;
AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget_2x1);
ComponentName thisWidget = new ComponentName(context, MyWidget.class);
remoteViews.setTextViewText(R.id.my_text_view, "myText" + System.currentTimeMillis());
appWidgetManager.updateAppWidget(thisWidget, remoteViews);
Courtesy - Stuck :)
Instead of doing the call from your activity, I prefere to send a broad cast request to the widget for updating. The onUpdate method will be triggered and all widget layouts are passed.
Here is my code below:
1- sending broad cast from the activity:
Intent intent = new Intent(ctxt, MyAppWidgetProvider.class);
intent.setAction(AppWidgetManager.ACTION_APPWIDGET_UPDATE);
int[] ids = {R.layout.appwidget};
intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS,ids);
ctxt.sendBroadcast(intent);
and now, 2- implement the onUpdate method:
Intent i = new Intent(ctxt, Settings.class);
PendingIntent pi = PendingIntent.getActivity(ctxt, 0, i, 0);
for (int widgetId : allWidgetIds) {
RemoteViews remoteViews = new RemoteViews(ctxt.getPackageName(), widgetId);
remoteViews.setTextViewText(R.id.statusMsg, msg);
remoteViews.setOnClickPendingIntent(R.id.rootView, pi);
AppWidgetManager mngr = AppWidgetManager.getInstance(ctxt);
ComponentName wdgt = new ComponentName(ctxt, MyAppWidgetProvider.class);
mngr.updateAppWidget(wdgt, remoteViews);
}
That is it! I wish it helps you :)
RemoteViews view = new RemoteViews("pakagename", R.layout.widget_layout_name);
view.setTextViewText(R.id.textView_id, String.valueOf(hr + ":" + mi)); // for setting a textview
view.setCharSequence(R.id.PunchIn, "setText", "Punch In"); //for setting a button name
view.setInt(R.id.PunchIn, "setBackgroundResource", R.color.black); //for setting button color
ComponentName theWidget = new ComponentName(getActivity(), AppWidget_name.class);
AppWidgetManager manager = AppWidgetManager.getInstance(getActivity());
manager.updateAppWidget(theWidget, view);

Categories

Resources