Android multiple widgets - multiple functions - android

So, I'm developing an app, that has 5 main functions. I want to make widgets for these functions - each widget provides one function. I've made the 5 layouts, the 5 provider properties xml, and the 5 AppWidgetProvider class. In each provider class I've implemented the onUpdate() method, as you can see in the code below. And sure I have registered them in the manifest file.
My problem is, when I click on one of the widgets, it calls the other widgets onUpdate method too and only the last one starts the service. How can I make the 5 widget independent?
One of the provider class:
public class myWidgetProvider extends AppWidgetProvider {
#Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
final int N = appWidgetIds.length;
for (int i = 0; i < N; i++) {
int appWidgetId = appWidgetIds[i];
String label = appWidgetManager.getAppWidgetInfo(appWidgetId).label;
Intent intent = new Intent(context, WidgetService.class);
intent.putExtra(Constants.FUNCTION, Constants.F1);
PendingIntent pendingIntent = PendingIntent.getService(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget_f1);
remoteViews.setOnClickPendingIntent(R.id.widgetF1Button, pendingIntent);
appWidgetManager.updateAppWidget(appWidgetId, remoteViews);
}
}
}
The WidgetService:
public class WidgetService extends Service {
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
Bundle bundle = intent.getExtras();
int function = bundle.getInt(Constants.FUNCTION);
switch (function) {
case Constants.F1
//function1
case Constants.F2:
//funcion2
case Constants.F3: {
//function3
case Constants.F4: {
//function4
case Constants.F5: {
//function5public class WidgetService extends Service {
}
return START_STICKY;
}
#Override
public IBinder onBind(Intent intent) {
return null;
}
}

Try using different requestCodes for every pendingIntent:
PendingIntent pendingIntent = PendingIntent.getService(context, uniqueRequestCode, intent, PendingIntent.FLAG_UPDATE_CURRENT);
It should enable android to differentiate between your pending intents and therefore all buttons will start the service, not just last one. The reason why this should work was discussed here somewhere long time ago - I can try to look it up if you are interested.

Related

Xamarin: Android Widget with timer, stops when app killed

I have this code:
public class MyWidgetProvider : AppWidgetProvider
{
public override void OnUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds)
{
Log.Debug("WIDGET", "Updating the widget");
// Open app on click
RemoteViews views = new RemoteViews(context.PackageName, Resource.Layout.MyWidget);
Intent launchAppIntent = new Intent(context, typeof(MainActivity));
PendingIntent launchAppPendingIntent = PendingIntent.GetActivity(context, 0, launchAppIntent, PendingIntentFlags.UpdateCurrent);
views.SetOnClickPendingIntent(Resource.Id.main, launchAppPendingIntent);
appWidgetManager.UpdateAppWidget(appWidgetIds[0], views);
// Start timer
System.Timers.Timer timer = new System.Timers.Timer();
timer.Interval = 1000;
timer.Elapsed += OnTimedEvent;
timer.Enabled = true;
}
private void OnTimedEvent(object sender, ElapsedEventArgs e)
{
Log.Debug("WIDGET", "Updating status...");
new Handler(Looper.MainLooper).Post(() =>
{
//Run my code to periodically update the widget
});
}
}
And I would like to know why following occurs:
When I drop the widget on phone screen, the timer starts to run, this is ok.
When I click on the widget the app starts, timer continues to run, this is ok.
When I click on back button the app goes to background, timer continues to run, this is ok.
When I terminate the app in task manager the timer stops, this is bad.
When I click on the widget again the app starts but the timer does not resume operation, this is bad.
The timer resumes operation only when next OnUpdate is called (I have the lowest possible interval 30 minutes), this is bad because I need frequent updating when the screen is on (or better when the widget is visible to the user).
I would like know the basics here as I could not find any relevant information. Why the timer runs when I first drop the widget on screen (without running app) and stops when the app gets killed?
Yes I have read almost everything about widget basics, then about using AlarmManager, Service, JobService, JobIntentService, JobScheduler etc. But I am interested in this solution with timer as it is very simple and works across all present Android versions (even newest Oreo). Things to solve yet are to stop the timer when the screen goes off and start it again when it goes on. To save the phone battery.
This is how I solved it:
public static class WidgetConsts
{
public const string DebugTag = "com.myapp.WIDGET";
public const string ActionWakeup = "com.myapp.WIDGET_WAKEUP";
public const string ActionWidgetUpdate = "android.appwidget.action.APPWIDGET_UPDATE";
public const string ActionWidgetDisabled = "android.appwidget.action.APPWIDGET_DISABLED";
}
[BroadcastReceiver]
[IntentFilter(new string[] { WidgetConsts.ActionWakeup })]
public class AlarmReceiver : BroadcastReceiver
{
public override void OnReceive(Context context, Intent intent)
{
if (intent.Action.Equals(WidgetConsts.ActionWakeup))
{
Log.Debug(WidgetConsts.DebugTag, "Wakeup alarm called");
if (MyWidgetProvider.widgetTimer == null)
{
Log.Debug(WidgetConsts.DebugTag, "Widget updating does not run, enforcing update...");
MyWidgetProvider.UpdateAppWidget(context);
}
else
{
Log.Debug(WidgetConsts.DebugTag, "Widget updating runs, no action needed");
}
}
}
}
[BroadcastReceiver]
[IntentFilter(new string[] { WidgetConsts.ActionWidgetUpdate })]
[IntentFilter(new string[] { WidgetConsts.ActionWidgetDisabled })]
[MetaData("android.appwidget.provider", Resource = "#xml/widget_info")]
public class MyWidgetProvider : AppWidgetProvider
{
public static System.Timers.Timer widgetTimer = null;
public override void OnUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds)
{
Log.Debug(WidgetConsts.DebugTag, "Updating the widget");
// Open app on click
RemoteViews views = new RemoteViews(context.PackageName, Resource.Layout.MyWidget);
Intent launchAppIntent = new Intent(context, typeof(MainActivity));
PendingIntent launchAppPendingIntent = PendingIntent.GetActivity(context, 0, launchAppIntent, PendingIntentFlags.UpdateCurrent);
views.SetOnClickPendingIntent(Resource.Id.main, launchAppPendingIntent);
appWidgetManager.UpdateAppWidget(appWidgetIds[0], views);
// set timer for updating the widget views each 5 sec
if (widgetTimer == null)
{
widgetTimer = new System.Timers.Timer();
widgetTimer.Interval = 5000;
widgetTimer.Elapsed += OnTimedEvent;
}
widgetTimer.Enabled = true;
// set alarm to wake up the app when killed, each 60 sec
// needs a fresh BroadcastReceiver because AppWidgetProvider.OnReceive is
// not virtual and overriden method in this class would not be called
AlarmManager am = (AlarmManager)context.GetSystemService(Context.AlarmService);
Intent ai = new Intent(context, typeof(AlarmReceiver));
ai.SetAction(WidgetConsts.ActionWakeup);
PendingIntent pi = PendingIntent.GetBroadcast(context, 0, ai, PendingIntentFlags.CancelCurrent);
am.SetRepeating(AlarmType.ElapsedRealtime, SystemClock.ElapsedRealtime(), 1000 * 60, pi);
}
public override void OnDisabled(Context context)
{
Log.Debug(WidgetConsts.DebugTag, "Disabling the widget");
if (widgetTimer != null)
{
Log.Debug(WidgetConsts.DebugTag, "Stopping timer");
widgetTimer.Enabled = false;
}
else
Log.Debug(WidgetConsts.DebugTag, "Timer is null");
base.OnDisabled(context);
}
private void OnTimedEvent(object sender, ElapsedEventArgs e)
{
Log.Debug(WidgetConsts.DebugTag, "Updating status...");
new Handler(Looper.MainLooper).Post(() =>
{
//Run my code to periodically update the widget
RemoteViews views = new RemoteViews(Application.Context.PackageName, Resource.Layout.MyWidget);
AppWidgetManager manager = AppWidgetManager.GetInstance(Application.Context);
ComponentName thisWidget = new ComponentName(Application.Context, Java.Lang.Class.FromType(typeof(MyWidgetProvider)));
int[] appWidgetIds = manager.GetAppWidgetIds(thisWidget);
views.SetTextViewText(Resource.Id.myText, "my text");
manager.UpdateAppWidget(appWidgetIds[0], views);
});
}
static public void UpdateAppWidget(Context context)
{
Intent intent = new Intent(context, typeof(MyWidgetProvider));
intent.SetAction(WidgetConsts.ActionWidgetUpdate);
int[] ids = AppWidgetManager.GetInstance(context).GetAppWidgetIds(new ComponentName(context, Java.Lang.Class.FromType(typeof(MyWidgetProvider))));
intent.PutExtra(AppWidgetManager.ExtraAppwidgetIds, ids);
context.SendBroadcast(intent);
}
}
Pros:
Simple solution, works on all Android systems (tested on 3.2, 4.3, 8.1).
Battery friendly on Android systems >= 6.0 with doze mode (measured with GSam Battery monitor). Not restricted by the new background execution limits in >=8.0.
Cons:
Drains battery on systems below 6.0 without doze mode, but no one cares about these today...
First,You can try to make the Widget app not be skilled.
The widget itself will not be killed. The widget is originally a broadcastreciver, and it is static. This means that a subscribed broadcast widget can be received at any time, and the onReceive() method will be called. The reason why widgets can't be run is that they should be killed for the corresponding service. If want the widget to run all the time, the service should when be killed and be restarted.
Service is a component of the Android system, it is similar to the level of Activity, but he can not run by himself, can only run in the background, and can interact with other components.
In the Android development process, each time the startService (Intent) is called, the OnStartCommand(Intent, int, int) method of the Service object is called, and then some processing is done in the onStartCommand method.
1,Create the servide not be killed
#Override
public int onStartCommand(Intent intent, int flags, int startId)
{
return START_STICKY_COMPATIBILITY;
//return super.onStartCommand(intent, flags, startId);
}
#Override
public int onStartCommand(Intent intent, int flags, int startId)
{
flags = START_STICKY;
return super.onStartCommand(intent, flags, startId);
// return START_REDELIVER_INTENT;
}
#Override
public void onStart(Intent intent, int startId)
{
// again regsiter broadcast
IntentFilter localIntentFilter = new IntentFilter("android.intent.action.USER_PRESENT");
localIntentFilter.setPriority(Integer.MAX_VALUE);// max int
myReceiver searchReceiver = new myReceiver();
registerReceiver(searchReceiver, localIntentFilter);
super.onStart(intent, startId);
}
2,Restart the Service in the Service's onDestroy().
public void onDestroy()
{
Intent localIntent = new Intent();
localIntent.setClass(this, MyService.class); // restart Service
this.startService(localIntent);
}
3,create a broadcast and regsiter in XML
public class myReceiver extends BroadcastReceiver
{
#Override
public void onReceive(Context context, Intent intent)
{
context.startService(new Intent(context, Google.class));
}
}
<receiver android:name=".myReceiver" >
<intent-filter android:priority="2147483647" ><!--Priority plus highest-->
<!-- when applicayion lauch invoke -->
<action android:name="android.intent.action.BOOT_COMPLETED" />
<!-- unlock invole -->
<action android:name="android.intent.action.USER_PRESENT" />
<!--context switch -->
<action android:name="android.media.RINGER_MODE_CHANGED" />
</intent-filter>
</receiver>
<service android:name=".MyService" >
Note: Unlock, start, switch scene activation broadcast needs to add permissions, such as startup completion, and mobile phone status.
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
==================================================================
Second, If Widget app not be skilled, you can listen to screen is lock or unlock.
Custom a ScreenListener and add ScreenBroadcastReceiver
private class ScreenBroadcastReceiver extends BroadcastReceiver {
private String action = null;
#Override
public void onReceive(Context context, Intent intent) {
action = intent.getAction();
if (Intent.ACTION_SCREEN_ON.equals(action)) { // screen on
mScreenStateListener.onScreenOn();
} else if (Intent.ACTION_SCREEN_OFF.equals(action)) { // screen off
mScreenStateListener.onScreenOff();
} else if (Intent.ACTION_USER_PRESENT.equals(action)) { // screen unlock
mScreenStateListener.onUserPresent();
}
}
}
so that you can do with Timers or other showing with customer.
==============================================================================
More info:
This method not the best, there are more places to improve,just give a suggestion.

