更新appWidget中的textView android无法正常工作

时间:2015-06-16 21:32:42

标签: android textview runnable android-appwidget

我试图创建一个带有计时器和2按钮的appWidget,(播放和停止)。 问题是,当我启动playButton时,timerValue不会更新。可能是一个愚蠢的问题。

这是我的班级服务:

 @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);
        }

    };

这是提供者:

@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);
    }

更新: 这是widget的xml代码:

<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>
  

有什么问题?我怎么能以dinamically的方式更新timervalue?也许问题是当我使用widgetID或其他?非常感谢。

1 个答案:

答案 0 :(得分:1)

如果您使用这样的布局文件,则需要findViewbyId,而不是创建新的文本视图,就像您将行更改为此。

timerValue = (TextView) findViewById(R.id.timerValue)

您应该对按钮执行相同的操作。您还需要向按钮添加动作侦听器,以便在单击时在活动中的oncreate方法中执行此操作。