如何在 Android 中以编程方式启用/禁用 GPS?
此示例演示如何在 Android 中以编程方式启用/禁用 GPS。
步骤1 −在 Android Studio 中创建一个新项目,转到文件⇒新建项目,并填写所有必需的信息以创建一个新项目。
步骤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/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignTop="@+id/textView1" android:layout_centerHorizontal="true" android:layout_marginTop="70dp" android:text="Click Here to Enable Disable GPS location service!" /> <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" android:layout_marginTop="132dp" android:gravity="center" android:text="location service Stauts Shows Here" android:textAppearance="?android:attr/textAppearanceLarge" /> </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 {
Button button;
Context context;
Intent intent1;
TextView textview;
LocationManager locationManager ;
boolean GpsStatus ;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button)findViewById(R.id.button1);
textview = (TextView)findViewById(R.id.textView1);
context = getApplicationContext();
CheckGpsStatus();
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
intent1 = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
startActivity(intent1);
}
});
}
public void CheckGpsStatus(){
locationManager = (LocationManager)context.getSystemService(Context.LOCATION_SERVICE);
assert locationManager != null;
GpsStatus = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
if(GpsStatus == true) {
textview.setText("GPS Is Enabled");
} else {
textview.setText("GPS 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 移动设备与计算机连接。要从 Android Studio 运行应用程序,请打开项目的一个活动文件,然后单击工具栏中的运行
图标。选择你的移动设备作为选项,然后检查将显示默认屏幕的移动设备−



单击此处下载项目代码。
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP