LocationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)总是为false

nbgis1987 2012-05-07 09:14:10

package com.jt.classlibrary;

import java.util.List;

import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.SharedPreferences;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.AsyncTask;
import android.os.Binder;
import android.os.Bundle;
import android.os.IBinder;
import android.preference.PreferenceManager;
import android.provider.Settings.Secure;
import android.util.Log;

public class LocationService extends Service implements LocationListener
{
//private LocationAsyncTask asyncTask = null;
//private Timer asyncTimer = null;
private LocationManager lm = null;
//private Location loc = null;
private int iAutoUpdateFrequency = 10000;
private MyBroadcastReceiver broadcast = null;
private ServiceBinder binder = new ServiceBinder();

public class ServiceBinder extends Binder
{
public LocationService getService()
{
return LocationService.this;
}
}

@Override
public void onCreate()
{
// TODO Auto-generated method stub
try
{
SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(this);
iAutoUpdateFrequency = pref.getInt("AUTO_UPDATE_FREQUENCE", 10000);
}
catch(Exception ex)
{
Log.e("error", "Get SharedPreferences Failed---->" + ex.getMessage());
}

try
{
broadcast = new MyBroadcastReceiver();
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(ConstDef.ACTION_REQUIRE_LOCATION);
//intentFilter.addAction(ConstDef.ACTION_UPMYLOCATION);
registerReceiver(broadcast, intentFilter);
}
catch(Exception ex)
{
Log.e("error", "Register Broadcast Failed---->" + ex.getMessage());
}

try
{
lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
/*try
{
if(!lm.isProviderEnabled(LocationManager.GPS_PROVIDER))
{
if(ConstDef.bOpenGPS)
{
//Secure.setLocationProviderEnabled(getContentResolver(), LocationManager.GPS_PROVIDER, true);
}
}

if(!lm.isProviderEnabled(LocationManager.NETWORK_PROVIDER))
{
if(ConstDef.bOpenNET)
{
//Secure.setLocationProviderEnabled(getContentResolver(), LocationManager.NETWORK_PROVIDER, true);
}
}
}
catch(Exception ex)
{
Log.e("error", "Open GPS or NET Failed---->" + ex.getMessage());
} */

/*List<String> providers = lm.getProviders(false);

for(int i=0;i<providers.size();i++)
{
Log.i("info", providers.get(i));
}*/

if(lm.isProviderEnabled(LocationManager.GPS_PROVIDER))
{
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, iAutoUpdateFrequency, 1, this);
}
else if(lm.isProviderEnabled(LocationManager.NETWORK_PROVIDER))
{
lm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, iAutoUpdateFrequency, 1, this);
}

//asyncTimer = new Timer();
}
catch(Exception ex)
{
Log.e("error", "Set LocationManager Failed---->" + ex.getMessage());
}

Log.i("info", "LocationService Create Success");
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
// TODO Auto-generated method stub
return START_STICKY;
}

@Override
public void onDestroy() {
// TODO Auto-generated method stub
unregisterReceiver(broadcast);
lm.removeUpdates(this);
Log.i("info", "Service Destroy");
}

@Override
public boolean onUnbind(Intent intent) {
// TODO Auto-generated method stub
Log.i("info", "Service Unbind");
return super.onUnbind(intent);
}

private class MyBroadcastReceiver extends BroadcastReceiver
{
@Override
public void onReceive(Context context, Intent intent)
{
// TODO Auto-generated method stub
if(intent.getAction().equals(ConstDef.ACTION_REQUIRE_LOCATION))
{
new LocationAsyncTask().execute((Void[])null);
}
else if(intent.getAction().equals(ConstDef.ACTION_UPMYLOCATION))
{

}
}
}

private class LocationAsyncTask extends AsyncTask<Void, Void, Location>
{

@Override
protected Location doInBackground(Void... params) {
// TODO Auto-generated method stub
Location result = null;

for(int i=0;i<5;i++)
{
if(lm.isProviderEnabled(LocationManager.GPS_PROVIDER))
{
result = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if(result == null)
{
if(lm.isProviderEnabled(LocationManager.NETWORK_PROVIDER))
{
result = lm.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
}
}
}
else if(lm.isProviderEnabled(LocationManager.NETWORK_PROVIDER))
{
result = lm.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
}

if(result != null)
{
break;
}
}

return result;
}

@Override
protected void onPostExecute(Location result)
{
// TODO Auto-generated method stub
Intent intent = new Intent();
intent.setAction(ConstDef.ACTION_LOCATION_DONE);
if(result == null)
{
intent.putExtra("longitude", -1.0);
intent.putExtra("latitude", -1.0);
}
else
{
intent.putExtra("longitude", result.getLongitude());
intent.putExtra("latitude", result.getLatitude());
}
sendBroadcast(intent);
}
}

@Override
public void onLocationChanged(Location location) {
// TODO Auto-generated method stub
if(location != null)
{
Log.i("info", "Check Location Changed");

Intent intent = new Intent();
intent.setAction(ConstDef.ACTION_LOCATION_DONE);
intent.putExtra("longitude", location.getLongitude());
intent.putExtra("latitude", location.getLatitude());
sendBroadcast(intent);
}
}

@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub

}

@Override
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub

}

@Override
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub

}

@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
Log.i("info", "Service Bind");
return binder;
}

}


lm.isProviderEnabled(LocationManager.NETWORK_PROVIDER)总是为false,但是我网络确实是开了的,GPS就可以判断,谁知道怎么回事吗?
...全文
1597 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
OOOONO 2014-10-10
  • 打赏
  • 举报
回复
有可能是手机或者平板,设置中,位置信息使用权限中GPS或者WIFI或者网络的使用权限没有开启
机器法师 2014-03-21
  • 打赏
  • 举报
回复
楼主如何解决的啊??求分享!!
faith9111 2013-11-27
  • 打赏
  • 举报
回复
三星i9100国产的没有google服务,所以没有那个network组件,所以总是返回false,目前我也正在想解决办法!同求啊
国仔饼 2013-11-15
  • 打赏
  • 举报
回复
楼主解决了吗,我也遇到同样的问题
lbhappy1990 2012-09-21
  • 打赏
  • 举报
回复
lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
我的在这里就直接抛异常了

80,470

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