2

ネットワークプロバイダーを使用してユーザーの現在の場所を見つけたい小さなAndroidアプリケーションを開発しています。次の方法でこれを試しましたが、出力が得られません:

networklocationManager = (LocationManager) this
        .getSystemService(Context.LOCATION_SERVICE);
// Define a listener that responds to location updates
LocationListener networklocationListener = new LocationListener() {

    public void onLocationChanged(Location location) {
        Log.i("********************************",
                "this is my network location " + location);
        String Location_text = "NETWORK LOCATION latitude:"
                + location.getLatitude() + " longitude:"
                + location.getLatitude();
        network_location.setText(Location_text);
    }

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

    public void onProviderEnabled(String provider) {}

    public void onProviderDisabled(String provider) {}
};
// Register the listener with the Location Manager to receive location
// updates
networklocationManager
        .requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0,
                networklocationListener);

このようにマニフェストファイルに許可を与えました

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

私が欠けているものはありますか?これは正しい方法ですか?助けが必要。ありがとうございました...


public class MainActivity extends Activity implements LocationListener {

    private TextView latituteField;
    private TextView longitudeField;
    private LocationManager gpslocationManager;
    private LocationManager networklocationManager;
    private LocationManager networklocationManager1;
    private String provider;
    private TextView gps_location;
    private TextView network_location;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        gps_location = (TextView) findViewById(R.id.gps_location);
        network_location = (TextView) findViewById(R.id.network_location);
        networkLocation();
    }

    public void networkLocation() {
        networklocationManager = (LocationManager) this
                .getSystemService(Context.LOCATION_SERVICE);
        LocationListener networklocationListener = new LocationListener() {

            public void onLocationChanged(Location location) {
                Log.i("********************************",
                        "this is my network location " + location);
                String Location_text = "NETWORK LOCATION latitude:"
                        + location.getLatitude() + " longitude:"
                        + location.getLatitude();
                network_location.setText(Location_text);
            }

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

            public void onProviderEnabled(String provider) {}

            public void onProviderDisabled(String provider) {}
        };
        networklocationManager
                .requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0,
                        networklocationListener);
    }
}
4

3 に答える 3

1

ネットワークプロバイダーが有効になっていますか?

boolean network_enabled;
try {
    network_enabled = locManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
} catch(Exception ex) {
   ex.printStackTrace();
}
于 2013-07-13T19:17:52.290 に答える
1

設定で位置情報サービスを有効にしてください。それが問題になるはずです。無効になっている可能性があります (この設定は通常、[設定] の [場所とセキュリティ] にあります)

それがうまくいくかどうか教えてください!

于 2013-04-15T11:23:37.537 に答える
0

コードのこの部分がvogellaからの抽出に役立つことを願っています。

public class ShowLocationActivity extends Activity implements LocationListener {

    private TextView latituteField;
    private TextView longitudeField;
    private LocationManager locationManager;
    private String provider;

    @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);
        // Get the location manager
        locationManager = (LocationManager) 
                getSystemService(Context.LOCATION_SERVICE);
        // Define the criteria how to select the locatioin provider -> use
        // default
        Criteria criteria = new Criteria();
        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.");
            onLocationChanged(location);
        } else {
            latituteField.setText("Location not available");
            longitudeField.setText("Location not available");
        }
    }

    /* Request updates at startup */
    @Override
    protected void onResume() {
        super.onResume();
        locationManager.requestLocationUpdates(provider, 400, 1, this);
    }

    /* Remove the locationlistener updates when Activity is paused */
    @Override
    protected void onPause() {
        super.onPause();
        locationManager.removeUpdates(this);
    }

    @Override
    public void onLocationChanged(Location location) {
        int lat = (int) (location.getLatitude());
        int lng = (int) (location.getLongitude());
        latituteField.setText(String.valueOf(lat));
        longitudeField.setText(String.valueOf(lng));
    }

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

    @Override
    public void onProviderEnabled(String provider) {
        Toast.makeText(this, "Enabled new provider " + provider,
            Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onProviderDisabled(String provider) {
        Toast.makeText(this, "Disabled provider " + provider,
            Toast.LENGTH_SHORT).show();
    }
}
于 2013-04-15T11:25:21.757 に答える