我有一个小部件,使用LocationManager显示当前用户位置。我通过系统广播管理器禁用本地广播管理器进行位置更新来节省电池,该管理器指示屏幕是打开还是关闭。为了节省电池,我正在尝试实现一个GpsStatus.Listener告诉我gps是否已打开或关闭,以便我可以在我的位置管理器中使用它。如果只在GPS打开的情况下加载小部件,一切都能正常工作。如果在GPS关闭时加载小部件,则无法触发GpsStatus.Listener。显然有一个无法解决的解决方法(加载gps)但我很好奇,并问为什么如果gps在运行时关闭,则监听器不起作用。我正在使用os 4.0.3,我的清单包括精美的位置。 TA
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager,
int[] appWidgetIds)
{
// Get all ids
thisWidget = new ComponentName(context, MyWidgetProvider.class);
this.appWidgetManager = appWidgetManager;
int[] allWidgetIds = appWidgetManager.getAppWidgetIds(thisWidget);
remoteViews = new RemoteViews(context.getPackageName(), R.layout.main);
remoteViews.setTextViewText(R.id.update, "running......");
appWidgetManager.updateAppWidget(thisWidget, remoteViews);
ServicesManager = new MyLocationManager(context);
streetAddress = new CellAddress(context, ServicesManager);
ServicesManager.lm.addGpsStatusListener(myGPSListener);
filter = new IntentFilter(Intent.ACTION_SCREEN_ON);
filter.addAction(Intent.ACTION_SCREEN_OFF);
context.getApplicationContext().registerReceiver(mReceiver, filter);
LocalBroadcastManager.getInstance(context).registerReceiver(receiveUpdatedLocation,
new IntentFilter("sendUpdatedLocation"));
if (!(ServicesManager.isInternet()))
{
remoteViews.setTextViewText(R.id.update, "Address not available" + "\n\n" +
"Check data connection....");
appWidgetManager.updateAppWidget(thisWidget, remoteViews);
}
else
{
remoteViews.setTextViewText(R.id.update, streetAddress.address$);
appWidgetManager.updateAppWidget(thisWidget, remoteViews);
}
Log.d(TAG, "onUpdate");
}
GpsStatus.Listener myGPSListener = new GpsStatus.Listener()
{
public void onGpsStatusChanged(int event)
{
if(event == GpsStatus.GPS_EVENT_STARTED)
{
Log.d(TAG, "GPS Started");
} else if(event == GpsStatus.GPS_EVENT_STOPPED) {
Log.d(TAG, "GPS Stopped");
}
}
};