<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" /> <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
isGPS_Enabled = lm.isProviderEnabled(LocationManager.GPS_PROVIDER);
isNetwork_Enabled = lm.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
<?xml version="1.0" encoding="utf-8"?> <fragment xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/googleMap" android:layout_width="fill_parent" android:layout_height="fill_parent" android:name="com.google.android.gms.maps.SupportMapFragment"/>
package com.myapp; import com.google.android.gms.maps.GoogleMap; import com.google.android.gms.maps.SupportMapFragment; import android.app.AlertDialog; import android.content.Context; import android.content.DialogInterface; import android.content.Intent; import android.location.LocationManager; import android.os.Bundle; import android.provider.Settings; import android.support.v4.app.FragmentActivity; public class MainActivity extends FragmentActivity { // Google Map private GoogleMap googleMap; protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); googleMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.googleMap)).getMap(); final AlertDialog.Builder adb = new AlertDialog.Builder(this); LocationManager lm = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE); boolean isGPS_Enabled = false; boolean isNetwork_Enabled = false; try { isGPS_Enabled = lm.isProviderEnabled(LocationManager.GPS_PROVIDER); } catch (Exception ex) { } try { isNetwork_Enabled = lm.isProviderEnabled(LocationManager.NETWORK_PROVIDER); } catch (Exception ex) { } if (!isGPS_Enabled && !isNetwork_Enabled) { adb.setTitle("Warning Location Services!!"); adb.setMessage("Please Enable Location Services."); adb.setNegativeButton("Cancel", null); adb.setPositiveButton("Ok", new AlertDialog.OnClickListener() { public void onClick(DialogInterface dialog, int arg1) { // TODO Auto-generated method stub Intent myIntent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS); startActivity(myIntent); } }); adb.show(); } } }
ช่วยกันสนับสนุนรักษาเว็บไซต์ความรู้แห่งนี้ไว้ด้วยการสนับสนุน Source Code 2.0 ของทีมงานไทยครีเอท