Widget refresh from inside application

Good Day! I want to add text from inside application to widget, i have a main activity and it has list view and lots of text contents and it has a button to add the text to widget via shared preference, it's works fine when i close the widget and recreate it only, otherwise it's not automatically refresh.if anyone know; how to solve this please help me. here i attached the widget code below.
public class WidgetMaster extends AppWidgetProvider {
static void updateAppWidget(Context context, AppWidgetManager appWidgetManager,
int appWidgetId) {
Intent intentHome = new Intent(context, MainActivity.class);
PendingIntent pendingIntentHome = PendingIntent.getActivity(context, 0, intentHome, 0);
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widget_master);
views.setOnClickPendingIntent(R.id.wid_home, pendingIntentHome);
final SharedPreferences sharedPreferencestoWi = context.getSharedPreferences(String.valueOf(R.string.addTextToWidgetPref), MODE_PRIVATE);
String forWidget = sharedPreferencestoWi.getString("textToWidget", "");
String dum = "add from reading";
if(forWidget.equals("")){
views.setTextViewText(R.id.dum_appwidget_text, dum);
appWidgetManager.updateAppWidget(appWidgetId, views);
} else {
views.setTextViewText(R.id.appwidget_text, forWidget);
views.setViewVisibility(R.id.appwidget_text, 0);
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) {
updateAppWidget(context, appWidgetManager, appWidgetId);
}
}
#Override
public void onEnabled(Context context) {
// Enter relevant functionality for when the first widget is created
}
#Override
public void onDisabled(Context context) {
// Enter relevant functionality for when the last widget is disabled
}
}
How do you update the widget from mainActivity?
In the widget configuration file you can specify a fixed update interval. The smallest update interval is 1800000 milliseconds (30 minutes).
But its better to update widget programmatically, to do this You should send a broadCast to update widget and use a
method like alarmManager or Handler for a repeating task for example you can use the following broadCast to update widget form mainActivity:
Intent intent = new Intent(this, WidgetMaster.class);
intent.setAction(AppWidgetManager.ACTION_APPWIDGET_UPDATE);
int[] ids = AppWidgetManager.getInstance(getApplication())
.getAppWidgetIds(new ComponentName(getApplication(),WidgetMaster.class));
intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, ids);
sendBroadcast(intent);

