0

我正在使用服务GPS来 获取坐标Network

我正在使用警报管理器每 2 分钟激活一次服务,例如

Intent myIntent=new Intent(Main.this,ServiceNew.class);
PendingIntent pendingIntent=PendingIntent.getService(OkayaMain.this,0,myIntent,0);
AlarmManager alarmManager=(AlarmManager)getSystemService(ALARM_SERVICE);

Calendar cal=Calendar.getInstance();
cal.add(Calendar.MINUTE,0);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,cal.getTimeInMillis(),UPDATE_INTERVAL,pendingIntent);

它每 2 分钟激活一次但一段时间后它会杀死或 OS 让它杀死,我不想使用前台服务

我的 ServiceClass 是 ServiceNew.class

   public class ServiceNew extends Service
        {

        private class LocationListener implements android.location.LocationListener{
            Location mLastLocation;
            public LocationListener(String provider)
            {
                Log.e(TAG, "LocationListener " + provider);
                mLastLocation = new Location(provider);
            }
            @Override
            public void onLocationChanged(Location location)
            {
                Log.e(TAG, "onLocationChanged: " + location);
                mLastLocation.set(location);
                dLat=location.getLatitude();
                dLng=location.getLongitude();
                sCoordinateType=location.getProvider();
            }
            @Override
            public void onProviderDisabled(String provider)
            {
                Log.e(TAG, "onProviderDisabled: " + provider);            
            }
            @Override
            public void onProviderEnabled(String provider)
            {
                Log.e(TAG, "onProviderEnabled: " + provider);
            }
            @Override
            public void onStatusChanged(String provider, int status, Bundle extras)
            {
                Log.e(TAG, "onStatusChanged: " + provider);
            }
        } 
        LocationListener[] mLocationListeners = new LocationListener[] {
                new LocationListener(LocationManager.GPS_PROVIDER),
                new LocationListener(LocationManager.NETWORK_PROVIDER)
        };
        @Override
        public IBinder onBind(Intent arg0)
        {
            return null;
        }
        @Override
        public int onStartCommand(Intent intent, int flags, int startId)
        {
            Log.e(TAG, "onStartCommand");
            super.onStartCommand(intent, flags, startId);       
            return START_STICKY;
        }
        @Override
        public void onCreate()
        {


            Log.e(TAG, "onCreate");
            initializeLocationManager();
            try {
                mLocationManager.requestLocationUpdates(
                        LocationManager.NETWORK_PROVIDER, LOCATION_INTERVAL, LOCATION_DISTANCE,
                        mLocationListeners[1]);
            } catch (java.lang.SecurityException ex) {
                Log.i(TAG, "fail to request location update, ignore", ex);
            } catch (IllegalArgumentException ex) {
                Log.d(TAG, "network provider does not exist, " + ex.getMessage());
            }
            try {
                mLocationManager.requestLocationUpdates(
                        LocationManager.GPS_PROVIDER, LOCATION_INTERVAL, LOCATION_DISTANCE,
                        mLocationListeners[0]);
            } catch (java.lang.SecurityException ex) {
                Log.i(TAG, "fail to request location update, ignore", ex);
            } catch (IllegalArgumentException ex) {
                Log.d(TAG, "gps provider does not exist " + ex.getMessage());
            }

            fnPushLocation();
        }

        private void fnPushLocation(){
            try{
                context=getApplicationContext();
                timer1=new Timer();
                checkTime=new TimerTask(){
                    public void run(){
                        if(lIteration>=1){
                            if(timer1!=null){
                                timer1.cancel();
                                timer1=null;
                            }
                            SaveLocation();
                        }
                        lIteration++;
                    }
                };
                timer1.schedule(checkTime,0,UPDATE_INTERVAL);
            }
            catch(Exception ex){
                sResponse=ex.toString();
                //fnShowMessage(sResponse);
                return;
            }
        }

它让我在一段时间后得到坐标 android 杀死应用程序,我怎样才能以适当的方式处理这个问题,以便它清除 RAM 并再次激活我的应用程序

我不希望应用程序被杀死,我们有什么解决方案吗?

请帮助,在此先感谢

4

1 回答 1

0
 public void killSelf(Context context) {
            ActivityManager am = (ActivityManager) context
                            .getSystemService(Activity.ACTIVITY_SERVICE);
            am.killBackgroundProcesses(getPackageName());

            if (mServiceList != null) {
                    for (RunningServiceInfo service : mServiceList) {
                            context.stopService(new Intent().setComponent(service.service));
                    }
            }
    }

请点击以下链接以供参考。我希望它对你有用..

http://code.google.com/p/my-test-project-lion/

于 2013-09-13T08:44:37.833 回答