我想制作一个CountDown计时器的Widget。我想让它每秒更新一次。以下是我的代码。 onUpdate()
除了一次开始之外,根本没有被召唤。请指出我的问题。提前谢谢。
JAVA代码
public class MainActivity extends AppWidgetProvider {
private boolean isTimeComplete = false;
private int days, hours, minutes, seconds;
private PendingIntent pendingIntent;
@Override
public void onEnabled(Context context) {
super.onEnabled(context);
AlarmManager alarmManager = (AlarmManager) context
.getSystemService(Context.ALARM_SERVICE);
Intent alarm = new Intent(context, MainActivity.class);
pendingIntent = PendingIntent.getService(context, 0, alarm,
PendingIntent.FLAG_CANCEL_CURRENT);
alarmManager.setRepeating(AlarmManager.RTC, System.currentTimeMillis(),
1000, pendingIntent);
ComponentName thisWidget = new ComponentName(context,
MainActivity.class);
AppWidgetManager manager = AppWidgetManager.getInstance(context);
manager.updateAppWidget(thisWidget,
new RemoteViews(context.getPackageName(),
R.layout.activity_main));
int appWidgetIds[] = manager.getAppWidgetIds(thisWidget);
manager.notifyAppWidgetViewDataChanged(appWidgetIds, R.id.text_value);
}
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager,
int[] appWidgetIds) {
RemoteViews remoteViews = new RemoteViews(context.getPackageName(),
R.layout.activity_main);
ComponentName timerWidget = new ComponentName(context,
MainActivity.class);
SimpleDateFormat format = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss",
Locale.US);
Date d2 = null;
try {
d2 = format.parse("2014/10/04 10:00:00");
} catch (ParseException e) {
e.printStackTrace();
}
Calendar cal = Calendar.getInstance();
long diff = d2.getTime() - cal.getTime().getTime();
seconds = (int) (diff / 1000) % 60;
minutes = (int) ((diff / (1000 * 60)) % 60);
hours = (int) ((diff / (1000 * 60 * 60)) % 24);
days = (int) (diff / (1000 * 60 * 60 * 24));
remoteViews.setTextViewText(R.id.text_remaining,
context.getString(R.string.time_remaining_text));
remoteViews.setTextViewText(
R.id.text_value,
String.format(context.getString(R.string.text_days),
days)
+ " "
+ String.format(
context.getString(R.string.text_hours),
hours)
+ " "
+ String.format(context.getString(R.string.text_minutes),
minutes)
+ " "
+ String.format(context.getString(R.string.text_seconds),
seconds));
appWidgetManager.updateAppWidget(timerWidget, remoteViews);
}
}
提供商XML
<?xml version="1.0" encoding="utf-8"?>
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:initialLayout="@layout/activity_main"
android:minHeight="144dp"
android:minWidth="146dp"
android:updatePeriodMillis="1000" />
清单
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.poc_timerwidget"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="19" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<receiver
android:name=".MainActivity"
android:label="Timer Widget" >
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
</intent-filter>
<meta-data
android:name="android.appwidget.provider"
android:resource="@xml/timer_widget_provider" />
</receiver>
</application>
</manifest>
答案 0 :(得分:6)
<强>问题:强>
AppWidgetProvider
是一个BroadcastReceiver
。您正在检索启动服务的PendingIntent
,而不是检索发送广播的PendingIntent
。ACTION_APPWIDGET_UPDATE
的intent-filter。但是,未对传递给警报管理器的待处理意图设置操作AppWidgetManager.ACTION_APPWIDGET_UPDATE
。您还需要在待处理的意图上设置额外值AppWidgetManager.EXTRA_APPWIDGET_IDS
。设置重复闹钟以更新小部件:
Intent alarm = new Intent(context, MainActivity.class);
alarm.setAction(AppWidgetManager.ACTION_APPWIDGET_UPDATE); // Set appwidget update action
alarm.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, appWidgetIds); // Set appwidget ids to be updated
pendingIntent = PendingIntent.getBroadcast(context, 0, alarm,
PendingIntent.FLAG_CANCEL_CURRENT); // get the broadcast pending intent
alarmManager.setRepeating(AlarmManager.RTC, System.currentTimeMillis(),
1000, pendingIntent); // set repeat alarm
请务必将updatePeriodMillis
设置为0.来自docs:
将警报类型设置为ELAPSED_REALTIME或RTC,这只是 当设备处于唤醒状态时发出警报。然后设置 updatePeriodMillis为零(&#34; 0&#34;)。