I have created a widget to display the current date which refreshes every 5-10 seconds. Will be increasing the duration later. Created an alarm in the onUpdate method to start the service which in turn updates the widget with the date. When I invoke the widget nothing happens. Don't know if the problem is with the alarm or the service. Pasted the code below. Please let me know where I may be going wrong..
Thanks, Sam
Main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:id="@+id/scores"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
android:paddingTop="35dip" android:paddingLeft="29dip" android:textColor="#000000"/>
</LinearLayout>
Widget Provider xml
<?xml version="1.0" encoding="utf-8"?>
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
android:minWidth="146dip"
android:minHeight="72dip"
android:updatePeriodMillis="1000"
android:initialLayout="@layout/main"
/>
Android Manifest xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.helloandroid.worldcupscores"
android:versionCode="1"
android:versionName="1.0">
<application android:icon="@drawable/icon" and开发者_运维知识库roid:label="@string/app_name">
<!-- Broadcast Receiver that will process AppWidget updates -->
<receiver android:name=".WorldCupScores" android:label="@string/widget_name">
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
</intent-filter>
<meta-data android:name="android.appwidget.provider" android:resource="@xml/scores_widget_provider" />
</receiver>
<!-- Service to perform web API queries -->
<service android:name="WorldCupScores$UpdateService" />
</application>
<uses-permission android:name="android.permission.INTERNET" />
<uses-sdk android:minSdkVersion="7" />
</manifest>
WorldCupScores.java
package com.helloandroid.worldcupscores;
import android.app.Activity;
import android.os.Bundle;
import java.util.Date;
import android.app.PendingIntent;
import android.app.Service;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.res.Resources;
import android.net.Uri;
import android.os.IBinder;
import android.text.format.Time;
import android.util.Log;
import android.widget.RemoteViews;
import android.app.Activity;
import android.app.AlarmManager;
import java.util.Calendar;
import java.util.GregorianCalendar;
import android.widget.Toast;
public class WorldCupScores extends AppWidgetProvider {
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager,
int[] appWidgetIds) {
AlarmManager alarmManager;
Intent intent = new Intent(context, UpdateService.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0,
intent, PendingIntent.FLAG_UPDATE_CURRENT);
alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
Calendar cal = Calendar.getInstance();
cal.setTimeInMillis(System.currentTimeMillis());
cal.add(Calendar.SECOND, 10);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), 5*1000, pendingIntent);
}
public static class UpdateService extends Service {
@Override
public void onStart(Intent intent, int startId) {
// Build the widget update for today
RemoteViews updateViews = new RemoteViews(this.getPackageName(),
R.layout.main);
Date date = new Date();
updateViews.setTextViewText(R.id.scores, "Current Time "
+ date);
// Push update for this widget to the home screen
ComponentName thisWidget = new ComponentName(this, WorldCupScores.class);
AppWidgetManager manager = AppWidgetManager.getInstance(this);
manager.updateAppWidget(thisWidget, updateViews);
}
@Override
public IBinder onBind(Intent intent) {
// We don't need to bind to this service
return null;
}
}
}
change
PendingIntent.FLAG_UPDATE_CURRENT
into
intent.FLAG_ACTIVITY_NEW_TASK
精彩评论