Android working with views in widget

I am trying to make a widget for my app, and I want it to be with a buttonm progress bar and text view, which would later be changed, so I need to somehow work with the views themselves, like I can from an activity, show a view, hide a view, change a button background etc...
I tried tweaking with the sample code from the Android documentations, but I can only start an activity with this
public class WidgetProvider extends AppWidgetProvider {
#Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
super.onUpdate(context, appWidgetManager, appWidgetIds);
final int N = appWidgetIds.length;
for (int i = 0; i < N; i++) {
int appWidgetId = appWidgetIds[i];
Intent intent = new Intent(context, ActivityMain.class);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widget_layout);
views.setOnClickPendingIntent(R.id.sync_button, pendingIntent);
appWidgetManager.updateAppWidget(appWidgetId, views);
}
}
#Override
public void onReceive(Context context, Intent intent) {
super.onReceive(context, intent);
}
}
Does the widget need to be connected to an activity in order to make actions (connect to the internet, write in the database, write in the objects of the apps instance (if there is one))? And can I work with views the way I am asking?
Outside of setting on-click PendingIntents on views, there isn't much you can do inside of a widget. The standard recipe is:
Make a PendingIntent that sends a broadcast (PendingIntent.getBroadcast()) and set it on the appropriate view in your widget.
In the BroadcastReceiver that receives the intent, you update the widget to show a spinner and start a Service to do whatever long-running work you want to do (e.g connect to the internet).
Once that work is done, you can update your widget again and remove the spinner.

