我正在尝试创建一个带有定时器和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);
}Update:这是小部件的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>怎么了?我怎样才能一刀切地更新计时器值呢?也许问题是当我使用widgetID或其他的时候?非常感谢。
发布于 2015-06-21 01:48:50
如果您使用的是这样的布局文件,您需要findViewbyId,而不是像您这样创建一个新的文本视图。
timerValue = (TextView) findViewById(R.id.timerValue)你应该对你的纽扣做同样的事。您还需要向按钮中添加操作监听器,因为单击时,您应该在活动中的oncreate方法中这样做。
https://stackoverflow.com/questions/30878611
复制相似问题