我正在创造"开始","停止"和"重置"秒表安卓小部件的按钮。
下面是我的Main.java
public class Main extends AppWidgetProvider {
public void onUpdate(Context context, AppWidgetManager appWidgetManager,
int[] appWidgetIds) {
for (int appWidgetId : appWidgetIds) {
Intent buildButtonIntent = new Intent();
buildButtonIntent.setAction("android.intent.action.UPDATE");
buildButtonIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
PendingIntent buildButtonPendingIntent = PendingIntent.getBroadcast(context, appWidgetId, buildButtonIntent, 0);
Intent stopButtonIntent = new Intent();
stopButtonIntent.setAction("android.intent.action.UPDATEd");
stopButtonIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
PendingIntent stopButtonPendingIntent = PendingIntent.getBroadcast(context, appWidgetId, stopButtonIntent, PendingIntent.FLAG_CANCEL_CURRENT);
RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.main);
remoteViews.setOnClickPendingIntent(R.id.button1, buildButtonPendingIntent );
remoteViews.setOnClickPendingIntent(R.id.button2, stopButtonPendingIntent );
Toast.makeText(context, "wei...", Toast.LENGTH_SHORT).show();
appWidgetManager.updateAppWidget(appWidgetId, remoteViews);
}
}
关注Broadcast.java
public class Broadcast extends BroadcastReceiver{
private static int clickCount = 0;
private Button btnstart;
private Handler myHandler = new Handler();
private long startTime = 0L;
long timeInMillies = 0L;
long timeSwap = 0L;
long finalTime = 0L;
private boolean stopped = false;
private boolean start_running = true;
TextView textTimer;
RemoteViews remoteViews;
private int REFRESH_RATE = 100;
ComponentName widget;
AppWidgetManager awManager;
private Runnable startStopwatch = new Runnable(){
public void run(){
if(stopped){
timeInMillies =SystemClock.uptimeMillis()-startTime;
finalTime=timeSwap+timeInMillies;
int seconds=(int)(finalTime/1000);
int minutes=seconds/60;
int hours = minutes/60;
seconds=seconds%60;
int milliseconds = (int)(finalTime%1000);
remoteViews.setTextViewText(R.id.textView1, String.format("%02d : %02d : %02d", hours, minutes, seconds));
if(stopped){
myHandler.postDelayed(this, REFRESH_RATE);
awManager.updateAppWidget(widget, remoteViews);
}else{
finalTime=0;
timeSwap=0;
myHandler.removeCallbacks(startStopwatch);
awManager.updateAppWidget(widget, remoteViews);
}
}
}
};
public void onReceive(Context context, Intent intent) {
remoteViews = new RemoteViews(context.getPackageName(), R.layout.main);
widget = new ComponentName(context, Main.class);
awManager = AppWidgetManager.getInstance(context);
if(intent.getAction().equals("android.intent.action.UPDATE")){
startTime=SystemClock.uptimeMillis();
stopped = true;
start_running = false;
remoteViews.setOnClickPendingIntent(R.id.button1,null );
myHandler.postDelayed(startStopwatch, REFRESH_RATE);
awManager.updateAppWidget(widget, remoteViews);
Toast.makeText(context, "start", Toast.LENGTH_SHORT).show();
}else{
if(intent.getAction().equals("android.intent.action.UPDATEd"))
timeSwap += timeInMillies;
stopped = false;
start_running = false;
remoteViews.setOnClickPendingIntent(R.id.button2, null);
myHandler.removeCallbacks(startStopwatch);
awManager.updateAppWidget(widget, remoteViews);
Toast.makeText(context, "stop", Toast.LENGTH_SHORT).show();
}
}
}
答案 0 :(得分:0)
我也遇到了问题。 您无法在广播接收器中保留远程视图的参考。在每个循环中,您必须反复创建远程视图。 我正在使用该警报管理器并将时间间隔设置为10毫秒
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.add(Calendar.MILLISECOND, 10);
alarmManager.setRepeating(AlarmManager.RTC, calendar.getTimeInMillis(), 10, createClockIntent(context, 1, TIMER_INTENT_REQUEST_CODE));
和克里特时钟意图为您的广播接收器简单的待定意图。 我知道这个小部件并不是一个好主意,所以将秒表创建为小部件。