我的时钟小部件有问题。我在需要时运行一个服务来刷新它(在 USER_PRESENT 事件和屏幕打开时的每一分钟),但是这个服务似乎在一段时间后被随机杀死,并且没有重新启动。
我在我的服务的 onDestroy() 和 onLowMemory() 方法中设置了通知,但它们从未被调用。
我从不调用 stopSelf() 方法。
该小部件在系统中已启动的应用程序列表中不再可见。
当 WidgetProvider 自然更新(每 6 小时)时,该服务将重新启动,并且一切正常运行片刻。
这是我的代码:
(注意manifest中的Application在代码中没有命名也没有创建,只有provider和service,不知道对不对)
小部件提供者:
public class WidgetProvider extends AppWidgetProvider {
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] ids) {
super.onUpdate(context, appWidgetManager, ids);
Intent serviceIntent = new Intent(context.getApplicationContext(), WidgetService.class);
context.startService(serviceIntent);
}
@Override
public void onDeleted(Context context, int[] appWidgetIds) {
super.onDeleted(context, appWidgetIds);
}
@Override
public void onDisabled(Context context) {
Intent serviceIntent = new Intent(context.getApplicationContext(), WidgetService.class);
context.stopService(serviceIntent);
super.onDisabled(context);
}
}
服务 :
public class WidgetService extends Service {
private static String LOG = WidgetService.class.getSimpleName();
public Hashtable<Integer, ClockView> views = new Hashtable<Integer, ClockView>();
AppWidgetManager man;
private static WidgetService self;
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
Toast.makeText(this, LOG+" configuration change", Toast.LENGTH_LONG).show();
}
@Override
public void onCreate() {
super.onCreate();
man = AppWidgetManager.getInstance(this);
self = this;
// Registering Intent for screen state
...
registerReceiver(screenoffReceiver, filter);
// Registering Intent for click event
...
UpdateAllWidgets();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
UpdateAllWidgets();
return START_STICKY;
}
@Override
public void onDestroy() {
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.holder)
.setContentTitle("WidgetClock")
.setContentText("Destroyed at "+Calendar.getInstance().getTime());
((NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE)).notify(1, mBuilder.build());
super.onDestroy();
}
@Override
public void onLowMemory() {
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.holder)
.setContentTitle("WidgetClock")
.setContentText("LowMemory at "+Calendar.getInstance().getTime());
((NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE)).notify(1, mBuilder.build());
super.onLowMemory();
}
public void onScreenOff() {
}
private void onScreenOn() {
}
public void onUserPresent() {
}
public void UpdateAllWidgets() {
views.clear();
int[] ids = man.getAppWidgetIds(new ComponentName(this, WidgetProvider.class));
for (int id : ids)
createViewIfNecessary(id);
}
private void createViewIfNecessary(int id) {
if (!views.containsKey(id)) {
ClockView view = new ClockView(this, id);
views.put(id, view);
}
}
public static Context getApp(){
return self;
}
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
}
清单:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.widgetclock"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme"
android:clickable="true" >
<service android:name="WidgetService"></service>
<receiver android:name="WidgetProvider"
android:clickable="true">
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
</intent-filter>
<meta-data android:name="android.appwidget.provider"
android:resource="@xml/widget_provider_file" />
</receiver>
</application>
小部件提供程序配置 XML
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
android:minWidth="250dip"
android:minHeight="125dip"
android:updatePeriodMillis="21600000"
android:initialLayout="@layout/widget_main">
</appwidget-provider>