更新 appWidget android 中的 textView 无法正常工作
Update textView in appWidget android doesnt work properly
我正在尝试创建一个带有计时器和 2 个按钮(播放和停止)的 appWidget。
问题是当我启动 playButton 时,timerValue 没有更新。可能是一个愚蠢的问题。
这是我的 class 服务:
@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);
}
更新:
这是小部件的 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>
What is wrong ? How I can update the timervalue dinamically ?Maybe the problem is when i take widgetID or other ? Thanks a lot.
如果您使用的是这样的布局文件,则需要 findViewbyId,而不是像您那样创建新的文本视图,而是将行更改为此。
timerValue = (TextView) findViewById(R.id.timerValue)
你应该对你的按钮做同样的事情。您还需要将动作侦听器添加到您的按钮以进行单击,您应该在 activity.
的 oncreate 方法中执行此操作
我正在尝试创建一个带有计时器和 2 个按钮(播放和停止)的 appWidget。 问题是当我启动 playButton 时,timerValue 没有更新。可能是一个愚蠢的问题。
这是我的 class 服务:
@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);
}
更新: 这是小部件的 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>
What is wrong ? How I can update the timervalue dinamically ?Maybe the problem is when i take widgetID or other ? Thanks a lot.
如果您使用的是这样的布局文件,则需要 findViewbyId,而不是像您那样创建新的文本视图,而是将行更改为此。
timerValue = (TextView) findViewById(R.id.timerValue)
你应该对你的按钮做同样的事情。您还需要将动作侦听器添加到您的按钮以进行单击,您应该在 activity.
的 oncreate 方法中执行此操作