正在使用LocationListener GPS和融合API。当应用程序进入后台时获取值。使用服务类和广播接收器并将值保存到共享prefrence.Once禁用并再次启用我没有获得首选项值。请帮我解决实际问题。
1.以下代码适用于BroadcastReciever
public class AlarmReceiver extends BroadcastReceiver {
Context context;
@Override
public void onReceive(Context context, Intent intent) {
this.context = context;
System.out.println("Alarm Reciver Called");
if (isMyServiceRunning(this.context, CurrentTenderServiceFragment.GpsServiceFused.class)) {
System.out.println("already running no need to start again");
} else {
Intent background = new Intent(context, CurrentTenderServiceFragment.GpsServiceFused.class);
context.startService(background);
}
}
public static boolean isMyServiceRunning(Context context, Class<?> serviceClass) {
ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
List<ActivityManager.RunningServiceInfo> services = activityManager.getRunningServices(Integer.MAX_VALUE);
if (services != null) {
for (int i = 0; i < services.size(); i++) {
if ((serviceClass.getName()).equals(services.get(i).service.getClassName()) && services.get(i).pid != 0) {
return true;
}
}
}
return false;
}
}`
我也创建了服务类......
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
super.onStartCommand(intent, flags, startId);
boolean stopService = false;
if (intent != null)
stopService = intent.getBooleanExtra("stopservice", false);
System.out.println("stopservice " + stopService);
locationListener = new LocationListener();
if (stopService)
stopLocationUpdates();
else {
if (!mGoogleApiClient.isConnected())
mGoogleApiClient.connect();
}
return START_STICKY;
}
@Override
public void onCreate() {
buildGoogleApiClient();
if (mGoogleApiClient != null) {
mGoogleApiClient.connect();
}
initComponents();
}
清单文件
使用此功能启用和禁用服务
public void enabledBroadcastReceiver() {
PackageManager pm = DrawerHomeActivity.this.getPackageManager();
ComponentName componentName = new ComponentName(DrawerHomeActivity.this, AlarmReceiver.class);
pm.setComponentEnabledSetting(componentName, PackageManager.COMPONENT_ENABLED_STATE_ENABLED,
PackageManager.DONT_KILL_APP);
mContext.startService(new Intent(DrawerHomeActivity.this, CurrentTenderServiceFragment.GpsServiceFused.class));
String alarm = Context.ALARM_SERVICE;
AlarmManager am = (AlarmManager) getSystemService(alarm);
Intent intent = new Intent("REFRESH_THIS");
PendingIntent pi = PendingIntent.getBroadcast(this, 123456789, intent, 0);
int type = AlarmManager.RTC_WAKEUP;
long interval = 1000 * 50;
am.setInexactRepeating(type, System.currentTimeMillis(), interval, pi);
}
public void disabledBroadcastReceiver() {
ComponentName receiver = new ComponentName(DrawerHomeActivity.this, AlarmReceiver.class);
PackageManager pm = DrawerHomeActivity.this.getPackageManager();
pm.setComponentEnabledSetting(receiver,
PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
PackageManager.DONT_KILL_APP);
mContext.stopService(new Intent(DrawerHomeActivity.this, CurrentTenderServiceFragment.GpsServiceFused.class));
String alarm = Context.ALARM_SERVICE;
AlarmManager am = (AlarmManager) getSystemService(alarm);
Intent intent = new Intent("REFRESH_THIS");
PendingIntent pi = PendingIntent.getBroadcast(this, 123456789, intent, 0);
int type = AlarmManager.RTC_WAKEUP;
long interval = 1000 * 50;
am.cancel(pi);
Intent background = new Intent(DrawerHomeActivity.this, AlarmReceiver.class);
mContext.stopService(background);
}