我正在开发一个android服务,通过短信发送gps位置。该服务应定期发出该位置,并且永远不要停止,除非用户明确停止该服务。
我不在乎电池的使用情况,因为设备大多数时候都可以插上电源。
尽管只要设备在使用中它似乎可以正常工作,但只要设备进入睡眠模式,其短信间隔就会变慢。 (问题:配置为每10分钟发送一次,但是当设备处于非活动状态时,它仅每30〜40分钟发送一次)。
有人可以给我一个提示/告诉我我在做什么错吗?
public class SmsSendingService extends Service {
private LocationManager locationManager;
private Timer timer;
private TimerTask timerTask;
private Location currentLocation = null;
public static final String tag = "SMSSending Service";
public static final int NotificationId = 12362542;
public SmsSendingService() {
super();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
super.onStartCommand(intent, flags, startId);
Intent notificationIntent = new Intent(this, MainActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this,
0, notificationIntent, 0);
Notification notification = new NotificationCompat.Builder(this)
.setContentTitle("Sms Sending Service")
.setContentText("sending location every 10 minutes")
.setSmallIcon(R.drawable.ic_contact_phone_black_24dp)
.setContentIntent(pendingIntent)
.build();
startForeground(NotificationId, notification);
startTimer();
Log.d(tag, "onStartCommand");
return Service.START_STICKY;
}
@Override
public void onDestroy() {
super.onDestroy();
locationManager.removeUpdates(locationListener);
stoptimertask();
Intent broadcastIntent = new Intent("at.rm.smslocationsharer.restartsendingsms");
sendBroadcast(broadcastIntent);
Log.d(tag, "onDestroy");
}
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onTaskRemoved(Intent rootIntent) {
super.onTaskRemoved(rootIntent);
}
public void startTimer() {
timer = new Timer();
initializeTimerTask();
Log.d(tag, "start timer");
timer.schedule(timerTask, 1000, 600 * 1000);
}
public void initializeTimerTask() {
timerTask = new TimerTask() {
public void run() {
sendSms();
}
};
}
public void stoptimertask() {
if (timer != null) {
Log.d(tag, "stop timer");
timer.cancel();
timer = null;
}
}
@SuppressLint("MissingPermission")
public void sendSms() {
String decimalSeparator = ".";
String longitudeLatitudeSeparator ",";
if (locationManager == null) {
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 10000, 1, locationListener, Looper.getMainLooper());
}
if (!HasPermissionForGps() || !HasPermissionForSms()) {
return;
}
if (currentLocation == null) {
currentLocation = getLastKnownLocation();
}
if (currentLocation == null) {
return;
}
String longitude = format(currentLocation.getLongitude(), decimalSeparator.charAt(0));
String latitutude = format(currentLocation.getLatitude(), decimalSeparator.charAt(0));
String textToSend = latitutude + longitudeLatitudeSeparator + longitude;
try {
SmsManager smsManager = SmsManager.getDefault();
smsManager.sendTextMessage(12345678, null, textToSend, null, null);
} catch (Exception ex) {
Log.e("SmsSendingService Error", ex.getMessage(), ex);
}
}
private Location getLastKnownLocation() {
List<String> providers = locationManager.getProviders(true);
Location bestLocation = null;
for (String provider : providers) {
@SuppressLint("MissingPermission") Location loc = locationManager.getLastKnownLocation(provider);
if (loc == null) {
continue;
}
if (bestLocation == null || loc.getAccuracy() < bestLocation.getAccuracy()) {
// Found best last known location: %s", l);
bestLocation = loc;
}
}
return bestLocation;
}
private boolean HasPermissionForSms() {
return ContextCompat.checkSelfPermission(this, Manifest.permission.SEND_SMS) == PackageManager.PERMISSION_GRANTED;
}
private boolean HasPermissionForGps()
{
return ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED &&
ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED;
}
public static String format(double num, char separator) {
DecimalFormatSymbols decimalSymbols = DecimalFormatSymbols.getInstance();
decimalSymbols.setDecimalSeparator(separator);
return new DecimalFormat("0.00000", decimalSymbols).format(num);
}
LocationListener locationListener = new LocationListener() {
public void onLocationChanged(Location location) {
currentLocation = location;
}
public void onStatusChanged(String provider, int status, Bundle extras) {
}
public void onProviderEnabled(String provider) {
}
public void onProviderDisabled(String provider) {
}
};}