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>
详细解决方案
LocationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)总是为false解决办法
热度:127 发布时间:2016-04-28 06:30:05.0
相关解决方案
- jquery,二维数组取值。解决办法
- 怎么取<s:property value="news.CONTENT" escape="false"/>内容前几位
- 为什么Ext.getCmp("panelID").collapsed = false;无效呢
- 求架构方案:一个日均访问量50万的物流筛单系统的技术架构方案?解决办法
- struts2-json中诠注@JSON(deserialize=false)是什么用?不是阻止JSON反序列化成JAVA对象吗?求解
- 关于 if(rs.getString("").trim().equals(""))解决办法
- 诡异:代码执行到哪去了?解决办法
- 关于 Spring 声明式事务管理!解决办法
- 求《Java Web 程序设计与项目实践》解决办法
- 加载Hibernate的报错了,求解。解决办法
- B/S方面,java比PHP更好吗?解决办法
- 算法求解String str = "A,B,C,A,B,A"解决办法
- 这段代码还能不能优化?解决办法
- JAVA读取一个文件,非得搞那么麻烦么?解决办法
- 求帮忙。解决办法
- POI导入excel数据(数据有父子关系)解决办法
- IE不支持JQuery吗?解决办法
- 求WebService的解决方案!解决办法
- 关于ibatis的<isNotEmpty>解决办法
- hibernate获取SessionFactory失败 咋办?解决办法
- 关于<s:iterator>遍历数据。解决办法
- ASP技术升级选择.(.net或Java)解决办法
- jsp页面提交到action,中文乱码(服务器Tomcat5.0)解决办法
- 求按钮上传~解决办法
- 在项目中META INF下创建context.xml获得mysql数据库连接池(急)解决办法
- SSH插入数据,报空指针?解决办法
- JBPM 报错,需回答,在线急等!解决办法
- 吐了,java Timer 停止不了。解决办法
- struts2 带查询条件分页怎样把值带入下一页?解决办法
- jxl导出报表时,怎样合并单元格?解决办法