应用程序几个小时后自杀,不想使用startForeground

时间:2013-09-13 07:12:55

标签: android service cllocationmanager

我正在使用服务GPS以及Network

获取坐标

我正在使用Alarm Manager每隔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分钟激活但经过一段时间它会杀死或操作系统让它杀死, 我不想使用前台服务

我的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;
            }
        }

它让我在一段时间后杀死了应用程序的坐标,我怎么能以正确的方式处理它 因此,它清除RAM并再次激活我的应用程序

我不希望应用程序被杀死,我们是否有任何解决方案

请提供帮助,提前致谢

1 个答案:

答案 0 :(得分: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/