如何檢視 Android 應用中的位置服務是否已啟用?
本示例演示如何檢視 Android 應用中的位置服務是否已啟用。
步驟 1 − 在 Android Studio 中建立一個新專案,轉至檔案 ⇒ 新專案,然後填寫所有必需的詳細資訊以建立一個新專案。
步驟 2 − 將以下程式碼新增到 res/layout/activity_main.java
<? xml version= "1.0" encoding= "utf-8" ?> <RelativeLayout xmlns: android = "http://schemas.android.com/apk/res/android" xmlns: tools = "http://schemas.android.com/tools" android :layout_width= "match_parent" android :layout_height= "match_parent" android :layout_margin= "16dp" tools :context= ".MainActivity" > <Button android :id= "@+id/button" android :layout_width= "match_parent" android :layout_height= "wrap_content" android :text= "Enable Location" /> </RelativeLayout>
步驟 3 − 將以下程式碼新增到 src/MainActivity.java
package app.tutorialspoint.com.sample ;
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.v7.app.AlertDialog ;
import android.support.v7.app.AppCompatActivity ;
import android.view.View ;
import android.widget.Button ;
import android.widget.TextView ;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate (Bundle savedInstanceState) {
super .onCreate(savedInstanceState) ;
setContentView(R.layout. activity_main ) ;
Button button = findViewById(R.id. button ) ;
button.setOnClickListener( new View.OnClickListener() {
@Override
public void onClick (View v) {
locationEnabled() ;
}
}) ;
}
private void locationEnabled () {
LocationManager lm = (LocationManager)
getSystemService(Context. LOCATION_SERVICE ) ;
boolean gps_enabled = false;
boolean network_enabled = false;
try {
gps_enabled = lm.isProviderEnabled(LocationManager. GPS_PROVIDER ) ;
} catch (Exception e) {
e.printStackTrace() ;
}
try {
network_enabled = lm.isProviderEnabled(LocationManager. NETWORK_PROVIDER ) ;
} catch (Exception e) {
e.printStackTrace() ;
}
if (!gps_enabled && !network_enabled) {
new AlertDialog.Builder(MainActivity. this )
.setMessage( "GPS Enable" )
.setPositiveButton( "Settings" , new
DialogInterface.OnClickListener() {
@Override
public void onClick (DialogInterface paramDialogInterface , int paramInt) {
startActivity( new Intent(Settings. ACTION_LOCATION_SOURCE_SETTINGS )) ;
}
})
.setNegativeButton( "Cancel" , null )
.show() ;
}
}
}步驟 4 − 將以下程式碼新增到 androidManifest.xml
<? xml version= "1.0" encoding= "utf-8" ?> <manifest xmlns: android = "http://schemas.android.com/apk/res/android" package= "app.tutorialspoint.com.sample" > <uses-permission android :name= "android.permission.ACCESS_FINE_LOCATION" /> <uses-permission android :name= "android.permission.ACCESS_COARSE_LOCATION" /> <application android :allowBackup= "true" android :icon= "@mipmap/ic_launcher" android :label= "@string/app_name" android :roundIcon= "@mipmap/ic_launcher_round" android :supportsRtl= "true" android :theme= "@style/AppTheme" > <activity android :name= ".MainActivity" > <intent-filter> <action android :name= "android.intent.action.MAIN" /> <category android :name= "android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>
我們嘗試執行你的應用程式。我假設你已經將實際 Android 移動裝置連線到計算機。若要從 Android Studio 執行應用程式,請開啟專案的一個活動檔案,然後單擊工具欄中的執行
圖示。選擇你的移動裝置作為選項,然後檢視將顯示以下預設螢幕的移動裝置——

廣告
資料結構
網路
關係型資料庫管理系統
作業系統
Java
iOS
HTML
CSS
Android
Python
C 語言程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP