我正在尝试编写一个后台服务,只要每3分钟后距离变化超过650米,就会通过短信分享位置。
代码工作正常,但是当我停止旅行时,服务会在几分钟后自动关闭。我想让应用程序一直运行,以便应用程序在人们旅行时发送数据。
这是我写的代码..
Main.java
package com.example.locationtest;
import android.support.v7.app.ActionBarActivity;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
// Method to start the service
public void startService(View view) {
startService(new Intent(getBaseContext(), BackgroundService.class));
}
// Method to stop the service
public void stopService(View view) {
stopService(new Intent(getBaseContext(), BackgroundService.class));
}
}
BackgroundService.java
package com.example.locationtest;
import java.util.Date;
import java.util.List;
import android.annotation.SuppressLint;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.IBinder;
import android.os.Looper;
import android.util.Log;
import android.widget.Toast;
public class BackgroundService extends Service {
private static DataSharing data=new DataSharing();
private Thread triggerService;
protected LocationManager locationManager;
protected MyLocationListener MyLocationListener;
protected Criteria criteria;
public static final int MIN_TIME = 18000;
public static final long MIN_DISTANCE = 650;
private SharedPreferences settings;
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId)
{
data.setContext(getApplicationContext());
addLocationListener();
return START_STICKY;
}
private void addLocationListener()
{
triggerService = new Thread(new Runnable(){
public void run(){
try{
Looper.prepare();
locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
criteria = new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_FINE);
final String PROVIDER = locationManager.getBestProvider(criteria, true);
updateLocation(getLastBestLocation(MIN_TIME, MIN_DISTANCE));
MyLocationListener = new MyLocationListener();
locationManager.requestLocationUpdates(PROVIDER, MIN_TIME, MIN_DISTANCE, MyLocationListener);
Looper.loop();
}catch(Exception ex){
ex.printStackTrace();
}
}
}, "LocationThread");
triggerService.start();
}
public Location getLastBestLocation(int minDistance, long minTime) {
Location bestResult = null;
float bestAccuracy = Float.MAX_VALUE;
long bestTime = Long.MIN_VALUE;
List<String> matchingProviders = locationManager.getAllProviders();
for (String provider: matchingProviders) {
Location location = locationManager.getLastKnownLocation(provider);
if (location != null) {
float accuracy = location.getAccuracy();
long time = location.getTime();
if ((time > minTime && accuracy < bestAccuracy)) {
bestResult = location;
bestAccuracy = accuracy;
bestTime = time;
}
else if (time < minTime && bestAccuracy == Float.MAX_VALUE && time > bestTime) {
bestResult = location;
bestTime = time;
}
}
}
return bestResult;
}
public static void updateLocation(Location location)
{
double latitude, longitude;
latitude = location.getLatitude();
longitude = location.getLongitude();
data.sendSms(latitude, longitude, new Date());
}
class MyLocationListener implements LocationListener
{
@Override
public void onLocationChanged(Location location)
{
updateLocation(location);
}
@Override
public void onProviderDisabled(String provider) {
}
@Override
public void onProviderEnabled(String provider) {
}
@Override
public void onStatusChanged(String provider, int status,
Bundle extras) {
// TODO Auto-generated method stub
}
}
}
DataSharing.java
package com.example.locationtest;
import java.util.Date;
import android.content.Context;
import android.telephony.SmsManager;
import android.telephony.TelephonyManager;
public class DataSharing {
String message="";
String phoneNo="0300phone#";
Context mAppContext;
public DataSharing()
{
}
public DataSharing(Context context)
{
mAppContext=context;
}
public void setContext(Context context)
{
mAppContext=context;
}
public void sendSms(double clat,double clong,Date d)
{
message="Traffic-Advisor123 Latitude:"+String.valueOf(clat)+"\nLongitude:"+String.valueOf(clong)+"\nTime:"+String.valueOf(d);
try {
SmsManager smsManager = SmsManager.getDefault();
smsManager.sendTextMessage(phoneNo, null, message, null, null);
} catch (Exception e) {
e.printStackTrace();
}
}
public void sendViaNetwork(double clat,double clong,Date d)
{
TelephonyManager tMgr = (TelephonyManager)mAppContext.getSystemService(Context.TELEPHONY_SERVICE);
String mPhoneNumber = tMgr.getLine1Number();
}
}
我面临的另一个问题是,即使在3分钟过去之前,有时也会发送短信。可能是因为其他一些应用程序要求获取与我的应用程序共享的位置。如果数据的间隔不少于2分钟,我只能处理数据。如何将其限制在2分钟以上?
我正在寻找一些指导。谢谢。
答案 0 :(得分:2)
服务实际上不应该在后台持续运行。也许你应该使用BroadcastReceiver,这个问题的答案可能正是你要找的:BroadcastReceiver for location