【问题标题】:Finding current Location when the application is running in background is not working当应用程序在后台运行时查找当前位置不起作用
【发布时间】:2012-03-30 20:19:29
【问题描述】:

我想在应用程序在后台运行时找出当前位置

我有以下代码用于在后台运行的服务中查找当前位置。但它不起作用。

import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.IBinder;
import android.util.Log;
import android.widget.Toast;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
public class MyService extends Service {
private static LocationManager mlocManager;
private static final String TAG = "MyService";


@Override
public IBinder onBind(Intent intent) {
    return null;
}

@Override
public void onCreate() {
    Toast.makeText(this, "My Service Created", Toast.LENGTH_LONG).show();
    Log.d(TAG, "onCreate");

}

@Override
public void onDestroy() {
    Toast.makeText(this, "My Service Stopped", Toast.LENGTH_LONG).show();
    Log.d(TAG, "onDestroy");

}

@Override
public void onStart(Intent intent, int startid) {
    Toast.makeText(this, "start My Service Started", Toast.LENGTH_LONG).show();
    Log.d(TAG, "onStart");
     mlocManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);

    LocationListener mlocListener = new MyLocationListener();
    //Location newloc= new Location();


    mlocManager.requestLocationUpdates( LocationManager.GPS_PROVIDER, 0, 0, mlocListener);

    Toast.makeText(this, "hiiiits created", Toast.LENGTH_LONG).show();

}
/* Class My Location Listener */

public class MyLocationListener implements LocationListener

{

@Override

public void onLocationChanged(Location loc)

{
    Log.d(TAG, "onlocationchnage");
loc.getLatitude();

loc.getLongitude();

String Text ="My current location is: " +

"Latitud = " + loc.getLatitude() +

"Longitud = " + loc.getLongitude();

Toast.makeText( getApplicationContext(),

Text,

Toast.LENGTH_SHORT).show();

}

@Override

public void onProviderDisabled(String provider)

{

Toast.makeText( getApplicationContext(),

"Gps Disabled",

Toast.LENGTH_SHORT ).show();

}

@Override

public void onProviderEnabled(String provider)

{

Toast.makeText( getApplicationContext(),

"Gps Enabled",

Toast.LENGTH_SHORT).show();

}

@Override

public void onStatusChanged(String provider, int status, Bundle extras)

{

}

}/* End of Class MyLocationListener */
}

【问题讨论】:

    标签: android gps


    【解决方案1】:

    我认为您需要使用 GPS 的当前纬度和经度值..

    点击此链接..Click link

    我已经尝试过了,并在安卓手机上测试过。它的工作原理。

    注意: 模拟器我们没有获得当前的 GPS 位置..您必须并且应该在手机中安装您的应用程序.. 先在手机上开启GPS,试一下,就可以了。

    祝你有美好的一天。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-04-20
      • 1970-01-01
      • 1970-01-01
      • 2021-07-06
      • 1970-01-01
      • 2020-07-06
      • 2014-06-16
      • 2022-06-26
      相关资源
      最近更新 更多