2011-09-13 97 views
0

我要測試是否啓用GPS,如果是顯示一個警告警報啓用GPS和網絡定位

  protected void createGpsDisabledAlert() { 
    AlertDialog builder = new AlertDialog.Builder(this).create(); 
    builder.setMessage("Your GPS is disabled! Would you like to enable it?"); 

    builder.setButton("Enable GPS", new DialogInterface.OnClickListener() { 
     public void onClick(DialogInterface dialog, int id) { 
      showGpsOptions(); 
     } 

    }); 
    builder.setButton2("Do nothing", new DialogInterface.OnClickListener() { 
     public void onClick(DialogInterface dialog, int id) { 
      return; 
     } 
    }); 

    builder.show(); 
} 

private void showGpsOptions() { 
    Intent gpsOptionsIntent = new Intent(
      android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS); 
    startActivity(gpsOptionsIntent); 

} 

我做了調試,似乎一切就OK了,我沒有錯誤,但警報不會出現,即使在方法eneters

預先感謝您

回答

2

我已經創建了一個非常resuseful的方法! 只需根據其所需資源爲每個活動配置onResume即可。這種方法將提醒用戶啓用資源並提供資源設置的快捷方式。用戶仍然無法做任何事情,但一切都已啓用。

更新:http://code.google.com/p/android-resource-checker/

1

檢查這個代碼我添加了GPS啓用或禁用吐司.......

import android.app.Activity; 
import android.content.*; 
import android.location.*; 
import android.net.Uri; 
import android.os.Bundle; 
import android.view.Window; 
import android.widget.*; 

public class DirectionsWhereisparty extends Activity { 
    double destin_lat = 0, destin_lng = 0; 
    double current_lat = 0, current_lng = 0; 
    boolean flag = true; 

    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     requestWindowFeature(Window.FEATURE_NO_TITLE); 

     LocationManager mlocManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); 
     LocationListener mlocListener = new MyLocationListener(); 
     mlocManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, 
       mlocListener); 
    } 

    public void go(double c_lat, double c_lng, double d_lat, double d_lng) { 

     Bundle b = getIntent().getExtras(); 

     if (b != null) { 

      String lat = b.getString("lat"); 
      String lng = b.getString("lng"); 

      d_lat = Double.parseDouble(lat); 
      d_lng = Double.parseDouble(lng); 

      // System.out.println("d_lat " + d_lat); 
      // System.out.println("d_lng " + d_lng); 

      String Text = "My current location is: " + "Latitude = " + c_lat 
        + "Longitude = " + c_lng; 

      Toast.makeText(getApplicationContext(), Text, Toast.LENGTH_SHORT) 
        .show(); 

      Intent intent = new Intent(android.content.Intent.ACTION_VIEW, Uri 
        .parse("http://maps.google.com/maps?saddr=" + c_lat 
          + " , " + c_lng + "&daddr=" + d_lat + " , " 
          + d_lng)); 
      startActivity(intent); 
     } 
    } 

    public class MyLocationListener implements LocationListener 

    { 

     public void onLocationChanged(Location loc) { 

      current_lat = loc.getLatitude(); 
      current_lng = loc.getLongitude(); 

      if (flag == true) { 
       flag = false; 
       go(current_lat, current_lng, destin_lat, destin_lng); 
      } 

     } 

     public void onProviderDisabled(String provider) { 
      Toast.makeText(getApplicationContext(), "Gps Disabled", 
        Toast.LENGTH_SHORT).show(); 

     } 

     public void onProviderEnabled(String provider) { 
      Toast.makeText(getApplicationContext(), "Gps Enabled", 
        Toast.LENGTH_SHORT).show(); 
     } 

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

     } 

    } 

} 
相關問題