Android adding more widgets with different content

I am trying to create an application with a widget. When the user places the widget on the desktop a listview should come up with a list of items. The user selects an item then the widget is created with the respective text related to that item. For simplicity I am using now an alertdialog. The input text should appear only in the widget I create. It should not appear in the other widgets. How am I supposed to do that? I have not found anyhing about this.
The AppWidgetProvider:
public class MyWidgetProvider extends AppWidgetProvider {
private static final String ACTION_CLICK = "ACTION_CLICK";
#Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
Log.i("INFO", "AppWidgetProvider");
ComponentName thisWidget = new ComponentName(context, MyWidgetProvider.class);
int[] allWidgetIds = appWidgetManager.getAppWidgetIds(thisWidget);
//Intent intent = new Intent(context.getApplicationContext(), UpdateWidgetService.class);
//intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, allWidgetIds);
//context.startService(intent);
Intent intentA = new Intent(context.getApplicationContext(), NoteListToWidget.class);
intentA.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intentA);
}
}
The service:
public class UpdateWidgetService extends Service {
public static String ACTION_WIDGET_CONFIGURE = "ConfigureWidget";
public static String ACTION_WIDGET_RECEIVER = "ActionReceiverWidget";
String value;
Dialog dialog_newitem;
#Override
public void onStart(Intent intent, int startId) {
Log.i("INFO", "UpdateWidgetService");
AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(this.getApplicationContext());
int[] allWidgetIds = intent.getIntArrayExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS);
ComponentName thisWidget = new ComponentName(getApplicationContext(), MyWidgetProvider.class);
int[] allWidgetIds2 = appWidgetManager.getAppWidgetIds(thisWidget);
for (int widgetId : allWidgetIds) {
int number = (new Random().nextInt(100));
String v = GlobalVars.getValue();
RemoteViews remoteViews = new RemoteViews(this.getApplicationContext().getPackageName(), R.layout.widget_layout);
//remoteViews.setTextViewText(R.id.update, "Random: " + String.valueOf(number));
remoteViews.setTextViewText(R.id.update, v);
Intent configIntent = new Intent(this, Notepad.class);
configIntent.setAction(ACTION_WIDGET_CONFIGURE);
PendingIntent configPendingIntent = PendingIntent.getActivity(this, REQUEST_CODE_ONE, configIntent, PendingIntent.FLAG_UPDATE_CURRENT);
remoteViews.setOnClickPendingIntent(R.id.widgetlayout, configPendingIntent);
appWidgetManager.updateAppWidget(widgetId, remoteViews);
}
stopSelf();
super.onStart(intent, startId);
}
#Override
public IBinder onBind(Intent intent) {
return null;
}
}
This is the Activity with the AlertDialog (the latter listview):
public class NoteListToWidget extends Activity {
Dialog dialog_newitem;
EditText et_newitem;
String value;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
dialog_newitem = new Dialog(NoteListToWidget.this);
dialog_newitem.setContentView(R.layout.dialog_productlists_grp_capitalized);
dialog_newitem.setTitle("Select");
dialog_newitem.setCancelable(true);
et_newitem = (EditText) dialog_newitem.findViewById(R.id.et_item_name);
Button btn_Save = (Button) dialog_newitem.findViewById(R.id.btn_save_pr);
btn_Save.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
value = et_newitem.getText().toString();
GlobalVars.setValue(value);
dialog_newitem.dismiss();
/*
Intent serviceIntent = new Intent();
serviceIntent.setAction("com.myapp.app.UpdateWidgetService");
startService(serviceIntent);
*/
Intent intent = new Intent(NoteListToWidget.this, com.myapp.app.UpdateWidgetService.class);
startService(intent);
}
});
Button btn_Cancel = (Button) dialog_newitem.findViewById(R.id.btn_cancel_pr);
btn_Cancel.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
dialog_newitem.dismiss();
}
});
dialog_newitem.show();
}
}
I don't how and from where should I call the NoteListToWidget activity and how to make this whole thing work.
First of all it is impossible to answer your question in full. This is because it seems you are missing a lot of things about how app-widgets are working, so you must read more about them. However I will give you a place to start.
The onUpdate method of the AppWidgetProvider is called each time an app-widget that it is already placed on the desktop needs an update. That is, even if you manage to display your list from there and the user cancels the operation there is NO way to remove your app-widget from the onUpdate method.
Furthermore when the onUpdate method finishes the AppWidgetProvider is destroyed, so even if you manage to display your list from there there is NO way to get the items that user selected. Long story short, you canNOT do it from onUpdate method and you canNOT do it by this approach.
However, you CAN do it by using a configuration activity in a specified point of the app-widget's creation process but this cannot be answered in the context of your question because you must first understand how the app-widget system works in Android. The best place to start is this link. I had the same problems with my first app-widget application (and almost the same concerns with you) but then I started to read and read, finally after 1.5 months I 've managed to make something like the thing you want to do. So don't say
How am I supposed to do that? I have not found anyhing about this
and start reading, this is the life with Android. It's all in the above link, there are also very good open-source projects in google code like this weather widget which allows the user to configure it BEFORE it is placed on the desktop.
Hope this helps...

