在Android上通过WI-FI获取位置的麻烦

时间:2012-12-10 14:46:00

标签: android geolocation android-4.0-ice-cream-sandwich

我遇到了麻烦。我无法让我的HTC位置难以置信。

这是我的清单

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.locationfinder"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="15" />

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

<application
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name=".MainActivity"
        android:label="@string/title_activity_main" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

这是我的活动

public class MainActivity extends Activity {
 /** Called when the activity is first created. */
   @Override
   public void onCreate(Bundle savedInstanceState)
   {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.activity_main);

   /* Use the LocationManager class to obtain GPS locations */
   LocationManager mlocManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
   LocationListener mlocListener = new MyLocationListener();
   mlocManager.requestLocationUpdates( LocationManager.NETWORK_PROVIDER, 0, 0, mlocListener);
   }

   /* Class My Location Listener */
   public class MyLocationListener implements LocationListener
   {
       @Override
       public void onLocationChanged(Location loc)
       {
           loc.getLatitude();
           loc.getLongitude();

           String Text = "My current location is: " +
                         "Latitude = " + loc.getLatitude() +
                         "Longitude = " + loc.getLongitude();

           Toast.makeText( getApplicationContext(), Text, Toast.LENGTH_SHORT).show();
       }

       @Override
       public void onProviderDisabled(String provider)
       {
           Toast.makeText( getApplicationContext(), "WIFI Disabled", Toast.LENGTH_SHORT ).show();
       }

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

       @Override
       public void onStatusChanged(String provider, int status, Bundle extras)
       {
           Toast.makeText( getApplicationContext(), "snth", Toast.LENGTH_SHORT).show();
       }

   }/* End of Class MyLocationListener */

}

如果我选择LocationManager.GPS_PROVIDER代替LocationManager.NETWORK_PROVIDER并且GPS已关闭,则目前正在使用“禁用GPS”进行祝酒。但是,如果我使用LocationManager.NETWORK_PROVIDER,无论WiFi状态如何,都不会发生任何事情。

0 个答案:

没有答案