Skip to content Skip to sidebar Skip to footer

How To Get Location Of Android Device By Its Android Id

I am new in android and recently start learning about Android and I am trying to get location of any android device by its unique android id. By that I can track the approx or exa

Solution 1:

Finally I am able to do with this code:

publicclassMyServiceextendsServiceimplementsLocationListener{
StringGPS_FILTER="";
Thread triggerService;
LocationListener locationListener;
LocationManager lm;
privatestaticfinallongMINIMUM_DISTANCE_CHANGE_FOR_UPDATES=10; // 10 meterprivatestaticfinallongMINIMUM_TIME_BETWEEN_UPDATES=1000 * 60 * 3; // 1 minuteprotected LocationManager locationManager;
booleanisRunning=true;

Calendarcur_cal= Calendar.getInstance();

Location location;
double latitude; // latitudedouble longitude;
UserFunctions userFunction;
private JSONObject json;
privateAlertDialogManageralert=newAlertDialogManager();

privatestaticStringKEY_SUCCESS="success";
privatestaticStringKEY_ERROR="error";
privatestaticStringKEY_ERROR_MSG="error_msg";
privatestaticStringKEY_FLAG="flag";
String android_id ;
String userName;

@OverridepublicvoidonCreate() {
    // TODO Auto-generated method stubsuper.onCreate();
    Intentintent=newIntent(this, MyService.class);
    PendingIntentpintent= PendingIntent.getService(getApplicationContext(),
            0, intent, 0);
    AlarmManageralarm= (AlarmManager) getSystemService(Context.ALARM_SERVICE);

    android_id = Settings.Secure.getString(getContentResolver(),
                Settings.Secure.ANDROID_ID);
    if (getAccount() != null) {
        userName = getAccount();
    }

    GPS_FILTER = "MyGPSLocation";
    // locationManager = (LocationManager)// getSystemService(Context.LOCATION_SERVICE);// locationManager.requestLocationUpdates(// LocationManager.GPS_PROVIDER,// MINIMUM_TIME_BETWEEN_UPDATES,// MINIMUM_DISTANCE_CHANGE_FOR_UPDATES,// new MyLocationListener());
    cur_cal.setTimeInMillis(System.currentTimeMillis());
    alarm.setRepeating(AlarmManager.RTC_WAKEUP, cur_cal.getTimeInMillis(),
            60 * 1000*3, pintent);
}

@OverridepublicvoidonStart(Intent intent, int startId) {
    // TODO Auto-generated method stubsuper.onStart(intent, startId);
    //turnGPSOn();/*Toast.makeText(getApplicationContext(), "Hello1", Toast.LENGTH_LONG)
            .show();*/LocationManagerlocationManager= (LocationManager) getSystemService(LOCATION_SERVICE);
     locationListener = newMyLocationListener();
     locationManager.requestLocationUpdates(
                LocationManager.NETWORK_PROVIDER,
                MINIMUM_TIME_BETWEEN_UPDATES,
                MINIMUM_DISTANCE_CHANGE_FOR_UPDATES, (LocationListener) this);
     if (locationManager != null) {
            location = locationManager
                    .getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
            if (location != null) {
                latitude = location.getLatitude();
                longitude = location.getLongitude();
                /*Toast.makeText(getApplicationContext(), latitude + ", ll" + longitude, Toast.LENGTH_LONG)
                .show();*/
                userFunction = newUserFunctions();
                newYourAsyncTaskLogin().execute();
            }
        }

     location =locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);

    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,
            MINIMUM_TIME_BETWEEN_UPDATES, 1.0f, locationListener);

}

@OverridepublicvoidonDestroy() {
    // TODO Auto-generated method stubsuper.onDestroy();
    // removeGpsListener();
}



@Overridepublic IBinder onBind(Intent intent) {
    // TODO Auto-generated method stubreturnnull;
}

