Reputation: 3020
I want to Retrive The Location of device. i am doing it on my emulator. My emulator is Google Api's (Google inc.).I am using LocationManager class and GPS provider to get the current location but my location is always null. i gave my emulator dummy coordinates and added all the permissions in the manifest.
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.Toast;
public class Main extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
LocationManager lm = (LocationManager) getSystemService(LOCATION_SERVICE);
Location l = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if (l!= null) {
Toast.makeText(this, "Location is null", 300).show();
}else{
Toast.makeText(this,"Location is not null",300).show();
}
}
}
Manifest.xml
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.gotya"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="6"
android:targetSdkVersion="10" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_MOCK_LOCATION"/>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.RECEIVE_SMS"/>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.gotya.Main"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name=".CodeReceiver">
<intent-filter >
<action android:name="android.provider.Telephony.SMS_RECEIVED"/>
</intent-filter>
</receiver>
</application>
Upvotes: 0
Views: 239
Reputation: 26984
Android SDK doc reads:
getLastKnownlocation
returns the last known location for the provider. If the provider is currently disabled,null
is returned.
Also, the emulator can only emulate the GPS.
Bottom line: ensure that your program uses this location provider.
Upvotes: 2