Receiving more shared preferences than should exist when creating new widget - android

I am constructing widgets which give you the best currency rate for the values you choose. On the homePage, when I click on widgets and select the widget I want to create, the WidgetConfigure Application should launch.
However, it crashes before the configure page even launches and I get this error:
java.lang.RuntimeException:java.lang.IndexOutOfBoundsException: Index: 2, Size: 2. This is the code it refers to:
// Set the currencies for each object
for(String currency: preferredCurrencies){
currencyObjects.get(currencyCount).setCurrencyType(currency);
currencyCount+=1;
}
The code is in one of my widget methods responsible for updating it.
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int appWidgetId, List<CurrencyObject> currencyObjects, Intent clickIntent) {
theAppWidgetManager = appWidgetManager;
// There may be multiple widgets active, so update all of them
// Get the preferred Currencies
Set<String> preferredCurrencies = AppWidgetConfigure.loadCurrencyPref(context,appWidgetId);
// Inflate the layout
RemoteViews view = new RemoteViews(context.getPackageName(), layout);
// if the preferred Currencies have been declared already
if(preferredCurrencies!= null){
// Set the currencies for each object
*for(String currency: preferredCurrencies){
currencyObjects.get(currencyCount).setCurrencyType(currency);
currencyCount+=1;
}*
}
else{
for(CurrencyObject curObj:currencyObjects){
curObj.setCurrencyType("EUR");
}
}
currencyCount = 0;
}
In my widget configure class I have these methods, where I set the preferredCurrencies:
private static final String PREFS_NAME = "change.Widgets";
private static final String PREF_PREFIX_KEY = "appwidget_";
int mAppWidgetId = AppWidgetManager.INVALID_APPWIDGET_ID;
private List<String> currencies = new ArrayList<>();
private int checkCounter;
private Set<String> chosenCurrencies = new TreeSet<>();
static void saveCurrencyPref(Context context, int appWidgetId, Set<String> chosenCurrencies) {
SharedPreferences.Editor prefs = context.getSharedPreferences(PREFS_NAME, 0).edit();
prefs.putStringSet(PREF_PREFIX_KEY + appWidgetId, chosenCurrencies);
prefs.apply();
}
static void deleteCurrencyPref(Context context, int appWidgetId) {
SharedPreferences.Editor prefs = context.getSharedPreferences(PREFS_NAME, 0).edit();
prefs.remove(PREF_PREFIX_KEY + appWidgetId);
prefs.apply();
}
public static Set<String> loadCurrencyPref(Context context, int appWidgetId) {
SharedPreferences prefs = context.getSharedPreferences(PREFS_NAME, 0);
Set chosenCurrencies = prefs.getStringSet(PREF_PREFIX_KEY + appWidgetId, null);
return chosenCurrencies;
}
#Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
// Find the widget id from the intent.
Intent intent = getIntent();
Bundle extras = intent.getExtras();
if (extras != null) {
mAppWidgetId = extras.getInt(
AppWidgetManager.EXTRA_APPWIDGET_ID, AppWidgetManager.INVALID_APPWIDGET_ID);
}
// If this activity was started with an intent without an app widget ID, finish with an error.
if (mAppWidgetId == AppWidgetManager.INVALID_APPWIDGET_ID) {
finish();
return;
}
// Set the result to CANCELED. This will cause the widget host to cancel
// out of the widget placement if the user presses the back button.
setResult(RESULT_CANCELED);
// Create the layout with the checkboxes and the Button.
setContentView(R.layout.widget_configure);
LinearLayout ll = (LinearLayout) findViewById(R.id.configure_layout);
TextView txt = new TextView(this);
txt.setText("Must have: " + Integer.toString(checkBoxLimit) + " checkboxes");
ll.addView(txt);
// Create the checkboxes
currencies.addAll(Arrays.asList(getResources().getStringArray(R.array.currency_array)));
for(String item:currencies){
CheckBox ch = new CheckBox(this);
ch.setText(item);
ll.addView(ch);
ch.setOnCheckedChangeListener((cb, isChecked)->{
//If it's checked and more than the allowed limit, don't consider it
if(isChecked){
if(checkCounter>=checkBoxLimit){
cb.setChecked(false);
Toast.makeText(this, txt.getText(), Toast.LENGTH_SHORT).show();
}
// If it's within the allowed limit, add to list of chosenCurrencies.
else{
checkCounter+=1;
chosenCurrencies.add(cb.getText().toString());
}
}
// If its, unchecked remove the currency from the list of chosenCurrencies.
else{
checkCounter-=1;
chosenCurrencies.remove(cb.getText().toString());
}
});
}
// Create the button
Button btn = new Button(this);
btn.setText(R.string.apply);
ll.addView(btn);
// Finish this
//Launch the widget once the button is pressed
btn.setOnClickListener(v->{
//If User selects right amount of checkboxes
if(checkBoxLimit == checkCounter){
final Context context = AppWidgetConfigure.this;
// delete the previous currencies that existed there for that widget Id
deleteCurrencyPref(context, mAppWidgetId);
// Save the preferences
saveCurrencyPref(context, mAppWidgetId, chosenCurrencies);
// It is the responsibility of the configuration activity to update the app widget
AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
int[] oneIdList = new int[1];
oneIdList[0] = mAppWidgetId;
//Update the current type of widget
widget.onUpdate(context, appWidgetManager, oneIdList);
// Make sure we pass back the original appWidgetId
Intent resultValue = new Intent();
resultValue.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, mAppWidgetId);
setResult(RESULT_OK, resultValue);
finish();
}
else{
Toast.makeText(this, txt.getText(), Toast.LENGTH_SHORT).show();
}
});
}

