【问题标题】:My android location service killed when app switched to background当应用程序切换到后台时,我的 android 位置服务被终止
【发布时间】:2013-03-01 19:45:17
【问题描述】:

如果我的问题重复或简单,请向我道歉。在这个问题上纠结了很久。

即使我的应用切换到后台,我也需要跟踪用户的位置。在网上冲浪时,我发现可以将位置处理器代码编写为服务,这样服务就不会被杀死,我们可以获取用户的位置(在两种状态 - 应用程序在前台和应用程序在后台运行时)。

当我的应用程序处于前台时,我能够持续跟踪用户的位置。但是,当我将应用程序切换到后台时,我觉得我的位置服务停止了,并且我无法跟踪用户的位置。请在下面找到我的代码:

AndroidManifest.xml

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.testmylocation"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="15" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity"
            android:label="@string/title_activity_main" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <service android:name=".MyService"/>
    </application>
</manifest>

MainActivity.java

package com.example.testmylocation;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.Window;

public class MainActivity extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_main);

        startService(new Intent(this, MyService.class)); 
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }
}

MyService.java

package com.example.testmylocation;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.net.URL;
import java.net.URLConnection;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.json.JSONArray;
import org.json.JSONObject;

import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.location.Location;
import android.location.LocationManager;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.util.Log;

public class MyService extends Service
{
    private static final String TAG = "TestMyLocation";
    private LocationManager mLocationManager = null;
    private static final int LOCATION_INTERVAL = 0;
    private static final float LOCATION_DISTANCE = 0;
    long itsBatchId = 0;

    private class LocationListener implements android.location.LocationListener
    {
        Location mLastLocation;
        public LocationListener(String provider)
        {
            mLastLocation = new Location(provider);
        }

        @Override
        public void onLocationChanged(Location location)
        {        
            Thread aThread = new Thread(new Runnable() {
                @Override
                public void run() {
                    sendLocationValues(location);
                }
            });
            aThread.start();
        }

        @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);
        }
    } 

    private void sendLocationValues(Location theLocation) 
    {
        //A web service will be called and the user's current location will be stored in server
    }

    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)
    {
        super.onStartCommand(intent, flags, startId);       
        return START_STICKY;
    }

    @Override
    public void onCreate()
    {
        initializeLocationManager();    
        itsLocationHandler.sendEmptyMessage(1);
    }

    private Handler itsLocationHandler = new Handler() 
    {
        @Override
        public void handleMessage(Message theMessage) 
        {
            if(theMessage.what == 1)
            {
                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());
                }
            }
        }
    };

    @Override
    public void onDestroy()
    {
        super.onDestroy();
    } 

    private void initializeLocationManager() 
    {
        if (mLocationManager == null) 
        {
            mLocationManager = (LocationManager) getApplicationContext().getSystemService(Context.LOCATION_SERVICE);
        }
    }
}

谁能纠正我做错了什么。

谢谢。

【问题讨论】:

  • 您需要对用户进行持续跟踪还是可以间隔进行?
  • 我也可以每隔一段时间跟踪它的位置。仅出于测试目的,我使用了 LocationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, mLocationListeners[1]);

标签: android android-location


【解决方案1】:

自 Android 4.0 以来,操作系统在杀死不必要的进程方面变得更加积极。如果即使您的应用程序在后台,您也需要跟踪用户的位置,那么您需要将您的服务声明为前台服务。这提高了服务的优先级,因此除非它真的需要资源,否则 Android 不太可能杀死它。有关如何执行此操作的详细信息,请参阅Running a service in the foreground

【讨论】:

  • 我尝试使用 startForeground(),现在我的定位服务似乎在后台正常运行。谢谢你。但是,在调查接收到的位置坐标时,我发现有时位置坐标没有正确捕获。没有位置捕获的时间间隔很少。我确信我的服务似乎运行正常(能够在通知栏中看到我的应用程序图标)。如何进一步调整我的位置服务,以便没有时间间隔(应用程序背景)并且我定期接收位置。请帮忙,再次感谢您的帮助。
  • 无法保证您会定期收到回调。您传递给requestLocationUpdates() 的参数只是提示。每个制造商都自己实现此代码,他们都需要在性能、准确性和电池寿命之间进行权衡。我唯一能建议的就是获得一个唤醒锁,这样手机就不会在你期待位置时进入睡眠状态。这可能是影响回调频率的因素之一。请意识到,如果您这样做,将对电池寿命产生重大负面影响。
【解决方案2】:

位置处理器代码可以写成服务,这样服务就不会被杀死

没有什么可以保证服务永远不会被杀死,但是您可以通过获取WakeLock 并以Foreground service 启动它来增加服务继续运行的可能性

我建议你检查一些开源项目,例如 Open GPS TrackerTraceper

【讨论】:

  • 我尝试使用 startForeground(),现在我的定位服务似乎在后台正常运行。谢谢你。但是,在调查接收到的位置坐标时,我发现有时位置坐标没有正确捕获。没有位置捕获的时间间隔很少。我确信我的服务似乎运行正常(能够在通知栏中看到我的应用程序图标)。如何进一步调整我的位置服务,以便没有时间间隔(应用程序背景)并且我定期接收位置。请帮忙,再次感谢您的帮助。
猜你喜欢
  • 1970-01-01
  • 2011-09-05
  • 2019-09-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-12-13
  • 2011-11-16
相关资源
最近更新 更多