获取GPS启动/停止与addGpsStatusListener事件 [英] Get GPS start/stop events with addGpsStatusListener
问题描述
在一个应用程序,我想开始可以接收两个服务 通知从GPS:GPS_EVENT_STARTED和GPS_EVENT_STOPPED
In an application I would like to start a Service that can receive two notification from GPS: GPS_EVENT_STARTED and GPS_EVENT_STOPPED.
要做到这一点,我做了以下code:
To do so I've done the following code:
package com.test;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.location.GpsStatus;
import android.location.LocationManager;
import android.os.IBinder;
import android.util.Log;
public class TestNotification extends Service {
private LocationManager mLm;
private MyListener mMyListener;
private class MyListener implements GpsStatus.Listener {
@Override
public void onGpsStatusChanged(int event) {
Log.i("MyGps", "Event");
switch(event) {
case GpsStatus.GPS_EVENT_STARTED:
Log.i("MyGps", "Started");
break;
case GpsStatus.GPS_EVENT_STOPPED:
Log.i("MyGps", "Stopped");
break;
}
}
}
@Override
public IBinder onBind(Intent arg0) {
return null;
}
@Override
public void onCreate() {
mLm = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
mMyListener = new MyListener();
mLm.addGpsStatusListener( mMyListener );
}
}
我还设置了在清单以下权限:ACCESS_FINE_LOCATION 我不想用requestLocationUpdates,因为它使GPS工作 每次发送事件时,修复改变。
I also have set the following permission in manifest: ACCESS_FINE_LOCATION I don't want to use requestLocationUpdates because it makes the GPS working everytime and send events when fixes change.
据测试它在模拟器和真实设备(Eclair的),但它不工作, 该服务从未收到任何通知。
It test it on emulator and on real device (Eclair) but it doesn't work, the Service never receive any notification.
我已经仔细阅读下列线程(这里),但没有解决方案的工作。 我在一个单独的问题发送给它,因为我只是想被告知 GPS启动和停止,不能修复。
I've read in detail the following thread (here) but no solution work. I send it in a separate question because I only want to be informed of GPS start and stop, not fixes.
你对此有何建议? 可能是因为我尝试做一个服务,而在一个活动?
Do you have any advice on this? May be it is because I try to do it in a Service rather that in an Activity?
感谢您的阅读。
推荐答案
试图把这个变成AndroidManifest.xml文件:
try to put this into AndroidManifest.xml file:
<service android:enabled="true" android:name="com.test.TestNotification" />
这篇关于获取GPS启动/停止与addGpsStatusListener事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!