2013-09-20 3 views
-2

У меня есть класс, чтобы получить местоположение пользователя.включить gps и использовать его

Если GPS выключен, я включил его и покажу его, но он не работает.

это класс:

public class UseGPS implements Runnable{ 

Activity activity; 
Context context; 

private ProgressDialog pd; 

LocationManager mLocationManager; 
Location mLocation; 
MyLocationListener mLocationListener; 

Location currentLocation = null; 


public UseGPS(Activity Activity, Context Context){ 
    this.activity = Activity; 
    this.context = Context; 

} 

public void getMyPos(){ 

    DialogInterface.OnCancelListener dialogCancel = new DialogInterface.OnCancelListener() { 
     public void onCancel(DialogInterface dialog) { 
      Toast.makeText(activity,"no gps signal",Toast.LENGTH_LONG).show(); 
      handler.sendEmptyMessage(0);     
     }  
    }; 

    pd = ProgressDialog.show(activity,context.getString(R.string.looking_for), context.getString(R.string.gps_signal), true, true, dialogCancel); 
    writeSignalGPS(); 

} 

private void setCurrentLocation(Location loc) { 
    currentLocation = loc; 
} 


private void writeSignalGPS() { 

    Thread thread = new Thread(this); 
    thread.start(); 

} 

public void run() { 

    mLocationManager = (LocationManager)context.getSystemService(Context.LOCATION_SERVICE); 

    Looper.prepare(); 

    mLocationListener = new MyLocationListener(); 
    mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, mLocationListener); 

    Looper.loop(); 
    Looper.myLooper().quit(); 

} 

private Handler handler = new Handler() { 
    @Override 
    public void handleMessage(Message msg) { 
     pd.dismiss(); 
     mLocationManager.removeUpdates(mLocationListener); 

     if (currentLocation!=null) { 
       Toast.makeText(activity,currentLocation.getLatitude(),Toast.LENGTH_LONG).show(); 
       Toast.makeText(activity,currentLocation.getLongitude(),Toast.LENGTH_LONG).show(); 

     } 
    } 

}; 

private class MyLocationListener implements LocationListener { 
    @Override 
    public void onLocationChanged(Location loc) { 
     if (loc != null) { 
      setCurrentLocation(loc); 
      handler.sendEmptyMessage(0); 

     } 
    } 

    @Override 
    public void onProviderDisabled(String provider) { 

      /*turn on GPS*/ 
     Intent intent = new Intent("android.location.GPS_ENABLED_CHANGE"); 
     intent.putExtra("enabled", true); 
     context.sendBroadcast(intent); 


    } 

    @Override 
    public void onProviderEnabled(String provider) { 
     // TODO Auto-generated method stub 
    } 

    @Override 
    public void onStatusChanged(String provider, int status, Bundle extras) { 
     // TODO Auto-generated method stub 
    } 
} 

}

код работает, когда GPS включен, но это не превратить GPS на.

Что я могу сделать?

+3

Если GPS отключен в настройках, то она не может быть включена с помощью кода. Ранние версии Android позволили это, но позже (post Gingerbread?) Версии запрещают это. – NickT

+1

Попросите пользователя включить его – Selvin

+0

Вы не можете ничего сделать, чтобы программно включить GPS. Этот вопрос возник довольно много раз. Пожалуйста, сделайте поиск первым, прежде чем отправлять вопрос. –

ответ

0

При запуске вашего приложения дайте одно всплывающее сообщение с возможностью включения GPS пользователем.

Эта всплывающая кнопка переходит к настройке GPS в настройках для своего пользователя, который может включить GPS.

Вот фрагмент кода:

AlertDialog gpsonBuilder = new AlertDialog.Builder(Home_Activity.this); 
gpsonBuilder.setTitle("Your Gps Provider is disabled please Enable it"); 
gpsonBuilder.setPositiveButton("ON",new DialogInterface.OnClickListener() { 

    public void onClick(DialogInterface arg0, int arg1) { 
startActivity(new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS)); 
         } 
        }); 
      gpsonBuilder.show(); 
Смежные вопросы