获得位置更新频繁的服务 [英] Getting Location Updates frequently in Service

查看:139
本文介绍了获得位置更新频繁的服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发Android应用程序,获取位置更新频繁的服务。结果
我的code是这个

MainActivity.java

 进口android.app.Activity;
进口android.content.Intent;
进口android.location.LocationManager;
进口android.os.Bundle;
进口android.provider.Settings;
进口android.view.Menu;
进口android.view.View;
进口android.widget.CompoundButton;
进口android.widget.Switch;公共类MainActivity扩展活动实现CompoundButton.OnCheckedChangeListener {@覆盖
保护无效的onCreate(捆绑savedInstanceState){
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.activity_main);
    查看查看= this.getWindow()getDecorView()。
    的LocationManager服务=(的LocationManager)getSystemService(LOCATION_SERVICE);
    布尔启用=服务
      .isProviderEnabled(LocationManager.GPS_PROVIDER);    //检查是否启用,如果不发送用户普惠制设置
    //更好的解决办法是显示一个对话框,并建议到
    //转到设置
    如果(!启用){
      意向意图=新意图(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
      startActivity(意向);
    }
    开关S =(开关)findViewById(R.id.switch1);
    如果(S!= NULL){
        s.setOnCheckedChangeListener(本);
    }
}公共无效onResume(视图v){}
@覆盖
公共布尔onCreateOptionsMenu(菜单菜单){
    //充气菜单;如果是present这增加了项目操作栏。
    。getMenuInflater()膨胀(R.menu.main,菜单);
    返回true;
}
@覆盖
公共无效onCheckedChanged(CompoundButton buttonview,布尔器isChecked){
    // TODO自动生成方法存根
    如果(器isChecked){
        意向意图=新意图(这一点,GetGeoLocation.class);
        startService(意向);
    }    如果(!器isChecked){
        stopService(新意图(MainActivity.this,GetGeoLocation.class));
    }
}}

GetGeoLocation.java

 进口android.annotation.Sup pressLint;
进口android.app.Service;
进口android.content.Context;
进口android.content.Intent;
进口android.location.Criteria;
进口android.location.Location;
进口android.location.LocationListener;
进口android.location.LocationManager;
进口android.os.Bundle;
进口android.os.IBinder;
进口android.util.Log;
进口android.widget.Toast;公共类GetGeoLocation扩展服务实现LocationListener的{
受保护的LocationManager的LocationManager;
保护LocationListener的LocationListener的;
@覆盖
公众的IBinder onBind(意向为arg0){
    // TODO自动生成方法存根
    返回null;
}公共无效的onCreate(){
    Toast.makeText(这一点,新服务被创造,Toast.LENGTH_SHORT).show();
}
公众诠释调用onStart(){
    的LocationManager =(的LocationManager)getSystemService(Context.LOCATION_SERVICE);
    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,0,0,这一点);    标准CRI =新标准();
    字符串bestProvider = locationManager.getBestProvider(CRI,真);    位置LOC = locationManager.getLastKnownLocation(bestProvider);
    双latMy = loc.getLatitude();
    双lngMy = loc.getLongitude();
    Toast.makeText(这一点,+ latMy ++ lngMy,Toast.LENGTH_SHORT).show();
    返回START_STICKY;
}公共无效的onDestroy(){
    Toast.makeText(这一点,服务毁,Toast.LENGTH_SHORT).show();}@燮pressLint(ShowToast)
