App launch from widget if app is not running - android

I create a home screen widget whenever my app launches. But when I kill the app, and click on my widget, I need to launch the app. When the app is running in background or foreground, I must be able to do actions from the widget. My code for pending intent is -
public static PendingIntent buildButtonPendingIntent(Context context) {
++MyWidgetReceiver.clickCount;
// initiate widget update request
Intent intent = new Intent();
intent.setAction(AppWidgetManager.ACTION_APPWIDGET_UPDATE);
return PendingIntent.getBroadcast(context, 0, intent,
PendingIntent.FLAG_UPDATE_CURRENT);
//App launch if app is not running
Intent iSetting = new Intent(context, App.class);
PendingIntent piSetting = PendingIntent.getActivity(context, 0, iSetting, 0);
}
How do I do this for app killed and app launching from widget. My code is sort of incomplete.

Related

Open Activity from App-Widget, opens the last Activity

I have a function to open an Activity from the App-Widget like this:
protected PendingIntent openSettingsPedingIntent(Context context) {
Intent settingsIntent = new Intent(context, SettingsActivity.class);
settingsIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
return PendingIntent.getActivity(context, 1, settingsIntent, 0);
}
This works fine, but the App-Widget open the last Activity from my App, not the SettingsActivity, when I do the following steps:
Kill the app via task manager
Reopen the App via App-Widget
Switch from SettingsActivity to another Activity
Press the home button
When I now click on the App-Widget its open the last Activity I had open in my App, not the SettingsActivity.
Any ideas why this happen ?
Try replace 0 in PendingIntent with FLAG_UPDATE_CURRENT
Intent settingsIntent = new Intent(context, SettingsActivity.class);
settingsIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent.getActivity(context, 0, clickIntent, PendingIntent.FLAG_UPDATE_CURRENT)

Implement setOnClickPendingIntent from notification

I build custom notification that contain button and i want to listin when user press on it.
The button should not open any activity but only logic staff like change song.
the Code:
RemoteViews contentView = new RemoteViews(getPackageName(), R.layout.notification);
contentView.setTextViewText(R.id.toptext, nowPlayingTitle);
//this not work
Intent intent = new Intent(this, receiver.class);
intent.putExtra("UNIQ", "1");
PendingIntent pendingIntent = PendingIntent.getBroadcast(
this.getApplicationContext(), 234324243, intent, PendingIntent.FLAG_CANCEL_CURRENT)
contentView.setOnClickPendingIntent(R.id.imageButtonPlay,
pendingIntent);
notification.contentView = contentView;
// this is to return to my activity if click somwhere else in notification
Intent notificationIntent = new Intent(this, MYACTIVITY.class);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
notification.contentIntent = contentIntent;
mNotificationManager.notify(NOTIFICATION_ID, notification);
I don't get the hang of the setOnClickPendingIntent what need to be in the second param?
How can i just call a function after user press on the button?
im probably missing something cause i dont understand the receiver side and what happend after user press
You are missing the fact that the button you created actually doesn't belong to your application. It is created in another context, in another process. There is no way it can call your function.
Instead, when the user taps the button, that pending intent is fired. You can catch it by your receiver (in your activity), check some parameters and do the action.
Or you can implement a service and handle this intent in background. I'd prefer this way.
thanks for quick answer. I try using receiver but it never fired.
The code is in the main question and i created for the reciever class the following code:
public class receiver extends BroadcastReceiver
{
#Override
public void onReceive(Context context, Intent intent)
{
Bundle bundle = intent.getExtras();
}
}
but click on the notification never fire the receiver ( Test on debug mode )

How to start an IntentService from a press on an app widget

I would like to start an IntentService when pressing on my app widget.
I know how to update the widget with pressing on it but I don't have any idea how I would actually start an IntentService.
This is how to initiate the widget update
Intent intent = new Intent(context, MyWidgetProvider.class);
intent.setAction(AppWidgetManager.ACTION_APPWIDGET_UPDATE);
intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, appWidgetIds);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context,
0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
remoteViews.setOnClickPendingIntent(R.id.update, pendingIntent);
appWidgetManager.updateAppWidget(widgetId, remoteViews);
What would I have to change to start an INtentService instead?
ps I also started an activity based on the press, but this shows on the screen - which I do want to avoid.
Many thanks!
Use getService() instead of getBroacast(), and use an Intent that identifies your service.

Android: Start Service with Context.startService vs PendingIntent.getService

Context.startService
Intent intent = new Intent(context, MyService.class);
context.startService(intent);
PendingIntent.getService
Intent intent = new Intent(context, MyService.class);
PendingIntent pi = PendingIntent.getService(context, 0, intent, 0);
pi.send();
Questions
When would you start a service with Context.startService vs a PendingIntent?
Why would you use one over the other?
There really is no difference.
Specifically the Context method is used to directly start it where as a PendingIntent is typically used with a notification to fire this intent when it is tapped, which is delayed until the user taps it (typically). However; you wouldn't typically send the PendingIntent directly because that is not what it is for.
A PendingIntent is an Intent that is pending, pending, meaning that its NOT supposed to happen now, but in the near future. Whereas with an Intent, it is sent at the very moment.
If a PendingIntent is not pending when it is used, then it is no longer a PendingIntent and it is infact an Intent. Defeating the purpose entirely.
PendinIntents are very much used for widgets. As the layout of a running widget doesn't "belong" to your code, but it is instead under control of the system, you can't assign directly click listeners to the interface elements. Instead you assign a PendingIntent to those elements (like buttons) so when the user touches them, the PendingIntent is "executed", something like:
// get the widget layout
RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.id.widget_layout);
// prepare to listen the clicks on the refresh button
Intent active = new Intent(context, WidgetCode.UpdateService.class);
PendingIntent refreshPendingIntent = PendingIntent.getService(context, 0, active, 0);
remoteViews.setOnClickPendingIntent(R.id.buttonWidgetRefresh, refreshPendingIntent);
// send the changes to the widget
AppWidgetManager.getInstance(context).updateAppWidget(appwidgetid, remoteViews);
In this case a button in the widget starts a service. Usually you put extra info in the intent, with putExtras(), so the service will get any needed information to do its job.

Android AppWidget + android:clearTaskOnLaunch="true"

I want my app to show a login-screen every time it is opened. With android:clearTaskOnLaunch="true" everything works fine. But there is a bug which is happening only if the phone was shutdown and the app was first started with the widget.
Intent intent = new Intent(context, StartActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);
[....]
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widget_onebyone)
views.setOnClickPendingIntent(R.id.widget, pendingIntent);
This code should start my mainactivity. At first start is does. But ending the app with HOME and restart it with the widget causes my app to not start with my startactivity but with another activity of my app without showing the loginscreen.
What can I do to fix it.
I've tried
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);
But it does not work. All I want is to (re)-start the StartActivity-Intent like in startActivity(..) but I found no option to give my Widget an OnClickListener which starts a "normal" Activity.
Best regards,
Till
Have you tried adding the Flag to the intent:
Intent intent = new Intent(context, StartActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);
[....]
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widget_onebyone)
views.setOnClickPendingIntent(R.id.widget, pendingIntent);
Intent API
setFlags(int)
FLAG_ACTIVITY_CLEAR_TASK

Categories

Resources