i'm trying to create an appWidget with a timer and 2 button,(play and stop). The problem is that when i start the playButton the timerValue doesn't update. Probably is a stupid problem.
This is my class Service:
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
super.onStart(intent, startId);
productivityDB = new ProductivityDB(this);
updateTime(intent);
// stopSelf(startId);
appWidgetMan = AppWidgetManager.getInstance(this);
views = new RemoteViews(getApplicationContext().getPackageName(), R.layout.prod_widget);
timerValue = new TextView(getApplicationContext());
widgetId = intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, 0);
sendBroadcast(intent);
return START_REDELIVER_INTENT;
}
private void updateTime(Intent intent) {
if (intent != null){
String requestedAction = intent.getAction();
if (requestedAction != null && requestedAction.equals(UPDATETIME)){
startTime = SystemClock.uptimeMillis();
lavoroBean = new LavoroBean();
lavoroBean.setIdInterr(1L);
lavoroBean.setData(data);
Calendar cal = Calendar.getInstance();
cal.setTime(data);
year = String.valueOf(cal.get(Calendar.YEAR));
month = String.valueOf(cal.get(Calendar.MONTH));
day = String.valueOf(cal.get(Calendar.DAY_OF_MONTH));
lavoroBean.setAnno(year);
lavoroBean.setGiorno(day);
lavoroBean.setMese(month);
customHandler.postDelayed(updateTimerThread, 0);
}
if (requestedAction != null && requestedAction.equals(STOPTIME)){
timeSwapBuff += timeInMilliseconds;
customHandler.removeCallbacks(updateTimerThread);
typeInterrupt ="Non definito";
interruzioneBean = new InterruzioneBean();
interruzioneBean.setTipoInterruzione(typeInterrupt);
long minuti= TimeUnit.MILLISECONDS.toMinutes(updatedTime);
lavoroBean.setTempoLavorato(String.valueOf(updatedTime));
Calendar cal = Calendar.getInstance();
cal.setTime(data);
year = String.valueOf(cal.get(Calendar.YEAR));
month = String.valueOf(cal.get(Calendar.MONTH));
day = String.valueOf(cal.get(Calendar.DAY_OF_MONTH));
String dataString =day+ "/"+month+"/"+year;
lavoroBean.setAnno(year);
lavoroBean.setGiorno(day);
lavoroBean.setMese(month);
lavoroBean.setDataString(dataString);
productivityDB.insertLavoro(lavoroBean);
productivityDB.insertInterr(interruzioneBean);
Toast.makeText(this, "Interruzione", Toast.LENGTH_SHORT).show();
}
}
}
public class LocalBinder extends Binder {
ProductivityService getService() {
return ProductivityService.this;
}
}
@Override
public IBinder onBind(Intent intent) {
return null;
}
//This is the runnable for update the textView
public Runnable updateTimerThread = new Runnable() {
public void run() {
timeInMilliseconds = SystemClock.uptimeMillis() - startTime;
updatedTime = timeSwapBuff + timeInMilliseconds;
int secs = (int) (updatedTime / 1000);
int mins = secs / 60;
int hour = mins / 60;
secs = secs % 60;
int milliseconds = (int) (updatedTime % 1000);
timerValue.setText("" + hour + ":" + mins + ":" + String.format("%02d", secs));
views.setTextViewText(R.id.timerValueWidget, String.valueOf(timerValue));
customHandler.postDelayed(this, 0);
appWidgetMan.updateAppWidget(widgetId, views);
}
};
This is the provider:
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
super.onUpdate(context, appWidgetManager, appWidgetIds);
Log.i(WIDGETTIME, "onUpdate");
final int N = appWidgetIds.length;
// Perform this loop procedure for each App Widget that belongs to this provider
for (int i=0; i<N; i++) {
int appWidgetId = appWidgetIds[i];
Log.i(WIDGETTIME, "updating widget[id] " + appWidgetId);
views = new RemoteViews(context.getPackageName(), R.layout.prod_widget);
intent = new Intent(context, ProductivityService.class);
intent.setAction(ProductivityService.UPDATETIME);
intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
pendingIntent = PendingIntent.getService(context, 0, intent, 0);
views.setOnClickPendingIntent(R.id.startWidget, pendingIntent);
// views.setOnClickPendingIntent(R.id.button2, pendingIntent);
Log.i(WIDGETTIME, "pending intent set");
// Tell the AppWidgetManager to perform an update on the current App Widget
appWidgetManager.updateAppWidget(appWidgetId, views);
}
}
@Override
public void onReceive(Context context, Intent intent) {
super.onReceive(context, intent);
AppWidgetManager.getInstance(context).updateAppWidget
(intent.getIntArrayExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS), views);
}
Update: This is the xml code for widget:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:id="@+id/prod_widget">
<LinearLayout
android:layout_width="310dp"
android:layout_height="80dp"
android:background="#BBDEFB"
android:orientation="horizontal"
android:padding="16dp"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:id="@+id/linearLayout">
<!--android:padding="@dimen/widget_margin"-->
<TextView
android:id="@+id/timerValue"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:textSize="35sp"
android:elevation="2dp"
android:textColor="#303F9F"
android:text="@string/timerVal"
android:layout_marginTop="5dp" />
<Button
android:id="@+id/button2"
android:background="@drawable/stopbutton"
android:layout_width="50dp"
android:layout_height="50dp"
android:elevation="2dp"
android:layout_gravity="right"
android:layout_marginLeft="30dp"
android:clickable="true"/>
<Button
android:id="@+id/startWidget"
android:background="@drawable/playbutton"
android:layout_width="50dp"
android:layout_height="50dp"
android:elevation="2dp"
android:layout_gravity="right"
android:layout_marginLeft="10dp"
android:clickable="true"/>
</LinearLayout>
</RelativeLayout>
What is wrong ? How I can update the timervalue dinamically ?Maybe the problem is when i take widgetID or other ? Thanks a lot.
If you are using a layout file like this, you need to findViewbyId so instead of creating a new textview like you are change the line to this.
You should do the same for your buttons. You also need to add action listeners to your buttons for on click you should do this in your oncreate method in your activity.