// Set the currencies for each object
for (String currency : preferredCurrencies) {
if (currencyCount >= currencyObjects.size()) {
return
}
currencyObjects.get(currencyCount).setCurrencyType(currency);
currencyCount++;
}
your app crash because you want to get a invalid item of list. ex your list have 2 items but you call list.get(2)

Related

Android Material Dialog and Store Selected Value

My application is related to alarm. I used material AlertDialog in my application. I choose one value from AlertDialog and press choose button. I want to store that selected values. It value sets until I do not changed it. How do I?
final Dialog dialog = new MaterialDialog.Builder(Settings_Activity.this)
.title(R.string.full_battery_alarm1)
.iconRes(R.mipmap.ic_launcher)
.limitIconToDefaultSize()
.items(R.array.full_battery_alarm)
.itemsCallbackSingleChoice(0, new MaterialDialog.ListCallbackSingleChoice() {
#Override
public boolean onSelection(MaterialDialog mdialog, View view, int pos, CharSequence text) {
manager.sessionWork(pos);
showToast(pos + " = " + text);
int i = Integer.parseInt(text.toString());
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(Settings_Activity.this);
SharedPreferences.Editor editor = preferences.edit();
editor.putString("select full", Integer.toString(i));
editor.apply();
String abc = preferences.getString("select full", null);
final int l = Integer.parseInt(abc);
BroadcastReceiver batteryLevelReceiver = new BroadcastReceiver() {
public void onReceive(Context context, Intent intent) {
context.unregisterReceiver(this);
int rawlevel = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);
int scale = intent.getIntExtra(BatteryManager.EXTRA_SCALE, -1);
if (rawlevel >= 0 && scale > 0) {
level = (rawlevel * 100) / scale;
showToast("level full" + level);
if (level == l) {
showToast("level full 2"+level);
Intent i1 = new Intent(Settings_Activity.this, StopActivity.class);
i1.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(i1);
finish();
}
}
}
};
IntentFilter batteryLevelFilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
registerReceiver(batteryLevelReceiver, batteryLevelFilter);
mdialog.setSelectedIndex(pos);
return true;
}
})
.positiveText(R.string.choose)
.show();
Store it in shared preference or just use a db line sql lite
You can use SharedPreferences like this. Create your SharedPreferences and Editor.
static SharedPreferences sharedData;
static SharedPreferences.Editor editor;
now
sharedData = getSharedPreferences("mySharedPreference", Context.MODE_PRIVATE);
editor = sharedData.edit();
after this to save your value do
editor.putString("valueKey", ""+yourvaluevariable); editor.commit();
Now this value is saved and will be retrieved event when you will start the app later after closing it.
To access that value afterwards use
sharedData.getString("valueKey", "");

How can I save and restore AndroidWidgetHostViews

