我正在開發一個android應用程序。一旦他/她登錄到應用程序,我就需要找到用戶的位置。我不希望顯示地圖,應該在沒有用戶知識的情況下識別地點。使用Google地圖API可以做到這一點嗎?或者有沒有其他辦法可以做到這一點?查找用戶位置
感謝
我正在開發一個android應用程序。一旦他/她登錄到應用程序,我就需要找到用戶的位置。我不希望顯示地圖,應該在沒有用戶知識的情況下識別地點。使用Google地圖API可以做到這一點嗎?或者有沒有其他辦法可以做到這一點?查找用戶位置
感謝
試試這個,
protected LocationManager locationManager;
Context context;
public String gps_loc;
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 10, new MyLocationListener());
private class MyLocationListener implements LocationListener {
public void onLocationChanged(Location location) {
gps_loc = String.format("%1$s" +"-"+"%2$s",location.getLongitude(), location.getLatitude());
Toast.makeText(Clockin.this, gps_loc, Toast.LENGTH_SHORT).show();
}
public void onStatusChanged(String s, int i, Bundle b) {
Toast.makeText(class.this, "Provider status changed", Toast.LENGTH_SHORT).show();
}
public void onProviderDisabled(String s) {
Toast.makeText(class.this,"Provider disabled by the user. GPS turned off",Toast.LENGTH_SHORT).show();
final AlertDialog alertDialog = new AlertDialog.Builder(class.this).create();
alertDialog.setTitle("Activate GPS...");
alertDialog.setButton("ok", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
startActivityForResult(new Intent(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS), 0);
}
});
alertDialog.show();
}
public void onProviderEnabled(String s) {
Toast.makeText(class.this,"Provider enabled by the user. GPS turned on", Toast.LENGTH_SHORT).show();
}
}
要做到這一點,最好的辦法是使用PASSIVE
位置提供像這樣:
LocationManager lm = (LocationManager)yourActivityContext.getSystemService(Context.LOCATION_SERVICE);
Location lastKnown = lm.getLastKnownLocation(LocationManager.PASSIVE_PROVIDER);
這將返回由操作系統接收到的最後已知位置,所以這可能是過時的,但您可以通過查詢位置對象來檢查何時檢索位置以及由哪個提供商提供。
總之,用戶將不知道,你已經得到了一個位置除非您的應用程序將需要適當的位置的權限(S)。
取出麪包消息得到O/P沒有用戶的知識。 – wolverine 2012-04-19 06:57:08