我的android小部件服务在一段时间后停止

时间:2013-05-17 10:53:34

标签: android service widget clock

我的时钟小部件有问题。我运行一个服务来在需要时刷新它(在USER_PRESENT事件和屏幕打开时每分钟),但是这个服务似乎在一段时间后被随机杀死,并且没有重新启动。

我已经在我的服务的onDestroy()和onLowMemory()方法中设置了通知,但它们从未被调用过。

我从不调用stopSelf()方法。

在启动的应用程序列表中,小部件不再可见于系统中。

当WidgetProvider自然更新(每6个小时)时,服务会重新启动,并且所有工作都会完美运行。

这是我的代码:

(请注意,清单中的应用程序未在代码中命名或创建。只有提供者和服务。不知道它是否正确)

小工具提供商:

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>

0 个答案:

没有答案
相关问题