我试图插入一个处理程序以每 x 秒更新一次 widget_textview 背景,但我不知道放置什么以及放置在哪里
int i;
if (i==1) {
TextView myTxt = (TextView) findViewById(R.id.widget_textview);
myTxt.setBackgroundResource(R.drawable.pic1);
i++;
}else if(i==2) {
TextView myTxt = (TextView) findViewById(R.id.widget_textview);
myTxt.setBackgroundResource(R.drawable.pic2);
i++;
}else if(i==3) {
TextView myTxt = (TextView) findViewById(R.id.widget_textview);
myTxt.setBackgroundResource(R.drawable.pic1);
i=2;
}else {
i++;
}
我不能把它放在任何地方,在任何地方我放置这个代码我在 textview 中出现错误
这是我的代码
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
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.Handler;
import android.widget.RemoteViews;
import android.widget.TextView;
public class Widget extends AppWidgetProvider {
private Handler handler = new Handler();
RemoteViews views;
AppWidgetManager appWidgetManager;
ComponentName currentWidget;
Context context;
DateFormat format = new SimpleDateFormat("HH:mm");
public void onUpdate(Context context, AppWidgetManager appWidgetManager,
int[] appWidgetIds) {
this.context = context;
this.appWidgetManager = appWidgetManager;
views = new RemoteViews(context.getPackageName(), R.layout.widget);
currentWidget = new ComponentName(context, Widget.class);
handler.removeCallbacks(UpdateWidget);
handler.postDelayed(UpdateWidget, 100);
}
final Runnable UpdateWidget = new Runnable() {
public void run() {
Intent informationIntent = new Intent(context, MainActivity.class);
PendingIntent infoPendingIntent = PendingIntent.getActivity(
context, 0, informationIntent, 0);
views.setOnClickPendingIntent(R.id.Widget, infoPendingIntent);
views.setTextViewText(R.id.widget_textview,
"" + format.format(new Date()));
appWidgetManager.updateAppWidget(currentWidget, views);
handler.postDelayed(UpdateWidget, 1000);
}
};
@Override
public void onDisabled(Context context) {
super.onDisabled(context);
handler.removeCallbacks(UpdateWidget);
}
}