我想知道如何在Android应用在后台时接收GPS。有没有完整的教程来解释它?在Android的背景下运行GPS监听器
7
A
回答
0
您可以使用始终在后台运行的服务。在后台服务中,您可以获取当前的经纬度,并且可以使用Android地理编码器类将这些lats和long进一步转换为合适的位置。 检查以下链接:
0
,您将在活动做的事情可以在服务完成also..in服务的调用onStart()....
1
您需要使用服务理念而不是活动。 http://developer.android.com/guide/components/services.html
这里是一个很好的例子: Background service needs to send GPS location on server
4
您需要使用AlarmManager激活挂起的意图(通过广播接收器)启动后台服务。
一些示例代码为您
在您的MainActivity
AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
Intent notifyintent = new Intent(this, OnAlarmReceiver.class);
notifyintent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
notifyintent.setAction("android.intent.action.NOTIFY");
PendingIntent notifysender = PendingIntent.getBroadcast(this, 0, notifyintent,
PendingIntent.FLAG_UPDATE_CURRENT);
am.setInexactRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), 20 * 1000,
notifysender);
AlarmReceiver类
public class OnAlarmReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
// PullPendingRequests.acquireStaticLock(context)
try {
lock = getLock(context);
lock.acquire();
context.startService(new Intent(context, UpdateCustomerRequests.class));
} finally {
if (lock.isHeld()) {
lock.release();
}
}
}
的广播接收器
private static final String NAME = "com.commonsware.cwac.wakeful.WakefulIntentService";
private static volatile PowerManager.WakeLock lockStatic = null;
private static PowerManager.WakeLock lock;
// Needed since network will to work when device is sleeping.
synchronized private static PowerManager.WakeLock getLock(Context context) {
if (lockStatic == null) {
PowerManager mgr = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
lockStatic = mgr.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, NAME);
lockStatic.setReferenceCounted(true);
}
return (lockStatic);
}
}
后台服务。在这里,你,直到另行通知2个要点
- 唤醒锁定,以确保您的设备醒来,并在后台
- 一种的LocationManager破解工作使用的网络。我必须调试2周左右,相信我,你需要这个。 requestLocationUpdates和getLastKnownLocation只会在你想要的时候不给你位置。因此AlarmManager(运行得比java TimerTask好得多)。
所有这些都可以在Play商店的制作应用中使用。
public class UpdateCustomerRequests extends IntentService implements LocationListener {
private static Context mainContext;
public UpdateCustomerRequests() {
super("UpdateCustomerRequests");
mHandler = new Handler();
me = this;
}
public static UpdateCustomerRequests getService() {
if (me == null)
me = new UpdateCustomerRequests();
return me;
}
@Override
final protected void onHandleIntent(Intent intent) {
mainContext = getApplicationContext();
Location myLocation;
if (HomeScreen.getLocationManager() != null) {
// this is needed to trigger a background location change. Since LocationManager does not work on Samsung phones. Its a hack needed.
HomeScreen.getLocationManager().requestLocationUpdates(
LocationManager.NETWORK_PROVIDER, 0, 0, new LocationListener() {
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
@Override
public void onProviderEnabled(String provider) {
}
@Override
public void onProviderDisabled(String provider) {
}
@Override
public void onLocationChanged(final Location location) {
}
});
myLocation = HomeScreen.getLocationManager().getLastKnownLocation(
LocationManager.NETWORK_PROVIDER);
if (myLocation != null)
onLocationChanged(myLocation);
else {
God.notifications.setSpeedNotification();
}
} else
Log.e("Taxeeta:PullPendingRequets", "Not activated");
}
@Override
public void onLocationChanged(final Location location) {
// Do your background stuff
}
}
最后不要忘记你的清单thingy。
<service
android:name="com.taxeeta.UpdateCustomerRequests"
android:enabled="true"
android:label="@string/app_name"
android:screenOrientation="portrait"
android:theme="@android:style/Theme.Light.NoTitleBar" />
<receiver
android:name="com.taxeeta.support.OnAlarmReceiver"
android:exported="true" >
<intent-filter>
<action android:name="android.intent.action.NOTIFY" />
</intent-filter>
</receiver>
<receiver
android:name="com.taxeeta.HomeScreen$ResponseReceiver"
android:exported="true" >
<intent-filter>
<action android:name="com.taxeeta.intent.action.GET_SCREEN_UPDATES" />
</intent-filter>
</receiver>
+0
什么是HomeScreen课程? – ChArAnJiT
相关问题
- 1. 的Android GPS监听器始终运行
- 2. Android-gps监听器
- 3. 如何在java背景下运行这个按键监听器
- 4. Android GPS位置监听器
- 5. Android蓝牙背景监听器
- 6. 背景按键监听器
- 7. iPhone:背景监听器
- 8. Android应用中的GPS监听器
- 9. 串口监听器在背景C#
- 10. 在android中的位置监听器的背景服务
- 11. 如何在使用服务的后台运行GPS监听器 - Android
- 12. Android WebView下载监听器
- 13. Android监听GPS状态的变化?
- 14. GPS状态监听
- 15. Android onTouch监听器背景颜色变化
- 16. Android - GPS侦听器
- 17. Android监听器中断监听器
- 18. 在Android中处理下载监听器
- 19. 在Android的GIF监听器
- 20. 在android的后台服务中运行监听器
- 21. 如何禁用位置监听器,当应用程序在Android 4.4.2的背景
- 22. 在android studio中调用背景服务中的通知监听器
- 23. Gps位置监听器停止
- 24. 关于在背景下更新GPS
- 25. 在Android的另一个监听器中定义监听器
- 26. 在codeigneitor的背景下运行脚本?
- 27. 在接口的背景下运行matlab
- 28. 背景中的主页按钮监听器
- 29. Chrome扩展的背景消息监听器触发了两次
- 30. Android的位置监听器
谢谢,但这段代码是针对活动的,我该如何使用该服务。 – Dev