質問が繰り返されるか単純な場合は、お詫び申し上げます。長い間この問題に苦しんでいます。
アプリがバックグラウンドに切り替えられている場合でも、ユーザーの位置を追跡する必要があります。サーフィンで、ロケーションプロセッサコードをサービスとして記述できることがわかりました。これにより、サービスが強制終了されず、ユーザーのロケーションを取得できます(ステータス-フォアグラウンドのアプリとバックグラウンドで実行されているアプリの両方)。
アプリがフォアグラウンドにあるとき、ユーザーの位置を継続的に追跡することができました。しかし、アプリをバックグラウンドに切り替えると、位置情報サービスが停止し、ユーザーの位置を追跡できなくなったように感じます。以下の私のコードを見つけてください:
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);
}
}
}
誰かが私が間違っていることを訂正してくれませんか。
ありがとうございました。