问题描述
海想获得使用广播Receiver.its做工精细的GPS定位,但我想,每半小时once.i使用** MINIMUM_TIME_BETWEEN_UPDATES时,纬度和经度值改为** 30mts。我拿到的位置
更新
公共类MainActivity扩展活动实现LocationListener的{
最后DBAdapter1 DB =新DBAdapter1(本);
私人ConnectivityReceiver接收器= NULL;
私人TextView的txtNetworkInfo;
私有静态的TextView latituteField;
私有静态的TextView longitudeField;
私人的LocationManager的LocationManager;
SoapSerializationEnvelope信封=新SoapSerializationEnvelope(SoapEnvelope.VER11);
私有静态最后弦乐HostUrl =HTTP://xxx/Service.svc
//私有静态最后弦乐HostUrl =HTTP://yyyService.svc
私有静态最后弦乐NAMESPACE =http://tempuri.org/;
私人HttpTransportSE httpTransport =新HttpTransportSE(HostUrl);
私人字符串供应商;
私人SoapObject请求= NULL;
私有静态最后长MINIMUM_DISTANCE_CHANGE_FOR_UPDATES = 1; //在米
私有静态最后长MINIMUM_TIME_BETWEEN_UPDATES = 1800000; //(毫秒)@覆盖
公共无效的onCreate(捆绑savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.main);
latituteField =(的TextView)findViewById(R.id.TextView02);
longitudeField =(的TextView)findViewById(R.id.TextView04);
的LocationManager =(的LocationManager)getSystemService(Context.LOCATION_SERVICE);
//定义标准如何选择locatioin提供商 - >使用
//默认
标准标准=新标准();
Log.i(ConnTest,locationManager.toString());
供应商= locationManager.getBestProvider(标准,FALSE);
地点= locationManager.getLastKnownLocation(供应商);//初始化位置字段
如果(位置!= NULL){
的System.out.println(提供者+提供商+已被选择。);
浮动纬度=(浮点)(location.getLatitude());
浮动液化天然气=(浮点)(location.getLongitude());
latituteField.setText(将String.valueOf(LAT));
longitudeField.setText(将String.valueOf(LNG));}其他{
latituteField.setText(提供者不可用);
longitudeField.setText(提供者不可用);
} txtNetworkInfo =(的TextView)findViewById(R.id.txtNetworkInfo); 接收器=新ConnectivityReceiver(); registerReceiver(接收器,新的IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION));
}
@覆盖
保护无效onResume(){
super.onResume();
//locationManager.requestLocationUpdates(provider,1000,1,本);
locationManager.requestLocationUpdates(
供应商,
MINIMUM_TIME_BETWEEN_UPDATES,
MINIMUM_DISTANCE_CHANGE_FOR_UPDATES,
这个
);
}/ *删除LocationListener的更新时,活动暂停* /
@覆盖
保护无效的onPause(){
super.onPause();
locationManager.removeUpdates(本);
}@覆盖
公共无效onLocationChanged(地点){
// TODO自动生成方法存根
浮动纬度=(浮点)(location.getLatitude());
浮动液化天然气=(浮点)(location.getLongitude());
longitudeField.setText(将String.valueOf(LNG));
latituteField.setText(将String.valueOf(LAT));}@覆盖
公共无效onProviderDisabled(字符串提供商){
// TODO自动生成方法存根}@覆盖
公共无效onProviderEnabled(字符串提供商){
// TODO自动生成方法存根
Toast.makeText(这一点,已启用新的供应商+供应商,
Toast.LENGTH_SHORT).show();}@覆盖
公共无效onStatusChanged(字符串提供商,INT地位,捆绑演员){
// TODO自动生成方法存根
Toast.makeText(这一点,没有开启提供商+供应商,
Toast.LENGTH_SHORT).show();}
保护无效显示(光标C){
Toast.makeText(这一点,ROWID:+ c.getString(0)+\\ n+
纬度:+ c.getString(1)+\\ n+经度:+ c.getString(2)+\\ n+
Toast.LENGTH_LONG,0).show();
}@覆盖
保护无效的onDestroy(){
unregisterReceiver(接收机); super.onDestroy();
}私人字符串getNetworkStateString(NetworkInfo.State状态){
字符串stateString =未知; 开关(州)
{
案有关:stateString =已连接;打破;
案件正在连接:stateString =正在连接;打破;
案例DISCONNECTED:stateString =断开连接;打破;
案例DISCONNECTING:stateString =断开;打破;
案例暂停:stateString =暂停;打破;
默认:stateString =未知;打破;
} 返回stateString;
} 私有类ConnectivityReceiver扩展广播接收器{ 私人定时器mTimer;
私人的TimerTask mTimerTask; @覆盖
公共无效的onReceive(上下文的背景下,意图意图){
的NetworkInfo信息= intent.getParcelableExtra(ConnectivityManager.EXTRA_NETWORK_INFO); 如果(NULL!=资讯)
{
字符串状态= getNetworkStateString(info.getState());
如果(state.equals(已连接)){
尝试{
mTimer =新的Timer();
mTimer.scheduleAtFixedRate(mTimerTask,1000,1800000);
mTimerTask =新的TimerTask(){ @覆盖
公共无效的run(){
SoapPrimitive响应=无效;
最后弦乐方法名=InsertAllGPSInformation;
请求=新SoapObject空间(namespace,方法名);
envelope.dotNet = TRUE;
request.addProperty(经度,longitudeField.getText()的toString());
request.addProperty(纵横,latituteField.getText()的toString());
request.addProperty(日期,NEWTIME); envelope.setOutputSoapObject(请求);
字符串结果= NULL;
尝试
{
httpTransport.call空间(namespace +IService /+方法名,信封);
响应=(SoapPrimitive)envelope.getResponse();
结果= response.toString();
}
赶上(例外五)
{
Toast.makeText(getBaseContext(),你的净连接或未登录网+,Toast.LENGTH_LONG).show();
Log.e(上传图片错误:e.getMessage());
}
}
};
}
赶上(例外五)
{
Toast.makeText(getBaseContext(),e.getMessage()+,Toast.LENGTH_LONG).show();
Log.e(上传图片错误:e.getMessage());
}
我想问题是为什么会比每30分钟一次的更新?
那么答案将是:对于通知的最小时间间隔,以毫秒为单位该字段仅作为提示,以节省电源,位置更新之间的实际时间可能比这个值更大或更小。
请参见
另外,您可以使用一个处理程序或触发警报每30',并要求一个位置更新报警触发时。与API 9开始有一个requestSingleUpdate()。
Hai am trying to get the gps location using BroadCast Receiver.its working fine,But i want to get the location every half hour once.i used **MINIMUM_TIME_BETWEEN_UPDATES ** 30mts .i got when latitude and longitude value changed
update
public class MainActivity extends Activity implements LocationListener {
final DBAdapter1 db=new DBAdapter1(this);
private ConnectivityReceiver receiver = null;
private TextView txtNetworkInfo ;
private static TextView latituteField;
private static TextView longitudeField;
private LocationManager locationManager;
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
private static final String HostUrl =" http://xxx/Service.svc";
// private static final String HostUrl =" http://yyyService.svc";
private static final String NAMESPACE = "http://tempuri.org/";
private HttpTransportSE httpTransport = new HttpTransportSE(HostUrl);
private String provider;
private SoapObject request=null;
private static final long MINIMUM_DISTANCE_CHANGE_FOR_UPDATES = 1; // in Meters
private static final long MINIMUM_TIME_BETWEEN_UPDATES = 1800000; // in Milliseconds
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
latituteField = (TextView) findViewById(R.id.TextView02);
longitudeField = (TextView) findViewById(R.id.TextView04);
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
// Define the criteria how to select the locatioin provider -> use
// default
Criteria criteria = new Criteria();
Log.i("ConnTest",locationManager.toString());
provider = locationManager.getBestProvider(criteria, false);
Location location = locationManager.getLastKnownLocation(provider);
// Initialize the location fields
if (location != null) {
System.out.println("Provider " + provider + " has been selected.");
float lat = (float) (location.getLatitude());
float lng = (float) (location.getLongitude());
latituteField.setText(String.valueOf(lat));
longitudeField.setText(String.valueOf(lng));
} else {
latituteField.setText("provider not available");
longitudeField.setText("provider not available");
}
txtNetworkInfo = (TextView)findViewById(R.id.txtNetworkInfo);
receiver = new ConnectivityReceiver();
registerReceiver(receiver,new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION));
}
@Override
protected void onResume() {
super.onResume();
//locationManager.requestLocationUpdates(provider,1000, 1, this);
locationManager.requestLocationUpdates(
provider,
MINIMUM_TIME_BETWEEN_UPDATES,
MINIMUM_DISTANCE_CHANGE_FOR_UPDATES,
this
);
}
/* Remove the locationlistener updates when Activity is paused */
@Override
protected void onPause() {
super.onPause();
locationManager.removeUpdates(this);
}
@Override
public void onLocationChanged(Location location) {
// TODO Auto-generated method stub
float lat = (float) (location.getLatitude());
float lng = (float) (location.getLongitude());
longitudeField.setText(String.valueOf(lng));
latituteField.setText(String.valueOf(lat));
}
@Override
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
}
@Override
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
Toast.makeText(this, "Enabled new provider " + provider,
Toast.LENGTH_SHORT).show();
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
Toast.makeText(this, "Disenabled provider " + provider,
Toast.LENGTH_SHORT).show();
}
protected void Display(Cursor c) {
Toast.makeText(this, "rowid: " + c.getString(0) + "\n" +
"Latitude: " + c.getString(1) + "\n" + "Longitude: " + c.getString(2) + "\n" +
Toast.LENGTH_LONG, 0).show();
}
@Override
protected void onDestroy() {
unregisterReceiver(receiver);
super.onDestroy();
}
private String getNetworkStateString(NetworkInfo.State state){
String stateString = "Unknown";
switch(state)
{
case CONNECTED: stateString = "Connected"; break;
case CONNECTING: stateString = "Connecting"; break;
case DISCONNECTED: stateString = "Disconnected"; break;
case DISCONNECTING: stateString = "Disconnecting"; break;
case SUSPENDED: stateString = "Suspended"; break;
default: stateString = "Unknown"; break;
}
return stateString;
}
private class ConnectivityReceiver extends BroadcastReceiver{
private Timer mTimer;
private TimerTask mTimerTask;
@Override
public void onReceive(Context context, Intent intent) {
NetworkInfo info = intent.getParcelableExtra(ConnectivityManager.EXTRA_NETWORK_INFO);
if(null != info)
{
String state = getNetworkStateString(info.getState());
if(state.equals("Connected")){
try{
mTimer = new Timer();
mTimer.scheduleAtFixedRate(mTimerTask,1000,1800000);
mTimerTask = new TimerTask() {
@Override
public void run() {
SoapPrimitive response=null;
final String methodname="InsertAllGPSInformation";
request = new SoapObject(NAMESPACE,methodname);
envelope.dotNet = true;
request.addProperty("Longitude",longitudeField.getText().toString());
request.addProperty("Latitude",latituteField.getText().toString());
request.addProperty("Date",newtime);
envelope.setOutputSoapObject(request);
String result = null;
try
{
httpTransport.call(NAMESPACE+"IService/"+methodname, envelope);
response = ( SoapPrimitive )envelope.getResponse();
result=response.toString();
}
catch(Exception e)
{
Toast.makeText(getBaseContext(), "Your Net Connected or Not Login to Net"+"", Toast.LENGTH_LONG).show();
Log.e("Upload Picture Error:",e.getMessage());
}
}
};
}
catch(Exception e)
{
Toast.makeText(getBaseContext(),e.getMessage()+"", Toast.LENGTH_LONG).show();
Log.e("Upload Picture Error:",e.getMessage());
}
I guess the question is "why do I get more updates than once every 30 minutes?":
Then the answer would be: "the minimum time interval for notifications, in milliseconds. This field is only used as a hint to conserve power, and actual time between location updates may be greater or lesser than this value.".See http://developer.android.com/reference/android/location/LocationManager.html
Alternatively you could use a Handler or trigger an alarm every 30' and request a single location update when the alarm fires. Starting with API 9 there is a requestSingleUpdate().
这篇关于如何检查使用广播接收机的GPS跟踪半小时一次?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!