@覆盖
公共无效onLocationChanged(位置LOC){
    // TODO自动生成方法存根
    Toast.makeText(这一点,位置发生改变,Toast.LENGTH_SHORT);
    Toast.makeText(这一点,+ loc.getLatitude()++ loc.getLongitude(),Toast.LENGTH_SHORT).show();
    的System.out.println(位置+ loc.getLatitude()++ loc.getLongitude());
}@燮pressLint(ShowToast)
@覆盖
公共无效onProviderDisabled(字符串为arg0){
    // TODO自动生成方法存根
    Toast.makeText(这一点,ProviderDisabled,Toast.LENGTH_SHORT);
}@覆盖
公共无效onProviderEnabled(字符串为arg0){
    // TODO自动生成方法存根}@覆盖
公共无效onStatusChanged(字符串为arg0,ARG1 INT,捆绑ARG2){
    // TODO自动生成方法存根}}

我使用一个开关来启动服务。当我通过在交换机上检查启动该服务,该服务获取当前位置和频繁敬酒的位置。我不知道什么是错在我的code,我不知道它得到的位置或没有。结果
有人帮助我。

我logcat的犯规显示任何警告或错误

  20 01-29:29:34.996:I / SurfaceTextureClient(5512):STC :: queueBuffer(这一点:0x5b79b008)FPS:0.14,DUR:7126.33,最高:7126.33 ,最低:7126.33
01-29 20:29:34.997:I / SurfaceTextureClient(5512):STC :: queueBuffer]这样的:0x5b79b008,API:1,最后排队时间已过:7126.33
01-29 20:29:35.268:D / Skia的(5512):标志是不是10


解决方案

我不知道什么是错的code。我看到它使用旧的位置提供。我首先建议使用新戏谷歌位置服务。更容易使用和更准确。

我有一个使用新的谷歌播放位置服务,并在后台工作的全面工作示例。它是MIT许可,并准备使用。

https://github.com/nickfox/GpsTracker/tree/master/ phoneClients / Android的

I am developing an android app, Getting location updates frequently by service.
My code is this

MainActivity.java

import android.app.Activity;
import android.content.Intent;
import android.location.LocationManager;
import android.os.Bundle;
import android.provider.Settings;
import android.view.Menu;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.Switch;

public class MainActivity extends Activity implements CompoundButton.OnCheckedChangeListener{

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    View view = this.getWindow().getDecorView();
    LocationManager service = (LocationManager) getSystemService(LOCATION_SERVICE);
    boolean enabled = service
      .isProviderEnabled(LocationManager.GPS_PROVIDER);

    // check if enabled and if not send user to the GSP settings
    // Better solution would be to display a dialog and suggesting to 
    // go to the settings
    if (!enabled) {
      Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
      startActivity(intent);
    } 
    Switch s = (Switch) findViewById(R.id.switch1);
    if(s != null) {
        s.setOnCheckedChangeListener(this);
    }


}

public void onResume(View v) {

}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}
@Override
public void onCheckedChanged(CompoundButton buttonview, boolean isChecked) {
    // TODO Auto-generated method stub
    if(isChecked) {
        Intent intent = new Intent(this, GetGeoLocation.class);
        startService(intent);
    }

    if(!isChecked) {
        stopService(new Intent(MainActivity.this,GetGeoLocation.class));
    }
}

}

GetGeoLocation.java

import android.annotation.SuppressLint;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.os.IBinder;
import android.util.Log;
import android.widget.Toast;

public class GetGeoLocation extends Service implements LocationListener{


protected LocationManager locationManager;
protected LocationListener locationListener;
@Override
public IBinder onBind(Intent arg0) {
    // TODO Auto-generated method stub
    return null;
}

public void onCreate() {
    Toast.makeText(this, "The new Service was Created", Toast.LENGTH_SHORT).show();
}
public int onStart() {
    locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);

    Criteria cri = new Criteria();
    String bestProvider = locationManager.getBestProvider(cri, true);

    Location loc = locationManager.getLastKnownLocation(bestProvider);
    double latMy = loc.getLatitude();
    double lngMy = loc.getLongitude();
    Toast.makeText(this, " " +latMy+"  "+lngMy, Toast.LENGTH_SHORT).show();
    return START_STICKY;
}

public void onDestroy() {
    Toast.makeText(this, "Service Destroyed", Toast.LENGTH_SHORT).show();

}

@SuppressLint("ShowToast")
@Override
public void onLocationChanged(Location loc) {
    // TODO Auto-generated method stub
    Toast.makeText(this,"Location Changed", Toast.LENGTH_SHORT);
    Toast.makeText(this, " " +loc.getLatitude()+"  "+loc.getLongitude(), Toast.LENGTH_SHORT).show();
    System.out.println("Location"+loc.getLatitude()+"  " +loc.getLongitude());
}

@SuppressLint("ShowToast")
@Override
public void onProviderDisabled(String arg0) {
    // TODO Auto-generated method stub
    Toast.makeText(this,"ProviderDisabled", Toast.LENGTH_SHORT);
}

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

}

@Override
public void onStatusChanged(String arg0, int arg1, Bundle arg2) {
    // TODO Auto-generated method stub

}

}  

I am using a switch to start a service. When i start the service by checking on the switch, the service gets the current location and toast the location frequently. I dont know what is wrong in my code and i dont know it gets the location or not.
somebody help me out.

My logcat doesnt show any Warnings or errors

01-29 20:29:34.996: I/SurfaceTextureClient(5512): [STC::queueBuffer] (this:0x5b79b008) fps:0.14, dur:7126.33, max:7126.33, min:7126.33
01-29 20:29:34.997: I/SurfaceTextureClient(5512): [STC::queueBuffer] this:0x5b79b008, api:1, last queue time elapsed:7126.33
01-29 20:29:35.268: D/skia(5512): Flag is not 10

解决方案

I'm not sure what is wrong with your code. I see that it's using the old location provider. I would first suggest using the new google play location service. Much easier to use and more accurate.

I have a fully working sample that uses the new google play location services and works in the background. It's MIT licensed and ready to use.

https://github.com/nickfox/GpsTracker/tree/master/phoneClients/android

这篇关于获得位置更新频繁的服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