// private void removeGpsListener(){// try{// lm.removeUpdates(locationManager);// }// catch(Exception ex){// System.out.println("Exception in GPSService --- "+ex);// }// }privateclassMyLocationListenerimplementsLocationListener {

    publicvoidonLocationChanged(Location location) {
        postdata(location.getLatitude(), location.getLongitude());
        Stringmessage= String.format(
                "New Location \n Longitude: %1$s \n Latitude: %2$s",
                location.getLongitude(), location.getLatitude());
        /*Toast.makeText(MyService.this, message, Toast.LENGTH_LONG).show();
        turnGPSOff();*/
    }

    privatevoidpostdata(double latitude, double longitude) {
        // TODO Auto-generated method stub/*Toast.makeText(getApplicationContext(),
                latitude + ", " + longitude, Toast.LENGTH_LONG).show();*/
    }

    publicvoidonStatusChanged(String s, int i, Bundle b) {
        // Toast.makeText(MyService.this, "Provider status changed",// Toast.LENGTH_LONG).show();
    }

    publicvoidonProviderDisabled(String s) {
        // Toast.makeText(MyService.this,// "Provider disabled by the user. GPS turned off",// Toast.LENGTH_LONG).show();
    }

    publicvoidonProviderEnabled(String s) {
        // Toast.makeText(MyService.this,// "Provider enabled by the user. GPS turned on",// Toast.LENGTH_LONG).show();
    }

}

publicvoidturnGPSOn() {
    Intentintent=newIntent("android.location.GPS_ENABLED_CHANGE");
    intent.putExtra("enabled", true);
    this.sendBroadcast(intent);

    Stringprovider= Settings.Secure.getString(getContentResolver(),
            Settings.Secure.LOCATION_PROVIDERS_ALLOWED);
    if (!provider.contains("gps")) { // if gps is disabledfinalIntentpoke=newIntent();
        poke.setClassName("com.android.settings",
                "com.android.settings.widget.SettingsAppWidgetProvider");
        poke.addCategory(Intent.CATEGORY_ALTERNATIVE);
        poke.setData(Uri.parse("3"));
        this.sendBroadcast(poke);

    }
}

// automatic turn off the gpspublicvoidturnGPSOff() {
    Stringprovider= Settings.Secure.getString(getContentResolver(),
            Settings.Secure.LOCATION_PROVIDERS_ALLOWED);
    if (provider.contains("gps")) { // if gps is enabledfinalIntentpoke=newIntent();
        poke.setClassName("com.android.settings",
                "com.android.settings.widget.SettingsAppWidgetProvider");
        poke.addCategory(Intent.CATEGORY_ALTERNATIVE);
        poke.setData(Uri.parse("3"));
        this.sendBroadcast(poke);
    }
}

@OverridepublicvoidonLocationChanged(Location location) {
    // TODO Auto-generated method stub

}

@OverridepublicvoidonStatusChanged(String provider, int status, Bundle extras) {
    // TODO Auto-generated method stub

}

@OverridepublicvoidonProviderEnabled(String provider) {
    // TODO Auto-generated method stub

}

@OverridepublicvoidonProviderDisabled(String provider) {
    // TODO Auto-generated method stub

}


classYourAsyncTaskLoginextendsAsyncTask<Void, Void, Void> {

    //private ProgressDialog _ProgressDialog;@OverrideprotectedvoidonPreExecute() {
        // show your dialog here/*_ProgressDialog = ProgressDialog.show(getApplicationContext(), "",
                "Loading", true);*/

    }

    @Overrideprotected Void doInBackground(Void... params) {
        json = userFunction.sendLocations(android_id, userName,latitude+"", longitude+"");
        returnnull;
    }

    protectedvoidonPostExecute(Void result) {
        try {
            Log.e("Key_Success:",
                    json.getString(KEY_SUCCESS));
            if (json.getString(KEY_SUCCESS) != null) {
                // loginErrorMsg.setText("");Stringres= json.getString(KEY_SUCCESS);

                if (Integer.parseInt(res) == 1) {



                } else {
                    // Error in login// loginErrorMsg.setText("Incorrect username/password");//_ProgressDialog.cancel();


                }

            } else {
                // Error in login// loginErrorMsg.setText("Incorrect username/password");//_ProgressDialog.cancel();         

            }
        } catch (JSONException e) {
            e.printStackTrace();
            Log.e("error", e.getMessage());
        }

        //_ProgressDialog.dismiss();
    }
}
public String getAccount() {
    AccountManagermanager= (AccountManager) getSystemService(ACCOUNT_SERVICE);
    Account[] list = manager.getAccountsByType("com.google");
    if (list.length != 0) {
        Stringemail= list[0].name;
        return email;
    } else {
        returnnull;
    }
}
}

Solution 2:

if you want to get location of any device who has your app in their mobile , you can get it , firstly in your app you can upload locations to your server with memberid(you can set it , unique for every device) for every x time(you can set it how many times you update the location). And then you can check your db on your server which device in where.

(my advice you can use webservice to update db on your server)

Post a Comment for "How To Get Location Of Android Device By Its Android Id"