如何在安卓中用程式設計方式停用/啟用 GPS?
此示例演示如何用程式設計方式在安卓中停用/啟用 GPS。
步驟 1 − 在 Android Studio 中新建一個專案,轉到 File ⇒ New Project 並填寫所有必需的詳細資訊以新建專案。
步驟 2 − 將以下程式碼新增到 res/layout/activity_main.xml 中。
<?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" tools:context=".MainActivity"> <Button android:id="@+id/buttonGps" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:text="Enable/Disable GPS"/> <TextView android:id="@+id/textView" android:layout_centerInParent="true" android:layout_marginBottom="20dp" android:layout_above="@id/buttonGps" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="24sp" android:text="Location Services" /> </RelativeLayout>
步驟 3 − 將以下程式碼新增到 src/MainActivity.java 中
import android.content.Context;
import android.content.Intent;
import android.location.LocationManager;
import android.provider.Settings;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
TextView textView;
Button buttonGps;
Context context;
LocationManager locationManager ;
boolean GpsStatus ;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView = findViewById(R.id.textView);
buttonGps = findViewById(R.id.buttonGps);
context = getApplicationContext();
CheckGPSStatus();
buttonGps.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
startActivity(intent);
}
});
}
private void CheckGPSStatus() {
locationManager = (LocationManager)context.getSystemService(Context.LOCATION_SERVICE);
GpsStatus = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
if(GpsStatus) {
textView.setText("Location Services Is Enabled");
} else {
textView.setText("Location Services Is Disabled");
}
}
}步驟 4 − 將以下程式碼新增到 androidManifest.xml 中
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="app.com.sample"> <uses-permission android:name="android.permission.ACCESS_FINE_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 Studio 中執行應用,開啟專案的一個 activity 檔案並從工具欄中單擊執行
圖示。選擇你的移動裝置作為選項,然後檢查移動裝置,它將顯示你的預設介面 –




廣告
資料結構
網路
RDBMS
作業系統
Java
iOS
HTML
CSS
安卓
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP