I've tried to create a widget that, on click calls a routine on Configuration activity, on update calls another routine on Configuration activity, update must be under 30 minutes and all the instance created must work separately (in onUpdate and in onClick).
The code below: Click doesn't work (Can't See Toast Message and other function) Update works fine for first instance but with multiple widget instance it works bad: If i create 2 instances between 10 seconds (and refresh is set to 20 seconds) all will update every 10 seconds.
That's my Manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="it.fraschi.controllogiardinowg"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="it.fraschi.controllogiardinowg.Configurazione"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_CONFIGURE"/>
</intent-filter>
</activity>
<receiver android:name="it.fraschi.controllogiardinowg.ControlloWidget" android:label="@string/app_name">
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
</intent-filter>
<intent-filter>
<action android:name="it.fraschi.controllogiardinowg.ControlloWidget.ACTION_WIDGET_CLICKED"/>
</intent-filter>
<intent-filter>
<action android:name="it.fraschi.controllogiardinowg.ControlloWidget.MY_OWN_WIDGET_UPDATE" />
</intent-filter>
<meta-data android:name="android.appwidget.provider" android:resource="@xml/widget_provider" />
</receiver>
</application>
</manifest>
That's my Configuration:
package it.fraschi.controllogiardinowg;
import java.util.Calendar;
import java.util.Random;
import android.os.Bundle;
import android.app.Activity;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RemoteViews;
import android.widget.Spinner;
import android.widget.Toast;
import android.widget.AdapterView.OnItemSelectedListener;
public class Configurazione extends Activity {
private static final String PREFS_NAME = "it.fraschi.android.ControlloGiardinoWG";
public static final String NOME = "nome_";
private static long millis = 60000;
private int mAppWidgetId = AppWidgetManager.INVALID_APPWIDGET_ID;
private int selectedTextColor;
private int selectedBackgroundColor;
public Configurazione() {
super();
}
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
// Set the result to CANCELED. This will cause the widget host to cancel
// out of the widget placement if they press the back button.
setResult(RESULT_CANCELED);
// Set the view layout resource to use.
setContentView(R.layout.activity_configurazione);
// 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();
}
//final Spinner backgroundColorSelector = (Spinner)findViewById(R.id.backgroundColor);
final Button saveButton = (Button)findViewById(R.id.btnSalva);
final Button cancelButton = (Button)findViewById(R.id.btnCancel);
final EditText editNome = (EditText)findViewById(R.id.editNome);
/*textColorSelector.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
selectedTextColor = Integer.parseInt(getResources().getStringArray(R.array.textColorsValues)[textColorSelector.getSelectedItemPosition()]);
}
@Override
public void onNothingSelected(AdapterView<?> parentView) {}
});*/
saveButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
final Context context = Configurazione.this;
//prepare Alarm Service to trigger Widget
Intent intent = new Intent(ControlloWidget.MY_WIDGET_UPDATE);
PendingIntent pendingIntent = PendingIntent.getBroadcast(Configurazione.this, mAppWidgetId, intent, 0);
AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.add(Calendar.SECOND, 30);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), millis, pendingIntent);
//ControlloWidget.SaveAlarmManager(alarmManager, pendingIntent);
///
SharedPreferences.Editor prefs = context.getSharedPreferences(PREFS_NAME, 0).edit();
prefs.putString(NOME + mAppWidgetId, editNome.getText().toString());
prefs.commit();
// Push widget update to surface with newly set prefix
AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
ControlloWidget.updateAppWidget(context, appWidgetManager, mAppWidgetId);
// Make sure we pass back the original appWidgetId
Intent resultValue = new Intent();
resultValue.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, mAppWidgetId);
setResult(RESULT_OK, resultValue);
finish();
}
});
cancelButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
finish();
}
});
}
/*@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.configurazione, menu);
return true;
}*/
static String getName(Context context, String prefKey ,int appWidgetId) {
SharedPreferences prefs = context.getSharedPreferences(PREFS_NAME, 0);
String valuename = prefs.getString(prefKey + appWidgetId, "Non Trovato");
return valuename;
}
static String getColor(Context context, String prefKey, int appWidgetId){
///Test
SharedPreferences prefs = context.getSharedPreferences(PREFS_NAME, 0);
String nome = prefs.getString(prefKey + appWidgetId, "Non Trovato");
///
int number = (new Random().nextInt(100));
String color = "DaConf"+Integer.toString(number)+nome;
Toast.makeText(context, "ESEGUITO", Toast.LENGTH_LONG).show();
return color;
}
}
That's my Widget:
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Environment;
import android.os.StatFs;
import android.widget.RemoteViews;
import android.widget.Toast;
public class ControlloWidget extends AppWidgetProvider {
public static String ACTION_WIDGET_CLICKED = "it.fraschi.controllogiardinowg.ESEGUI";
public static String MY_WIDGET_UPDATE = "it.fraschi.controllogiardinowg.ControlloWidget.MY_OWN_WIDGET_UPDATE";
static String strWidgetText = "";
public static Boolean choice = false;
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
//super.onReceive(context, intent);
String currentDateTimeString = DateFormat.getDateTimeInstance().format(new Date());
if(MY_WIDGET_UPDATE.equals(intent.getAction())){
Bundle extras = intent.getExtras();
if(extras!=null) {
AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
ComponentName thisAppWidget = new ComponentName(context.getPackageName(),
ControlloWidget.class.getName());
int[] appWidgetIds = appWidgetManager.getAppWidgetIds(thisAppWidget);
choice=false;
onUpdate(context, appWidgetManager, appWidgetIds);
}
Toast.makeText(context, "WIDGET UPDATE" +currentDateTimeString, Toast.LENGTH_LONG).show();
}
//Test Click
if(ACTION_WIDGET_CLICKED.equals(intent.getAction())){
Bundle extras = intent.getExtras();
if(extras!=null) {
AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
ComponentName thisAppWidget = new ComponentName(context.getPackageName(),
ControlloWidget.class.getName());
int[] appWidgetIds = appWidgetManager.getAppWidgetIds(thisAppWidget);
choice=true;
onUpdate(context, appWidgetManager, appWidgetIds);
}
Toast.makeText(context, "WIDGET PREMUTO", Toast.LENGTH_LONG).show();
}
//TestClick
}
@Override
public void onEnabled(Context context) {
// TODO Auto-generated method stub
//super.onEnabled(context);
Toast.makeText(context, "