以固定间隔获取GPS位置的后台任务。 AlarmManager服务Android

时间:2016-12-18 20:34:20

标签: android service background alarmmanager

我想创建每隔五分钟在后台获取位置的应用程序。 我尝试做了一些事情,但是我遇到了AlarmManager的麻烦,因为第一个Toast出现了,但是第二个Toast出现了,它并没有获得位置。 你有什么想法,告诉我有什么不好吗?

的AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>

<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" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <meta-data
        android:name="com.google.android.gms.version"
        android:value="@integer/google_play_services_version" />

    <activity
        android:name=".MainActivity"
        android:label="@string/app_name"
        android:theme="@style/AppTheme.NoActionBar">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity
        android:name=".AfterLoginActivity"
        android:label="@string/title_activity_login"
        android:theme="@style/AppTheme.NoActionBar" />
    <activity
        android:name=".RegisterActivity"
        android:label="@string/title_activity_register"
        android:theme="@style/AppTheme.NoActionBar" />
    <activity
        android:name=".ActualLocationActivity"
        android:label="@string/title_activity_actual_location"
        android:theme="@style/AppTheme.NoActionBar" />
    <activity
        android:name=".DetailsActivity"
        android:label="@string/title_activity_details"
        android:theme="@style/AppTheme.NoActionBar"></activity>
    <service android:enabled="true" android:name="LocationUpdaterService"></service>
    <receiver android:name="ConnectivityReceiver"></receiver>
</application>

AfterLoginActivity中的OnClickListerner

public void onClickServicesAction(View view) {
Toast.makeText(this, " onclick", Toast.LENGTH_SHORT).show();
Intent intent = new Intent(this, ConnectivityReceiver.class);
PendingIntent pintent = PendingIntent.getBroadcast(this, 0, intent, 0);
AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
alarmManager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP,
        SystemClock.elapsedRealtime()+ (10 * 1000), pintent);

}

ConnectivityReceiver.java

public final class ConnectivityReceiver extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {

    Toast.makeText(context, " onReceive", Toast.LENGTH_SHORT).show();

    final AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
    final PendingIntent wakeupIntent = PendingIntent.getService(context, 0,
            new Intent(context, LocationUpdaterService.class), PendingIntent.FLAG_UPDATE_CURRENT);

    final boolean hasNetwork = !intent.getBooleanExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, false);
    if (hasNetwork) {
        // start service now for doing once
        context.startService(new Intent(context, LocationUpdaterService.class));

        // schedule service for every 10 seconds
        alarmManager.setInexactRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,
                SystemClock.elapsedRealtime() + 10 * 1000,
                10 * 1000, wakeupIntent);
    } else {
        alarmManager.cancel(wakeupIntent);
    }
}

}

LocationUpdaterService.java

public  final class LocationUpdaterService extends Service implements LocationListener {

private String latitude = "default";
private String longitude = "default";

private enum State {
    IDLE, WORKING;
}

private static State state;

private LocationManager locationManager;
private PowerManager.WakeLock wakeLock;

static {
    state = State.IDLE;
}

@Override
public void onCreate() {
    super.onCreate();
    final PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
    this.wakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "LocationUpdaterService");
}


@Override
public IBinder onBind(Intent arg0) {
    return null;
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    if (state == State.IDLE) {
        state = State.WORKING;
        this.wakeLock.acquire();
        // register location updates, not gonna code it you know

    }
    return START_STICKY;
}

@Override
public void onDestroy() {
    super.onDestroy();
    state = State.IDLE;
    if (this.wakeLock.isHeld()) {
        this.wakeLock.release();
    }
}

private void sendToServer(Location location) {
    Toast.makeText(getApplicationContext(), latitude +" "+ longitude, Toast.LENGTH_SHORT).show();
    // send to server in background thread. you might want to start AsyncTask here
}

private void onSendingFinished() {
    // call this after sending finished to stop the service
    this.stopSelf(); //stopSelf will call onDestroy and the WakeLock releases.
    //Be sure to call this after everything is done (handle exceptions and other stuff) so you release a wakeLock
    //or you will end up draining battery like hell
}

@Override
public void onLocationChanged(Location location) {
    if (ContextCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
        locationManager.removeUpdates(this); // you will want to listen for updates only once
        latitude = Double.toString(location.getLatitude());
        longitude = Double.toString(location.getLongitude());
    }
    sendToServer(location);
}

@Override
public void onProviderDisabled(String provider) {
}

@Override
public void onProviderEnabled(String provider) {
}

@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}

}

1 个答案:

答案 0 :(得分:2)

这是我的方式:

data want;
set have;
  mat_month_date=input(have, anydtdte.);
  format want monyy5.;
run;

你可以在开始时启动服务(只开始一次),然后你在服务中请求位置,如果它被定位,你可以做你想要的其他事情。