I'm trying to build a custom launcher app. I'm stuck at restoring widgets when activity is restarted.
After picking a widget the widgetID is stored to shared prefs and in onCreate I'm trying to restore the previous set widgets. I was following this tutorial.
AppWidgetManager mAppWidgetManager;
AppWidgetHost mAppWidgetHost;
ViewGroup hostView;
int numWidgets = 0;
/**
* Called on the creation of the activity.
*/
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_dashboard);
hostView = (ViewGroup) findViewById(R.id.LAYOUT_DASHBOARD);
hostView.setOnLongClickListener(new View.OnLongClickListener() {
#Override
public boolean onLongClick(View view) {
if(numWidgets < 4) {
selectWidget();
}else {
removeAllWidgets();
numWidgets = 0;
}
return false;
}
});
mAppWidgetManager = AppWidgetManager.getInstance(this);
mAppWidgetHost = new AppWidgetHost(this, R.id.APPWIDGET_HOST_ID);
restoreWidgets();
}
/**
* Launches the menu to select the widget. The selected widget will be on
* the result of the activity.
*/
void selectWidget() {
int appWidgetId = this.mAppWidgetHost.allocateAppWidgetId();
Intent pickIntent = new Intent(AppWidgetManager.ACTION_APPWIDGET_PICK);
pickIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
addEmptyData(pickIntent);
startActivityForResult(pickIntent, R.id.REQUEST_PICK_APPWIDGET);
}
/**
* This avoids a bug in the com.android.settings.AppWidgetPickActivity,
* which is used to select widgets. This just adds empty extras to the
* intent, avoiding the bug.
*
* See more: http://code.google.com/p/android/issues/detail?id=4272
*/
void addEmptyData(Intent pickIntent) {
ArrayList<AppWidgetProviderInfo> customInfo = new ArrayList<AppWidgetProviderInfo>();
pickIntent.putParcelableArrayListExtra(AppWidgetManager.EXTRA_CUSTOM_INFO, customInfo);
ArrayList<Bundle> customExtras = new ArrayList<Bundle>();
pickIntent.putParcelableArrayListExtra(AppWidgetManager.EXTRA_CUSTOM_EXTRAS, customExtras);
}
/**
* If the user has selected an widget, the result will be in the 'data' when
* this function is called.
*/
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
if (requestCode == R.id.REQUEST_PICK_APPWIDGET) {
configureWidget(data);
} else if (requestCode == R.id.REQUEST_CREATE_APPWIDGET) {
createWidget(data);
}
} else if (resultCode == RESULT_CANCELED && data != null) {
int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
if (appWidgetId != -1) {
mAppWidgetHost.deleteAppWidgetId(appWidgetId);
}
}
}
/**
* Checks if the widget needs any configuration. If it needs, launches the
* configuration activity.
*/
private void configureWidget(Intent data) {
Bundle extras = data.getExtras();
int appWidgetId = extras.getInt(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
if (appWidgetInfo.configure != null) {
Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_CONFIGURE);
intent.setComponent(appWidgetInfo.configure);
intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
startActivityForResult(intent, R.id.REQUEST_CREATE_APPWIDGET);
} else {
createWidget(data);
}
}
/**
* Creates the widget and adds to our view layout.
*/
public void createWidget(Intent data) {
if(numWidgets<4) {
Bundle extras = data.getExtras();
int appWidgetId = extras.getInt(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
AppWidgetHostView widHostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
widHostView.setAppWidget(appWidgetId, appWidgetInfo);
SharedPreferences sharedPref = this.getPreferences(Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPref.edit();
editor.putInt("ID"+numWidgets, appWidgetId);
editor.commit();
attachWidget(widHostView);
}
}
/**
* Attaches a new widget at the right position on hostView
* #param widHostView widget to attach
*/
public void attachWidget(AppWidgetHostView widHostView){
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(hostView.getWidth() / 2, hostView.getHeight() / 2);
if (numWidgets < 2) {
lp.leftMargin = numWidgets * (hostView.getWidth() / 2);
lp.topMargin = 0;
} else {
lp.leftMargin = (numWidgets - 2) * (hostView.getWidth() / 2);
lp.topMargin = hostView.getHeight() / 2;
}
this.hostView.addView(widHostView, lp);
numWidgets++;
}
/**
* Restores all widgets from shared preferences
*/
public void restoreWidgets()
{
SharedPreferences sharedPref = this.getPreferences(Context.MODE_PRIVATE);
restoreWidget(sharedPref.getInt("ID0", -1));
restoreWidget(sharedPref.getInt("ID1", -1));
restoreWidget(sharedPref.getInt("ID2", -1));
restoreWidget(sharedPref.getInt("ID3", -1));
}
/**
* Restores a single widget
*/
public void restoreWidget(int _widgetId){
if(_widgetId < 0){
return;
}
AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(_widgetId);
AppWidgetHostView hostView = mAppWidgetHost.createView(this, _widgetId, appWidgetInfo);
hostView.setAppWidget(_widgetId, appWidgetInfo);
attachWidget(hostView);
}
/**
* Registers the AppWidgetHost to listen for updates to any widgets this app
* has.
*/
#Override
protected void onStart() {
super.onStart();
mAppWidgetHost.startListening();
}
/**
* Stop listen for updates for our widgets (saving battery).
*/
#Override
protected void onStop() {
super.onStop();
mAppWidgetHost.stopListening();
}
/**
* Removes the widget displayed by this AppWidgetHostView.
*/
public void removeWidget(AppWidgetHostView hostView) {
mAppWidgetHost.deleteAppWidgetId(hostView.getAppWidgetId());
this.hostView.removeView(hostView);
}
/**
* Removes all widgets displayed on screen.
*/
public void removeAllWidgets(){
boolean stop = false;
do{
int childCount = hostView.getChildCount();
if (childCount >= 1) {
View view = hostView.getChildAt(childCount - 1);
if (view instanceof AppWidgetHostView) {
removeWidget((AppWidgetHostView) view);
}
}else{
stop = true;
}
}while (!stop);
}
}
I did some debugging for this and realized, that you need to store the appWidgetIds for each widget added. When I stored appWidgetIds and tried to get AppWidgetProviderInfo from them, the width and height was always 0, BUT mAppWidgetHost.createView would return a valid view. So to fix this store the last width and height, the id, and any other data you wish to a WidgetHolder. Then you can store those values into SharedPreferences if you wish.
Here is a working WidgetHolder class I wrote ...
import android.util.Base64;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
public class WidgetHolder implements Serializable {
// any other data you want
public int id;
public int width, height;
public WidgetHolder() {}
public WidgetHolder(int id, int width, int height) {
this.id = id;
this.width = width;
this.height = height;
}
public static WidgetHolder deserialize( String data ) {
try {
byte [] bites = Base64.decode( data.getBytes(), 0);
ByteArrayInputStream bis = new ByteArrayInputStream( bites );
ObjectInputStream ois = new ObjectInputStream( bis );
WidgetHolder holder = (WidgetHolder) ois.readObject();
ois.close();
return holder;
} catch (IOException e ) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
return new WidgetHolder();
}
public String serialize() {
try {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream( baos );
oos.writeObject(this);
oos.flush();
return new String(Base64.encode(baos.toByteArray(), 0));
} catch( IOException e ) {
return "";
}
}
}
Then whenever you add a new AppWidget to your AppWidgetHost you store the WidgetHolder data to SharedPreferences or a cached file.
private void handleAddNewAppWidget( int appWidgetId, AppWidgetProviderInfo info ) {
// use application context, for issues with support v7 libs causing AppCompatImage exceptions
AppWidgetHostView hostView = mAppWidgetHost
.createView(context.getApplicationContext(), appWidgetId, info);
hostView.setAppWidget(appWidgetId, info);
// calculate width and height for hostView from info.minHeight and info.minWidth
RelativeLayout.LayoutParams rlp = new RelativeLayout.LayoutParams( width, height );
hostView.setLayoutParams(rlp);
// add hostView to widgetContainer (RelativeLayout)
widgetContainer.addView(hostView);
// create widgetHolder
WidgetHolder holder = new WidgetHolder( appWidgetId, width, height );
String serialized = holder.serialize();
// storage is a wrapper for SharedPreferences, the key is the appWidgetId, and the value is the serialized content
storage.store(appWidgetId + "", serialized);
}
private void handleRestoreWidgets(ArrayList<String> widgets) {
// Retrieve all saved widgets from Storage
for( int i = 0; i < widgets.size(); i++ ) {
String widgetString = widgets.get(i);
WidgetHolder holder = WidgetHolder.deserialize(widgetString);
AppWidgetProviderInfo info = AppWidgetManager
.getInstance(getContext().getApplicationContext() )
.getAppWidgetInfo(holder.id);
AppWidgetHostView hostView = mAppWidgetHost
.createView(context.getApplicationContext(), holder.id, info);
hostView.setAppWidget(holder.id, info);
RelativeLayout.LayoutParams rlp = new RelativeLayout.LayoutParams( holder.width,
holder.height );
hostView.setLayoutParams(rlp);
// restore app widget from holder
widgetContainer.addView(hostView);
}
}
The Storage class was not included for brevity as this post is already long. However, to get all key-value pairs from SharedPreferences is easy.
A small example ...
Map<String, String> allKeyValues = getContext().getSharedPreferences("WidgetHolders", Context.MODE_PRIVATE).getAll();
// then to get all stored String do
ArrayList<String> serializedWidgetHolders = new ArrayList<String>( allKeyValues.values() );

Android appwidget click not working

Gosh, there must be a thousand different tutorials on android appwidgets and even more questions here, but I just cannot figure out why mine isn't working. sigh
Rhetorical question: why can't the code be the same here as just about every other object with the setOnClickListener (new new Button.OnClickListener() { // do stuff }...
Anyway, my widget shows up on the screen and the labels are correct, but when I tap on the widget, nothing happens. I've put breakpoints in all the places where I think something would happen, but nothing is being executed.
Question 1: What code is executed after a widget is tapped?
My widget doesn't really update when it is tapped. Rather, it just executes some code in the rest of my program. It just makes some networking http and/or socket server commands. Also, my widget is configured with an activity before it is placed on the desktop.
Here's the manifest:
<receiver android:name="PhcaAppWidgetProvider" >
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
<action android:name="com.skipmorrow.phca.PhcaAppWidgetProvider.WIDGET_CLICKED" />
</intent-filter>
<meta-data android:name="android.appwidget.provider"
android:resource="#xml/phca_widget_info" />
</receiver>
Here's the widget configurator activity
private Activity act;
private static ListView listView;
private static ArrayAdapter<String> adapter;
private ArrayList<String> actionList;
private final String widgetPageName = "_widget";
int mAppWidgetId = AppWidgetManager.INVALID_APPWIDGET_ID;
private static final String PREFS_NAME = "PHCA";
private static final String PREF_PREFIX_KEY = "prefix_";
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setTitle("Choose an action for this widget");
actionList = GetActionList();
if (!actionList.isEmpty()) {
listView = getListView();
adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, android.R.id.text1, actionList);
setListAdapter(adapter);
}
else {
// no objects on the widget page
}
// Find the widget id from the intent.
Intent intent = getIntent();
Bundle extras = intent.getExtras();
if (extras != null) {
mAppWidgetId = extras.getInt(
AppWidgetManager.EXTRA_APPWIDGET_ID, AppWidgetManager.INVALID_APPWIDGET_ID);
}
// If they gave us an intent without the widget id, just bail.
if (mAppWidgetId == AppWidgetManager.INVALID_APPWIDGET_ID) {
finish();
}
}
private ArrayList<String> GetActionList() {
ArrayList<String> l = new ArrayList<String>();
Page p = CommonActivity.GetPageNamed(getApplicationContext(), widgetPageName);
if (p!=null) {
if (p.pageObjects.size()==0) DisplayEmptyPageHelpDialog();
for (int i = 0; i < p.pageObjects.size(); i++) {
l.add(p.pageObjects.get(i).GetParsedMajorLabel(getApplicationContext()).toString());
}
}
else {
CreateWidgetPage();
DisplayEmptyPageHelpDialog();
}
return l;
}
private void CreateWidgetPage() {
Page widgetPage = new Page(getApplicationContext());
widgetPage.setPageName(widgetPageName);
widgetPage.SetPageType("list");
widgetPage.setNote("Widget Page");
widgetPage.setPageTitle("Widget Page");
widgetPage.setImageFilename("");
widgetPage.setTransparentImageOverlayFilename("");
widgetPage.InsertInstanceIntoDatabase(getApplicationContext());
}
private void DisplayEmptyPageHelpDialog() {
Dialog helpDialog = new Dialog(this);
helpDialog.setContentView(R.layout.phca_help_dialog);
helpDialog.setTitle("PHCA Widget");
TextView helpText = (TextView) helpDialog.findViewById(R.id.tvHelpText);
helpText.setText("Your _widget page is empty. Please add an action to the _widget page so it can be used in a widget.");
TextView subTitle = (TextView) helpDialog.findViewById(R.id.tvSubject);
subTitle.setText("PHCA Widget configurator");
helpDialog.show();
}
#Override
protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
SharedPreferences.Editor prefs = getSharedPreferences(PREFS_NAME, 0).edit();
prefs.putInt(PREF_PREFIX_KEY + mAppWidgetId, position);
prefs.commit();
// Push widget update to surface with newly set prefix
String majorLabel = CommonActivity.GetPageObjectAtIndex(getApplicationContext(), widgetPageName, position).GetParsedMajorLabel(getApplicationContext()).toString();
String minorLabel = CommonActivity.GetPageObjectAtIndex(getApplicationContext(), widgetPageName, position).GetParsedMinorLabel(getApplicationContext()).toString();
AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(getApplicationContext());
PhcaAppWidgetProvider.updateAppWidget(getApplicationContext(), appWidgetManager,
mAppWidgetId, majorLabel, minorLabel);
Intent resultValue = new Intent();
resultValue.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, mAppWidgetId);
setResult(RESULT_OK, resultValue);
finish();
}
And here's my appwidget provider
public class PhcaAppWidgetProvider extends AppWidgetProvider {
private static final String ACTION_CLICK = "WIDGET_CLICKED";
private final String widgetPageName = "_widget";
private static final String PREFS_NAME = "PHCA";
private static final String PREF_PREFIX_KEY = "prefix_";
#Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager,
int[] appWidgetIds) {
// Get all ids
ComponentName thisWidget = new ComponentName(context,
PhcaAppWidgetProvider.class);
//int[] allWidgetIds = appWidgetManager.getAppWidgetIds(thisWidget);
final int N = appWidgetIds.length;
for (int i=0; i<N; i++) {
int appWidgetId = appWidgetIds[i];
SharedPreferences myPrefs = context.getSharedPreferences(PREFS_NAME, context.MODE_WORLD_WRITEABLE);
Integer objNum = myPrefs.getInt(PREF_PREFIX_KEY + appWidgetId, -1);
if (objNum > -1) {
PageAction pa = (PageAction) CommonActivity.GetPageObjectAtIndex(context, widgetPageName, objNum);
String majorLabel = pa.GetUnparsedMajorLabel(context).toString();
String minorLabel = pa.GetUnparsedMinorLabel(context).toString();
updateAppWidget(context, appWidgetManager, appWidgetId, majorLabel, minorLabel);
}
}
}
#Override
public void onEnabled(Context context) {
Log.d("Widget", "onEnabled");
}
#Override
public void onReceive(Context context, Intent intent) {
String intentAction = intent.getAction();
updateWidgetState(context, intentAction);
if (intentAction.equals(ACTION_CLICK)) {
Bundle extras = intent.getExtras();
Integer appWidgetId = extras.getInt("appwidgetid");
SharedPreferences myPrefs = context.getSharedPreferences(PREFS_NAME, context.MODE_WORLD_WRITEABLE);
Integer objNum = myPrefs.getInt(PREF_PREFIX_KEY + appWidgetId, -1);
if (objNum > -1) {
PageAction pa = (PageAction) CommonActivity.GetPageObjectAtIndex(context, widgetPageName, objNum);
pa.ExecuteActionFromWidgetClick(context);
}
} else {
super.onReceive(context, intent);
}
}
public static void updateWidgetState(Context paramContext, String paramString)
{
RemoteViews localRemoteViews = buildUpdate(paramContext, paramString);
ComponentName localComponentName = new ComponentName(paramContext, PhcaAppWidgetProvider.class);
AppWidgetManager.getInstance(paramContext).updateAppWidget(localComponentName, localRemoteViews);
}
private static RemoteViews buildUpdate(Context ctx, String paramString)
{
RemoteViews views = new RemoteViews(ctx.getPackageName(), R.layout.phca_appwidget);
views.setTextViewText(R.id.majorlabel, "majorLabel");
views.setTextViewText(R.id.minorlabel, "minorLabel");
Intent intent = new Intent(ctx, PhcaAppWidgetProvider.class);
intent.setAction(ACTION_CLICK);
intent.putExtra("appwidgetid", 0);
PendingIntent pendingIntent = PendingIntent.getBroadcast(ctx, 0, intent , 0);
views.setOnClickPendingIntent(R.layout.phca_appwidget, pendingIntent);
if(paramString.equals(ACTION_CLICK))
{
Toast.makeText(ctx, "ACTION_CLICK", Toast.LENGTH_LONG).show();
}
return views;
}
}
When I add the widget and remove it, different intents are passed into the onReceive, so that is working, but nothing when a click happens.
Question 2: Can someone please be so kind as to point out what I did wrong? I pretty followed the tutorials here: http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/appwidget/ExampleAppWidgetProvider.html
Question 3: It would seem that I could put an android:onclick="WidgetClicked" in the layout xml for the widget, but I couldn't figure out where to put the WidgetClicked method. The WidgetProvider seemed logical, but that didn't work for me either. Is there a way to do this in the xml?
DISCLAIMER: the code above is the current state after a day and a half of troubleshooting. It just one iteration of many different tries.
you need to rigister a reciver for widget click as in manifest :
<receiver android:name="MyAppWidgetProvider" >
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
<action android:name="com.myname.myapp.MyAppWidgetProvider.ACTION_CLICK" />
</intent-filter>
<meta-data android:name="android.appwidget.provider"
android:resource="#xml/my_widget_info" />
</receiver>
AppWidgetProvider.java
public class MyAppWidgetProvider extends AppWidgetProvider {
private static final String ACTION_CLICK = "ACTION_CLICK_WIDGET";
private final String widgetPageName = "_widget";
private static final String PREFS_NAME = "MYAPP";
private static final String PREF_PREFIX_KEY = "prefix_";
#Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager,
int[] appWidgetIds) {
ComponentName thisWidget = new ComponentName(context,
MyAppWidgetProvider.class);
final int N = appWidgetIds.length;
for (int i=0; i<N; i++) {
int appWidgetId = appWidgetIds[i];
SharedPreferences myPrefs =
context.getSharedPreferences(PREFS_NAME, context.MODE_WORLD_WRITEABLE);
Integer objNum = myPrefs.getInt(PREF_PREFIX_KEY + appWidgetId, -1);
if (objNum > -1) {
PageAction pa = (PageAction) CommonActivity
.GetPageObjectAtIndex(context, widgetPageName, objNum);
String majorLabel = pa.GetUnparsedMajorLabel(context).toString();
String minorLabel = pa.GetUnparsedMinorLabel(context).toString();
updateAppWidget(context, appWidgetManager,
appWidgetId, majorLabel, minorLabel);
}
}
}
#Override
public void onReceive(Context context, Intent intent) {
String intentAction = intent.getAction();
if (intentAction.equals(ACTION_CLICK)) {
updateWidgetState(paramContext, str);
Bundle extras = intent.getExtras();
Integer appWidgetId = extras.getInt("appwidgetid");
SharedPreferences myPrefs =
context.getSharedPreferences(PREFS_NAME, context.MODE_WORLD_WRITEABLE);
Integer objNum = myPrefs.getInt(PREF_PREFIX_KEY + appWidgetId, -1);
if (objNum > -1) {
PageAction pa = (PageAction) CommonActivity
.GetPageObjectAtIndex(context, widgetPageName, objNum);
pa.ExecuteActionFromWidgetClick(context);
}
} else {
super.onReceive(context, intent);
}
}
public static void updateWidgetState(Context paramContext, String paramString)
{
RemoteViews localRemoteViews = buildUpdate(paramContext, paramString);
ComponentName localComponentName = new ComponentName(paramContext, MyAppWidgetProvider.class);
AppWidgetManager.getInstance(paramContext).updateAppWidget(localComponentName, localRemoteViews);
}
private static RemoteViews buildUpdate(Context ctx, String paramString)
{
RemoteViews views =
new RemoteViews(ctx.getPackageName(), R.layout.my_appwidget);
views.setTextViewText(R.id.majorlabel, majorLabel);
views.setTextViewText(R.id.minorlabel, minorLabel);
Intent intent = new Intent(ctx, MyAppWidgetProvider.class);
intent.setAction(ACTION_CLICK);
intent.putExtra("appwidgetid", mAppWidgetId);
PendingIntent configPendingIntentprev = PendingIntent.getBroadcast(ctx, 0, intent , 0);
views.setOnClickPendingIntent(R.layout.my_appwidget, pendingIntent);
if(parmString.equals(ACTION_CLICK))
{
//Toast.maketext("").show();
//
}
return rview;
}
}
see my answer in this post for full example.

