I'm trying to make a simple application on android to get the current location using GPS or network provider(WIFI or tower) but I'm facing so issues while trying to make it run on the android. It won't start it gave me an exception, I don't know why.
This is the code. It's so simple, because I want to test getting the location & later I will add some features & functionality..
the main:
package com.GpsTesting;
import android.app.Activity;
import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.widget.Toast;
public class GPS_TestActivity extends Activity{
/** Called when the activity is first created. */
String location_text="";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
// Define a listener that responds to location updates
LocationListener locationListener = new LocationListener() {
public void onLocationChanged(Location location) {
// Called when a new location is found by the network location provider.
if (location != null) {
double lat = location.getLatitude();
double lng = location.getLongitude();
location_text= "latitude: "+lat+"longitude: "+lng;
Toast.makeText(getApplicationContext(), location_text, Toast.LENGTH_SHORT).show();
}
}
public void onStatusChanged(String provider, int status, Bundle extras) {}
public void onProviderEnabled(String provider) {}
public void onProviderDisabled(String provider) {}
};
lm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,0,0,locationListener);
}
}
the manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.GpsTesting"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="8" />
<application android:icon="@drawable/icon" android:label="@string/app_name" android:permission="android.permission.ACCESS_FINE_LOCATION">
<activity android:name=".GPS_TestActivity"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
</manifest>
So, Is there is a problem in the code?? I'm doing that using online tutorial (from android dev & other sites).
Thanks alot.
Notes Added:
**I have really weared thing. I try to run the application using the eclipse way(connecting my device through USB & run the application into the device instead of the application).. when I tried this way, the application run just fine without problem !!! while using the old way(sending the .apk through email & then installing it from the phone, it keeps giving me the exception !!) can any one explain for me why is that happening ?? Am I missing something ?? since, I'm knew in the android development. so, I'm afraid that I'm missing a point...
Note: I'm sending through the E-mail the .apk file that is in the bin folder(that is signed with eclipse, for testing purpose)... I tried with another simple application just to make sure that the .apk file that is available in the bin folder the right onw(for testing) or not, & it does works.. so, the apk file is just fine... but why for the network application its not working while in the eclipse it does work ?? Thanks in advance ;)**
Solved ;)
The solution for the problem i added a permission before in the manifest in the application tag
android:permission="android.permission.ACCESS_FINE_LOCATION"
and it was duplicate since its already been added below(or because its in the wrong place). So, i deleted it & it is running fine.
Thank you very much guys