获得"未找到源"试图检测我的应用程序的Andr​​oid设备的位置误差 [英] Getting "Source not found" error while trying to detect the location of an android device in my application

查看:130
本文介绍了获得"未找到源"试图检测我的应用程序的Andr​​oid设备的位置误差的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我按照以下链接来检测我的程序的设备的位置

<一个href=\"http://stackoverflow.com/questions/2227292/how-to-get-latitude-and-longitude-of-the-mobiledevice-in-android\">How获得使用mobileDevice的经度和纬度的Andr​​oid?。

但我的应用程序不断给源未找到运行时异常。谁能解释这背后的原因。我也张贴我的code如下: -

公共无效的getLocation()
    {
        的LocationManager locationmgr =(的LocationManager)this.getSystemService(Context.LOCATION_SERVICE);

 位置位置= locationmgr.getLastKnownLocation(LocationManager.GPS_PROVIDER);LocationListener的LocationListener的=新LocationListener的(){    公共无效onLocationChanged(地点){
        location_longitude = location.getLongitude();
        location_latitude = location.getLatitude();
    }    公共无效onStatusChanged(字符串提供商,INT地位,捆绑演员){}    公共无效onProviderEnabled(字符串提供商){}    公共无效onProviderDisabled(字符串提供商){}};locationmgr.requestLocationUpdates(LocationManager.GPS_PROVIDER,2000,10,LocationListener的);


解决方案

我使用这个类,并成功地找到用户的当前位置。希望它会为你的作品...

 公共类LocationTracker扩展服务实现LocationListener的{私人最终上下文mContext;//标志GPS状态
布尔isGPSEnabled = FALSE;//标志网络状态
布尔isNetworkEnabled = FALSE;//标志GPS状态
布尔canGetLocation = FALSE;地点位置; // 位置
双纬度; //纬度
双经度; //经度
私有静态最后长MIN_DISTANCE_CHANGE_FOR_UPDATES = 10; //10米//毫秒更新之间的最短时间
私有静态最后长MIN_TIME_BW_UPDATES = 1000 * 60 * 1; // 1分钟//声明一个位置管理
受保护的LocationManager的LocationManager;公共LocationTracker(上下文的背景下){
    this.mContext =背景;
    的getLocation();
}公共场所的getLocation(){    尝试{
        的LocationManager =(的LocationManager)mContext
                .getSystemService(LOCATION_SERVICE);        //获取GPS状态
        isGPSEnabled =的LocationManager
                .isProviderEnabled(LocationManager.GPS_PROVIDER);        //获取网络状态
        isNetworkEnabled =的LocationManager
                .isProviderEnabled(LocationManager.NETWORK_PROVIDER);        如果(isGPSEnabled&安培;!&安培;!isNetworkEnabled){
            //没有启用网络供应商
        }其他{
            this.canGetLocation = TRUE;
            //首先从网络提供商处获取位置
            如果(isNetworkEnabled){
                locationManager.requestLocationUpdates(
                        LocationManager.NETWORK_PROVIDER,
                        MIN_TIME_BW_UPDATES,
                        MIN_DISTANCE_CHANGE_FOR_UPDATES,这一点);
                Log.d(网络,网络);
                如果(的LocationManager!= NULL){
                    位置=的LocationManager
                            .getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
                    如果(位置!= NULL){
                        纬度= location.getLatitude();
                        经度= location.getLongitude();
                    }
                }
            }
            如果(isGPSEnabled){
                如果(位置== NULL){
                    locationManager.requestLocationUpdates(
                            LocationManager.GPS_PROVIDER,
                            MIN_TIME_BW_UPDATES,
                            MIN_DISTANCE_CHANGE_FOR_UPDATES,这一点);
                    Log.d(GPS已启用,GPS功能的);
                    如果(的LocationManager!= NULL){
                        位置=的LocationManager
                                .getLastKnownLocation(LocationManager.GPS_PROVIDER);
                        如果(位置!= NULL){
                            纬度= location.getLatitude();
                            经度= location.getLongitude();
                        }
                    }
                }
            }
        }    }赶上(例外五){
        e.printStackTrace();
    }    返回的位置;}/ **
 *停止使用GPS监听器
 *调用此函数将停止在你的应用程序中使用GPS
 * * /
公共无效stopUsingGPS(){
    如果(的LocationManager!= NULL){
        locationManager.removeUpdates(LocationTracker.this);
    }
}/ **
 *函数来获取纬度
 * * /
公共双getLatitude(){
    如果(位置!= NULL){
        纬度= location.getLatitude();
        Log.d(纬度,北纬+);
    }    //返回纬度
    返回纬度;
}/ **
 *函数来获取经度
 * * /
公共双getLongitude(){
    如果(位置!= NULL){
        经度= location.getLongitude();
        Log.d(长,经度+);
    }    //返回经度
    返回经度;
}
/ **
 *功能检查启用GPS / WIFI
 * @返回布尔
 * * /
公共布尔canGetLocation(){
    返回this.canGetLocation;
}/ **
 *功能,以显示设置警告对话框
 *在pressing设置按钮将劳克设置选项
 * * /
公共无效showSettingsAlert(){
    AlertDialog.Builder alertDialog =新AlertDialog.Builder(mContext);    //设置对话框标题
    alertDialog.setTitle(GPS设置);    //设置对话框留言
    alertDialog.setMessage(GPS未启用你想要去设置菜单?);    //在pressing设置按钮
    alertDialog.setPositiveButton(设置,新DialogInterface.OnClickListener(){
        公共无效的onClick(DialogInterface对话,诠释它){
            意向意图=新意图(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
            mContext.startActivity(意向);
        }
    });    //在pressing取消按钮
    alertDialog.setNegativeButton(取消,新DialogInterface.OnClickListener(){
        公共无效的onClick(DialogInterface对话,诠释它){
        dialog.cancel();
        }
    });    //显示警报信息
    alertDialog.show();
}@覆盖
公共无效onLocationChanged(地点){
}@覆盖
公共无效onProviderDisabled(字符串提供商){
}@覆盖
公共无效onProviderEnabled(字符串提供商){
}@覆盖
公共无效onStatusChanged(字符串提供商,INT地位,捆绑演员){
}@覆盖
公众的IBinder onBind(意向为arg0){
    返回null;
}

}

和您的活动创建它的对象,类似如下获取位置....

  LocationTracker locationTracker =新LocationTracker(StreamActivity.this);
如果(!locationTracker.canGetLocation()){
            locationTracker.showSettingsAlert();
        }其他{            纬度= locationTracker.getLatitude;
            经度= locationTracker.getLongitude;
            locationTracker.stopUsingGPS();        }

和清单文件中添加的所有权限......

 &LT;使用许可权的android:NAME =android.permission.ACCESS_MOCK_LOCATION/&GT;
&LT;使用许可权的android:NAME =android.permission.INTERNET对/&GT;
&LT;使用许可权的android:NAME =android.permission.READ_PHONE_STATE/&GT;
&LT;使用许可权的android:NAME =android.permission.ACCESS_NETWORK_STATE/&GT;
&LT;使用许可权的android:NAME =android.permission.ACCESS_COARSE_LOCATION/&GT;
&LT;使用许可权的android:NAME =android.permission.ACCESS_FINE_LOCATION/&GT;

好运......

I have followed the following link to detect the location of a device in my program

How to get Latitude and Longitude of the mobiledevice in android?.

But my application keeps giving the "Source not found" run time exception. Can anyone explain the reason behind it. I am also posting my code below:-

public void getLocation() { LocationManager locationmgr = (LocationManager)this.getSystemService(Context.LOCATION_SERVICE);

Location location = locationmgr.getLastKnownLocation(LocationManager.GPS_PROVIDER);

LocationListener locationListener = new LocationListener() {

    public void onLocationChanged(Location location) {
        location_longitude = location.getLongitude();
        location_latitude = location.getLatitude();
    }

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

    public void onProviderEnabled(String provider) {}

    public void onProviderDisabled(String provider) {}

};

locationmgr.requestLocationUpdates(LocationManager.GPS_PROVIDER, 2000, 10, locationListener);

解决方案

I used this class and successfully finds user's current location . Hope it will works for you ...

public class LocationTracker extends Service implements LocationListener {

private final Context mContext;

// flag for GPS status
boolean isGPSEnabled = false;

// flag for network status
boolean isNetworkEnabled = false;

// flag for GPS status
boolean canGetLocation = false;

Location location; // location
double latitude; // latitude
double longitude; // longitude


private static final long MIN_DISTANCE_CHANGE_FOR_UPDATES = 10; // 10 meters

// The minimum time between updates in milliseconds
private static final long MIN_TIME_BW_UPDATES = 1000 * 60 * 1; // 1 minute

// Declaring a Location Manager
protected LocationManager locationManager;

public LocationTracker(Context context){
    this.mContext = context;
    getLocation();
}

public Location getLocation(){

    try {
        locationManager = (LocationManager) mContext
                .getSystemService(LOCATION_SERVICE);

        // getting GPS status
        isGPSEnabled = locationManager
                .isProviderEnabled(LocationManager.GPS_PROVIDER);

        // getting network status
        isNetworkEnabled = locationManager
                .isProviderEnabled(LocationManager.NETWORK_PROVIDER);

        if (!isGPSEnabled && !isNetworkEnabled) {
            // no network provider is enabled
        } else {
            this.canGetLocation = true;
            // First get location from Network Provider
            if (isNetworkEnabled) {
                locationManager.requestLocationUpdates(
                        LocationManager.NETWORK_PROVIDER,
                        MIN_TIME_BW_UPDATES,
                        MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
                Log.d("Network", "Network");
                if (locationManager != null) {
                    location = locationManager
                            .getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
                    if (location != null) {
                        latitude = location.getLatitude();
                        longitude = location.getLongitude();
                    }
                }
            }
            if (isGPSEnabled) {
                if (location == null) {
                    locationManager.requestLocationUpdates(
                            LocationManager.GPS_PROVIDER,
                            MIN_TIME_BW_UPDATES,
                            MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
                    Log.d("GPS Enabled", "GPS Enabled");
                    if (locationManager != null) {
                        location = locationManager
                                .getLastKnownLocation(LocationManager.GPS_PROVIDER);
                        if (location != null) {
                            latitude = location.getLatitude();
                            longitude = location.getLongitude();
                        }
                    }
                }
            }
        }

    } catch (Exception e) {
        e.printStackTrace();
    }

    return location;

}

/**
 * Stop using GPS listener
 * Calling this function will stop using GPS in your app
 * */
public void stopUsingGPS(){
    if(locationManager != null){
        locationManager.removeUpdates(LocationTracker.this);
    }       
}

/**
 * Function to get latitude
 * */
public double getLatitude(){
    if(location != null){
        latitude = location.getLatitude();
        Log.d("lat", latitude+"");
    }

    // return latitude
    return latitude;
}

/**
 * Function to get longitude
 * */
public double getLongitude(){
    if(location != null){
        longitude = location.getLongitude();
        Log.d("long :", longitude+"");
    }

    // return longitude
    return longitude;
}


/**
 * Function to check GPS/wifi enabled
 * @return boolean
 * */
public boolean canGetLocation() {
    return this.canGetLocation;
}

/**
 * Function to show settings alert dialog
 * On pressing Settings button will lauch Settings Options
 * */
public void showSettingsAlert(){
    AlertDialog.Builder alertDialog = new AlertDialog.Builder(mContext);

    // Setting Dialog Title
    alertDialog.setTitle("GPS settings");

    // Setting Dialog Message
    alertDialog.setMessage("GPS is not enabled. Do you want to go to settings menu?");

    // On pressing Settings button
    alertDialog.setPositiveButton("Settings", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog,int which) {
            Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
            mContext.startActivity(intent);
        }
    });

    // on pressing cancel button
    alertDialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) {
        dialog.cancel();
        }
    });

    // Showing Alert Message
    alertDialog.show();
}

@Override
public void onLocationChanged(Location location) {
}

@Override
public void onProviderDisabled(String provider) {
}

@Override
public void onProviderEnabled(String provider) {
}

@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}

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

}

and in your activity create the object of it and fetch location like as follows ....

LocationTracker locationTracker = new LocationTracker(StreamActivity.this);
if (!locationTracker.canGetLocation()) {
            locationTracker.showSettingsAlert();
        } else {

            latitude = locationTracker.getLatitude;
            longitude = locationTracker.getLongitude;


            locationTracker.stopUsingGPS();

        }

and add all the permissions in manifest file ....

<uses-permission android:name="android.permission.ACCESS_MOCK_LOCATION" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

best of luck ....

这篇关于获得&QUOT;未找到源&QUOT;试图检测我的应用程序的Andr​​oid设备的位置误差的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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