ViewFlipper in app widgets

I'm playing around building a new widget and was looking at the Android app widget documentation, in particular the section on which widget classes were supported. I noticed that ViewFlipper is supported, but I'm struggling to find any examples on how to use one in a home screen widget. In particular, I'm wondering if its possible to manually trigger swapping views. In an activity this looks relatively straightforward, one example being hooking up the onclick listener of a button to call the showNext() of the flipper.
The RemoteViews object has showNext and showPrevious methods but I'm not sure I understand how to hook them up to an event fired from the user interacting with the widget. Can anyone provide examples of when these methods might be called?
It looks like buttons in widgets can only be wired up to intents rather than code to exercise the flipper. If this restriction is true, then is the only use of a view flipper in an app widget for auto flipping of views?
Say you have 2 buttons: LEFT and RIGHT. First you would attach pending intent to each (here it's triggered from Service#onStart:
#Override
public void onStart(Intent intent, int startId) {
AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(this.getApplicationContext());
int[] allWidgetIds = intent.getIntArrayExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS);
// add listeners for every widget registered
for (int widgetId : allWidgetIds) {
addClickListeners(appWidgetManager, widgetId, root);
}
stopSelf();
}
protected void addClickListeners(AppWidgetManager appWidgetManager, int widgetId, RemoteViews root) {
root.setOnClickPendingIntent(R.id.left, getNavigationIntent(widgetId, R.id.left));
root.setOnClickPendingIntent(R.id.right, getNavigationIntent(widgetId, R.id.right));
}
protected PendingIntent getNavigationIntent(int widgetId, final int id) {
Intent clickIntent = new Intent(this, WidgetProvider.class);
clickIntent.setAction(AppWidgetManager.ACTION_APPWIDGET_UPDATE);
clickIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, widgetId);
clickIntent.putExtra(TRIGGER, id);
PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), 0, clickIntent,
PendingIntent.FLAG_UPDATE_CURRENT);
return pendingIntent;
}
Then, in AppWidgetProvider do
#Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
Bundle extras = intent.getExtras();
Integer id = (Integer) (extras == null ? null : extras.get(TRIGGER));
if (AppWidgetManager.ACTION_APPWIDGET_UPDATE.equals(action) && id != null) {
int widgetId = extras.getInt(AppWidgetManager.EXTRA_APPWIDGET_ID, 0);
onNavigate(context, widgetId, id);
} else {
super.onReceive(context, intent);
}
}
protected void onNavigate(Context context, Integer widgetId, Integer id) {
AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
RemoteViews root = new RemoteViews(context.getPackageName(), R.layout.app_widget);
if (id == R.id.left) {
root.showPrevious(R.id.scroll);
} else {
root.showNext(R.id.scroll);
}
appWidgetManager.updateAppWidget(widgetId, root);
}
This should do it. Now the problem is - this will only work in API 11+ and I just found the hard way that root.setInt(R.id.scroll, "setDisplayedChild", pos) will not work in API 7.

Categories

Resources