Sharedpreferences does not work with appWidgetProvider (getApplicationContext is undefined)

I am creating a widget that will show some datas for the user from a database. One of the datas depends on a parameter that can be set in my settings activity. I save this parameter with sharedpreferences so I can use it anywhere in my code.
In an activity I could use getApplicationContext, but here where I tell the widget what to do, it doesnt work. What should I use instead of getApplicationContext?
UPDATED
public class plWidget extends AppWidgetProvider{
SharedPreferences sharedPreferences;
String loadedWeightType;
#Override
public void onDeleted(Context context, int[] appWidgetIds) {
// TODO Auto-generated method stub
super.onDeleted(context, appWidgetIds);
Toast.makeText(context, "deleted", 2500).show();
}
#Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager,
int[] appWidgetIds) {
// TODO Auto-generated method stub
super.onUpdate(context, appWidgetManager, appWidgetIds);
String Wcal="0",Wfat="0",Wprot="0",Wcarb="0",Wsport="0";
final int N = appWidgetIds.length;
for (int i = 0;i<N;i++)
{
int awID = appWidgetIds[i];
updateAppWidget(context, appWidgetManager, appWidgetIds[i]);
GlobalVars.setSulyType(loadedWeightType);
Log.i("SULYYYY", GlobalVars.getSulyType());
long now = System.currentTimeMillis();
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
Date resultdate = new Date(now);
Log.i("ASAS", sdf.format(resultdate));
hornot database = new hornot(context);
database.open();
int ccc = database.checkDataExists(sdf.format(resultdate), sdf.format(resultdate));
if (ccc==0){
Log.i("nulla", "0");
Log.i("nulla", GlobalVars.getSulyType());
Wcal="0";
Wfat="0";
Wprot="0";
Wcarb="0";
}
else{
database.getDateFromAndToFromDatePicker(sdf.format(resultdate), sdf.format(resultdate));
Wcal = GlobalVars.getSums();
database.FATgetDateFromAndToFromDatePicker(sdf.format(resultdate), sdf.format(resultdate));
Wfat = GlobalVars.getSums();
database.PROTEINgetDateFromAndToFromDatePicker(sdf.format(resultdate), sdf.format(resultdate));
Wprot = GlobalVars.getSums();
database.CARBSgetDateFromAndToFromDatePicker(sdf.format(resultdate), sdf.format(resultdate));
Wcarb = GlobalVars.getSums();
}
int ddd = database.checkDataExistsSports(sdf.format(resultdate), sdf.format(resultdate));
if (ddd==0){
Wsport="0";
}
else{
if (loadedWeightType.equals("kilogramm"))
{
database.SportgetDateFromAndToFromDatePicker(sdf.format(resultdate), sdf.format(resultdate));
// Wsport = GlobalVars.getSums();
Wsport= "kilogramm";
}
else if (loadedWeightType.equals("pound"))
{
database.SportgetDateFromAndToFromDatePicker(sdf.format(resultdate), sdf.format(resultdate));
Wsport="pound";
}
}
RemoteViews v = new RemoteViews(context.getPackageName(), R.layout.widget);
v.setTextViewText(R.id.tvwidgetUpdate, Wcal+Wfat+Wprot+Wcarb+Wsport);
appWidgetManager.updateAppWidget(awID, v);
database.close();
}
}
public void updateAppWidget(Context context, AppWidgetManager appWidgetManager, int appWidgetId) {
SharedPreferences prefs = context.getSharedPreferences(plWidget.class + Integer.toString(appWidgetId),
Context.MODE_WORLD_READABLE);
loadedWeightType= prefs.getString("weighttype", "kilogramm");
}
}
Thanks in advance!
UPDATE
As usual I do the load funciton:
public void LoadWeightType(){
sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
loadedWeightType= sharedPreferences.getString("weighttype", "kilogramm");
}
With this in a normal activity, I can load the weighttype. I guess that updateAppWidget function should somehow substitue this function.
public static void updateAppWidget(Context context, AppWidgetManager appWidgetManager, int appWidgetId) {
SharedPreferences prefs = context.getSharedPreferences(MyConfigActivity.NAME + Integer.toString(appWidgetId), Context.MODE_WORLD_READABLE);
I call updateAppWidget from onUpdate for each instance of my widget, passing in the parameters. The passed in Context has getSharedPreferences defined, and I specify which set of preferences to get based on the name of the configuration activity and the id of the widget. Here's the sdk reference for the getSharedPreferences function: http://developer.android.com/reference/android/content/Context.html#getSharedPreferences%28java.lang.String,%20int%29

Multiple Android Widget instances only updating last widget

I tried solving my problem using this link
update - I figured out that there is something wrong with the setter of the pending intent- every time I click on the imageview- the intent sends the extre details of the last defined widget-
meaning that the other pending intents that where defined on the pre-added widgets were run-over by the newer widgets
I have an app widget that shows a picture chosen by the user.(many widgets- many pictures)
my problem is: no matter which widget on screen I press - only the last added widget gets updated:
here is my code
My Widget xml provider Code
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
android:initialLayout="#layout/widget_Test"
android:minHeight="146dip"
android:minWidth="146dip"
android:updatePeriodMillis="0" />
My Manifest xml code
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.test"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="8" />
<uses-permission android:name="android.permission.BIND_REMOTEVIEWS" >
</uses-permission>
<application
android:icon="#drawable/icon"
android:label="#string/app_name" >
<activity
android:name=".Activities.WidgetConfig"
android:screenOrientation="portrait" >
<!-- prbbly delete this line -->
<action android:name="android.appwidget.action.APPWIDGET_CONFIGURE" />
</activity>
<!-- Test Widget -->
<receiver
android:name=".Simple.Widget"
android:label="#string/app_widget_Test" >
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
</intent-filter>
<meta-data
android:name="android.appwidget.provider"
android:resource="#xml/widget_Test_provider" />
</receiver>
<service android:name=".Simple.Widget$TestWidgetService" />
</application>
</manifest>
Widget provider
public class Widget extends AppWidgetProvider
{
public static String PREFENCES_WIDGET_CONFIGURE = "ActionConfigureWidget";
public static int[] widgets;
#Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager,
int[] appWidgetIds)
{
Intent svcIntent = new Intent(context, TESTWidgetService.class);
widgets = appWidgetIds;
context.startService(svcIntent);
}
public static class TESTWidgetService extends Service
{
#Override
public void onStart(Intent intent, int startId)
{
super.onStart(intent, startId);
// Update the widget
RemoteViews remoteView = buildRemoteView(this);
// Push update to homescreen
Mngr.getInstance().pushUpdate(remoteView,
getApplicationContext(), Widget.class);
// No more updates so stop the service and free resources
stopSelf();
}
public RemoteViews buildRemoteView(Context context)
{
RemoteViews remoteViews = new RemoteViews(context.getPackageName(),
R.layout.widget_TEST);
if (widgets != null)
{
int length = widgets.length;
for (int i = 0; i < length; i++)
{
Intent configIntent = new Intent(context,
WidgetConfig.class);
configIntent.setAction(Widget.PREFENCES_WIDGET_CONFIGURE);
String number = AppWidgetManager.EXTRA_APPWIDGET_ID
+ "number";
configIntent.putExtra(number, length);
String widgetID = AppWidgetManager.EXTRA_APPWIDGET_ID + i;
int id = widgets[i];
configIntent.putExtra(widgetID, id);
PendingIntent runTESTPendingIntent = PendingIntent
.getActivity(context, 0, configIntent,
PendingIntent.FLAG_UPDATE_CURRENT);
remoteViews.setOnClickPendingIntent(R.id.TESTWidgetImage,
runTESTPendingIntent);
Mngr controller = Mngr.getInstance();
controller.updateTESTWidget(context, remoteViews);
}
}
return remoteViews;
}
#Override
public void onConfigurationChanged(Configuration newConfig)
{
int oldOrientation = this.getResources().getConfiguration().orientation;
if (newConfig.orientation != oldOrientation)
{
// Update the widget
RemoteViews remoteView = buildRemoteView(this);
// Push update to homescreen
Mngr.getInstance().pushUpdate(remoteView,
getApplicationContext(), Widget.class);
}
}
#Override
public IBinder onBind(Intent arg0)
{
// TODO Auto-generated method stub
return null;
}
}
}
my config activity
public class WidgetConfig extends ListActivity // implements
{
private Bundle m_extras;
private ArrayList<Test> m_tests = null;
private int mAppWidgetId = AppWidgetManager.INVALID_APPWIDGET_ID;
ImageAdapter m_adapter;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.imagelist);
Resources res = getResources();
Mngr mngr = Mngr.getInstance();
mngr.setResources(res);
Context ctx = getApplicationContext();
mngr.setContext(ctx);
getTests();
m_adapter = new ImageAdapter(ctx, R.layout.row, m_tests);
ImageDownloader.Mode mode = ImageDownloader.Mode.CORRECT;
m_adapter.getImageDownloader().setMode(mode);
setListAdapter(m_adapter);
Intent intent = getIntent();
m_extras = intent.getExtras();
}
private void getTests()
{
m_tests = new ArrayList<Test>();
Resources res = getResources();
String[] TestNames = res.getStringArray(R.array.fav_Test_array);
TypedArray imgs = getResources().obtainTypedArray(
R.array.fav_Test_integer);
for (int i = 0; i < TestNames.length; i++)
{
Test o1 = new Test();
String TestName = TestNames[i];
int resID = imgs.getResourceId(i, -1);
o1.setTestName(TestName);
o1.setIMGID(resID);
m_tests.add(o1);
}
imgs.recycle();
}
#Override
protected void onListItemClick(ListView l, View v, int position, long id)
{
ListAdapter adapt = l.getAdapter();
Object obj = adapt.getItem(position);
if (obj != null)
{
Mngr mngr = Mngr.getInstance();
Context context = getApplicationContext();
String key = context.getString(R.string.TestWidget_string) + "_"
+ AppWidgetManager.EXTRA_APPWIDGET_ID;
Test Test = (Test) obj;
String val = Test.getIMGID().toString();
mngr.putString(context, key, val);
updateWidget();
}
super.onListItemClick(l, v, position, id);
}
private void updateWidget()
{
Context ctx = getApplicationContext();
AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(ctx);
setResult(RESULT_CANCELED);
if (m_extras != null)
{
Intent resultValue = new Intent();
int numberOfWidgets = m_extras.getInt(
AppWidgetManager.EXTRA_APPWIDGET_ID + "number",
AppWidgetManager.INVALID_APPWIDGET_ID);
for (int i = 0; i < numberOfWidgets; i++)
{
String stringID = AppWidgetManager.EXTRA_APPWIDGET_ID + i;
mAppWidgetId = m_extras.getInt(stringID,
AppWidgetManager.INVALID_APPWIDGET_ID);
/*************************************************************/
/*I Don't really know if I am using this piece of code right */
Uri data = Uri.withAppendedPath(Uri.parse("ABCD"
+ "://widget/id/"), String.valueOf(mAppWidgetId));
resultValue.setData(data);
/*************************************************************/
RemoteViews views = new RemoteViews(ctx.getPackageName(),
R.layout.widget_Test);
Mngr.getInstance().updateTestWidget(ctx, views);
appWidgetManager.updateAppWidget(mAppWidgetId, views);
resultValue.putExtra(stringID, mAppWidgetId);
}
setResult(RESULT_OK, resultValue);
finish();
}
}
}
My Mngr code
public void updateTestWidget(Context context, RemoteViews remoteViews)
{
String key = context.getString(R.string.TestWidget_string) + "_"
+ AppWidgetManager.EXTRA_APPWIDGET_ID;
String s = getString(context, key, "");
if (s != null && s.equals("") == false)
{
int resID = Integer.valueOf(s);
remoteViews.setImageViewResource(R.id.TestWidgetImage, resID);
}
}
public void pushUpdate(RemoteViews remoteView, Context ctx, Class<?> cls)
{
ComponentName myWidget = new ComponentName(ctx, cls);
AppWidgetManager manager = AppWidgetManager.getInstance(ctx);
manager.updateAppWidget(myWidget, remoteView);
}
The problem isn't your configActivity, the problem is in your widgetProvider. I had the same problem as you did but solved using the link you specified. You need to set the "hacked" intent on your configIntent in buildRemoteView.
I had the same problem when I tested my application on emulator of 2.2 or 2.3 version. On a real device everything should work fine. Moreover, in my case emulator 4.0 worked properly. In any case I recommend you to test it on a real device.

Categories

Resources