如何获得通知时,禁止在Android的用户在GPS? [英] How to get notified when the gps in disabled by the user in android?
问题描述
我创建一个应用程序,它使用全球定位系统。这是第一次在 的onCreate()
我检查了GPS是否启用。而如果没有启用那么我发送用户设置GPS功能的设置菜单。
一旦GPS启用然后我开始执行我的东西。 的但是,如果在两者之间的用户停止滚动的通知管理器那么GPS怎么会知道这个?的
由于我的方法只执行一次当GPS未启用。之后,当它再次被禁用,它没有得到执行。
我只是想知道,每当在工作中的全球定位系统被用户关闭。只是需要此信息。
下面是我的code。
类LocationFinderService延伸服务{
布尔gps_enabled;
LocationManager locationManager;
@覆盖
公共无效的onCreate(){
// TODO自动生成方法存根
super.onCreate();
locationManager =(LocationManager)this.getSystemService(Context.LOCATION_SERVICE);
如果(!isGpsEnabled())
{
startGpsForLocation();
stopSelf();
}
}
@覆盖
公众的IBinder onBind(意向意图){
// TODO自动生成方法存根
返回null;
}
公共布尔isGpsEnabled()
{
gps_enabled =(locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)及&安培; locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER));
Log.i(日志,全球定位系统:+ gps_enabled);
返回gps_enabled;
}
公共无效startGpsForLocation()
{
意向意图=新的意图(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(意向);
Toast.makeText(这一点,请启动GPS,Toast.LENGTH_LONG).show();
}
}
我呼吁Activity类的按钮,点击此服务。
任何帮助将是AP preciated。
lmGps.requestLocationUpdates(LocationManager.GPS_PROVIDER,0,0,gpsLocationListener);
lmGps.addGpsStatusListener(gpsStatusListener);
gpsLocationListner是
LocationListener的gpsLocationListener =新LocationListener的(){
公共无效onLocationChanged(位置定位){
dGpsLat = location.getLatitude();
dGpsLng = location.getLongitude();
DLAT = dGpsLat;
dLng = dGpsLng;
尝试{
SaveLocation();
}
赶上(例外五){
sResponse = e.toString();
返回;
}
}
公共无效onStatusChanged(字符串商,INT地位,捆绑演员){}
公共无效onProviderEnabled(字符串提供商){}
公共无效onProviderDisabled(字符串提供商){}
};
gpsStatusListener
GpsStatus.Listener gpsStatusListener =新GpsStatus.Listener(){
@覆盖
公共无效onGpsStatusChanged(INT事件){
如果(事件== GpsStatus.GPS_EVENT_SATELLITE_STATUS){
尝试{
GpsStatus状态= lmGps.getGpsStatus(空);
SATS = status.getSatellites();
迭代器萨提= sats.iterator();
诠释计数= 0;
而(satI.hasNext()){
GpsSatellite gpssatellite =(GpsSatellite)satI.next();
如果(gpssatellite.usedInFix()){
算上++;
}
}
iSalelliteCount =计数;
}
赶上(例外前){}
}
}
};
I am creating an app which uses GPS. For the first time in onCreate()
I am checking whether the GPS is enabled or not. And if its not enabled then I am sending the user to the setting menu to set the GPS enabled.
Once the GPS is enabled then I starts executing my stuff. But if in between the user stops the GPS scrolling on Notification manager then how would I know this?
Because my method executed only once when the GPS is not enabled. After that its not getting executed when it is disabled again.
I just want to know whenever in between the work the GPS is disabled by the user. Just this information is required.
Here is my code.
class LocationFinderService extends Service{
boolean gps_enabled;
LocationManager locationManager;
@Override
public void onCreate() {
// TODO Auto-generated method stub
super.onCreate();
locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
if(!isGpsEnabled())
{
startGpsForLocation();
stopSelf();
}
}
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
public boolean isGpsEnabled()
{
gps_enabled = (locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)&& locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER));
Log.i("Log", "Gps: "+gps_enabled);
return gps_enabled;
}
public void startGpsForLocation()
{
Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK) ;
startActivity(intent);
Toast.makeText(this, "Please start the GPS", Toast.LENGTH_LONG).show();
}
}
I am calling this service on the button click of the Activity class.
Any help would be appreciated.
lmGps.requestLocationUpdates(LocationManager.GPS_PROVIDER,0,0,gpsLocationListener);
lmGps.addGpsStatusListener(gpsStatusListener);
gpsLocationListner is
LocationListener gpsLocationListener=new LocationListener(){
public void onLocationChanged(Location location){
dGpsLat=location.getLatitude();
dGpsLng=location.getLongitude();
dLat=dGpsLat;
dLng=dGpsLng;
try{
SaveLocation();
}
catch (Exception e) {
sResponse=e.toString();
return;
}
}
public void onStatusChanged(String provider,int status,Bundle extras){}
public void onProviderEnabled(String provider){}
public void onProviderDisabled(String provider){}
};
gpsStatusListener
GpsStatus.Listener gpsStatusListener=new GpsStatus.Listener(){
@Override
public void onGpsStatusChanged(int event){
if(event==GpsStatus.GPS_EVENT_SATELLITE_STATUS){
try{
GpsStatus status=lmGps.getGpsStatus(null);
sats=status.getSatellites();
Iterator satI=sats.iterator();
int count=0;
while(satI.hasNext()){
GpsSatellite gpssatellite=(GpsSatellite) satI.next();
if(gpssatellite.usedInFix()){
count++;
}
}
iSalelliteCount=count;
}
catch(Exception ex){}
}
}
};
这篇关于如何获得通知时,禁止在Android的用户在GPS?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!