好的,我意识到我的原始代码将近 400 行,所以我将其格式化了一下,只留下了最重要的部分,所以我希望有人能提供帮助。
所以基本上我在后台服务中运行 LocationListener,这里是代码:package als.wakeup;
import java.text.DecimalFormat;
import java.util.Calendar;
import com.google.android.gms.maps.model.LatLng;
import android.app.AlarmManager;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.location.Criteria;
import android.location.GpsStatus;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.location.LocationProvider;
import android.os.BatteryManager;
import android.os.Bundle;
import android.os.IBinder;
import android.support.v4.app.NotificationCompat;
import android.util.Log;
import android.widget.Toast;
public class Awake_Alarm extends Service implements GpsStatus.Listener{
public static final String BROADCAST_ACTION = "als.wakeup.Intro";
private static final int TWO_MINUTES = 1000 * 60 * 2;
public LocationManager locationManager;
public MyLocationListener listener;
public Location previousBestLocation = null;
public ConversePrefs cp;
public ConversePrefs_sets cpss;
int cp_counter = 0;
Intent intent;
int counter = 0;
int batt_level = 0;
private static int alarmID = 2;
private LocationManager mService;
private GpsStatus mStatus;
@Override
public void onCreate() {
super.onCreate();
intent = new Intent(BROADCAST_ACTION);
}
private BroadcastReceiver mBatInfoReceiver = new BroadcastReceiver(){
@Override
public void onReceive(Context ctxt, Intent intent) {
//Functions.
}
};
@Override
public void onStart(Intent intent, int startId) {
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
listener = new MyLocationListener();
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 4000, 0, listener);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 4000, 0, listener);
cp = new ConversePrefs(this);
cpss = new ConversePrefs_sets(this);
mService = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
mService.addGpsStatusListener(this);
}
@Override
public IBinder onBind(Intent intent) {
return null;
}
protected boolean isBetterLocation(Location location, Location currentBestLocation) {
//Functions
}
private boolean isSameProvider(String provider1, String provider2) {
//Functions
}
@Override
public void onDestroy() {
// handler.removeCallbacks(sendUpdatesToUI);
super.onDestroy();
Log.v("STOP_SERVICE", "DONE");
locationManager.removeUpdates(listener);
}
public static Thread performOnBackgroundThread(final Runnable runnable) {
final Thread t = new Thread() {
@Override
public void run() {
try {
runnable.run();
} finally {
}
}
};
t.start();
return t;
}
public class MyLocationListener implements LocationListener
{
public void onLocationChanged(final Location loc)
{
//Functions.
}
public void onProviderDisabled(String provider){}
public void onProviderEnabled(String provider){}
public void onStatusChanged(String provider, int status, Bundle extras){}
}
@Override
public void onGpsStatusChanged(int event) {
mStatus = mService.getGpsStatus(mStatus);
switch (event) {
case GpsStatus.GPS_EVENT_STARTED:
Toast.makeText(getApplicationContext(), "Started", Toast.LENGTH_SHORT).show();
Log.d("GPS", "Start");
break;
case GpsStatus.GPS_EVENT_STOPPED:
Toast.makeText(getApplicationContext(), "Stopped", Toast.LENGTH_SHORT).show();
Log.d("GPS", "Stopped");
break;
case GpsStatus.GPS_EVENT_FIRST_FIX:
Toast.makeText(getApplicationContext(), "Pre-Started", Toast.LENGTH_SHORT).show();
Log.d("GPS", "Pre Start");
break;
case GpsStatus.GPS_EVENT_SATELLITE_STATUS:
break;
}
}
}
但是我的 onGpsStatusChange 只有在服务启动时才会被调用。一旦应用程序启动,它就会运行并显示:预启动、停止、启动。然后,即使我关闭 GPS、Wifi 和移动数据 + 飞行模式,它也不再工作了。
基本布局:
public class Awake_Alarm extends Service implements GpsStatus.Listener{
onStart{
//Registers GpsStatusListener
}
public class MyLocationListener implements LocationListener{
}
@Override
public void onGpsStatusChanged(int event) {
//Functions that get called only once for some reason.
}
}