2012-05-17 2 views
0

Я пытаюсь показать позицию пользователя с помощью GPS. Я написал следующий код, чтобы, если GPS отключен на устройстве, пользователь должен включить его, перейдя к настройкам. Если он включен, тогда он должен показывать текущую позицию пользователя. Я запускаю приложение на устройстве Android. У меня нет ошибок при запуске приложения, но позиция не будет отображаться. Пожалуйста, помогите ------------ Основной активности --------------------Долгота и широта не отображаются

package com.android.disasterAlertApp; 

import android.app.Activity; 
import android.app.AlertDialog; 
import android.content.DialogInterface; 
import android.content.Intent; 
import android.location.Location; 
import android.location.LocationListener; 
import android.location.LocationManager; 
import android.os.Bundle; 

import android.util.Log; 
import android.widget.Toast; 

public class MainActivity extends Activity { 
/** Called when the activity is first created. */ 

protected void onCreate(Bundle savedInstanceState) { 
super.onCreate(savedInstanceState); 
setContentView(R.layout.main); 
LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE); 
LocationListener ll = new mylocationlistener(); 
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, ll); 

if (locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)){ 
Toast.makeText(this, "GPS is Enabled in your devide", Toast.LENGTH_SHORT).show(); 
    } 
    else{ 
    showGPSDisabledAlertToUser(); 
    } 
    } 

    private void showGPSDisabledAlertToUser(){ 
    AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this); 
    alertDialogBuilder.setMessage("GPS is disabled.Do you want to enable it?") 
.setCancelable(false) 
.setPositiveButton("Goto Settings Page To Enable GPS", 
new DialogInterface.OnClickListener(){ 
public void onClick(DialogInterface dialog, int id){ 
Intent callGPSSettingIntent = new Intent(
android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS); 
startActivity(callGPSSettingIntent); 
    } 
    }); 
alertDialogBuilder.setNegativeButton("Cancel", 
new DialogInterface.OnClickListener(){ 
public void onClick(DialogInterface dialog, int id){ 
dialog.cancel(); 
    } 
}); 
AlertDialog alert = alertDialogBuilder.create(); 
alert.show(); 
} 

    private class mylocationlistener implements LocationListener { 
    public void onLocationChanged(Location location) { 
     if (location != null) { 
    Log.d("LOCATION CHANGED", location.getLatitude() + ""); 
Log.d("LOCATION CHANGED", location.getLongitude() + ""); 
Toast.makeText(MainActivity.this, 
    location.getLatitude() + "" + location.getLongitude(), 
    Toast.LENGTH_LONG).show(); 
    } 
    } 

    public void onProviderDisabled(String provider) { 
    } 
    public void onProviderEnabled(String provider) { 
    } 
    public void onStatusChanged(String provider, int status, Bundle extras) { 
    } 
    } 
} 

------------Manifest----------------- 

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

<application 
    android:icon="@drawable/ic_launcher" 
android:label="@string/app_name" > 
<activity 
    android:name=".Splash" 
    android:label="@string/app_name" > 
    <intent-filter> 
     <action android:name="android.intent.action.MAIN" /> 

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

     <activity 
     android:name=".MainActivity" 
     android:label="@string/app_name" > 
     <intent-filter> 
      <action android:name="com.android.disasterAlertApp.MAINACTIVITY" /> 
      <category android:name="android.intent.category.DEFAULT" /> 
     </intent-filter> 
    </activity> 

</application> 
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"></uses-permission> 
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"></uses-permission> 
    <uses-permission android:name="android.permission.ACCESS_LOCATION_EXTRA_COMMANDS"></uses-permission> 
    <uses-permission android:name="android.permission.ACCESS_MOCK_LOCATION"></uses-permission> 
    <uses-permission android:name="android.permission.CONTROL_LOCATION_UPDATES"></uses-permission> 
</manifest> 
+1

интернет-разрешение – Akram

+0

добавлено разрешение на Интернет, по-прежнему не работает –

ответ

0

Для получения позиции GPS он принимает время и если ваш внутри здания его трудно получить значения GPS, если устройство находится в наружной части Bulding мы можем получить значения GPS ..

0

Добавить это разрешение в файле манифеста

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> 
0

Убедитесь, что ваше аппаратное устройство GPS работает. У меня было 2 новых телефона LG Lucid, и GPS не работал ни на одном из них. В настройках местоположения проверьте только поле GPS и затем запустите Google Map, чтобы узнать, может ли он получить местоположение.

Смежные вопросы