文章目录

  • 准备
  • 项目
  • 参考

准备

开发环境:

Android Studio 4.1.1Build #AI-201.8743.12.41.6953283, built on November 5, 2020Runtime version: 1.8.0_242-release-1644-b01 amd64VM: OpenJDK 64-Bit Server VM by JetBrains s.r.oWindows 10 10.0GC: ParNew, ConcurrentMarkSweepMemory: 1237MCores: 8Registry: ide.new.welcome.screen.force=true, external.system.auto.import.disabled=true

项目

新建项目,选择 Empty Activity,在配置项目时,我选择的 Minimum SDKAPI 16: Android 4.1 (Jelly Bean)

app\build.gradle 文件(这是默认的,我没有修改):

plugins {    id 'com.android.application'}android {    compileSdkVersion 30    buildToolsVersion "30.0.3"    defaultConfig {        applicationId "com.example.gotoenablelocationservice"        minSdkVersion 16        targetSdkVersion 30        versionCode 1        versionName "1.0"        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"    }    buildTypes {        release {            minifyEnabled false            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'        }    }    compileOptions {        sourceCompatibility JavaVersion.VERSION_1_8        targetCompatibility JavaVersion.VERSION_1_8    }}dependencies {    implementation 'androidx.appcompat:appcompat:1.1.0'    implementation 'com.google.android.material:material:1.1.0'    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'    testImplementation 'junit:junit:4.+'    androidTestImplementation 'androidx.test.ext:junit:1.1.1'    androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'}

编辑 src\main\res\layout\activity_main.xml 文件,删除原有的 TextView 元素,新增 Button 元素:

<?xml version="1.0" encoding="utf-8"?><androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:app="http://schemas.android.com/apk/res-auto"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    tools:context=".MainActivity">    <Button        android:id="@+id/button"        android:layout_width="0dp"        android:layout_height="wrap_content"        android:layout_marginStart="16dp"        android:layout_marginLeft="16dp"        android:layout_marginTop="16dp"        android:layout_marginEnd="16dp"        android:layout_marginRight="16dp"        android:text="Go To Enable Location Service"        app:layout_constraintEnd_toEndOf="parent"        app:layout_constraintStart_toStartOf="parent"        app:layout_constraintTop_toTopOf="parent" />androidx.constraintlayout.widget.ConstraintLayout>

编辑 MainActivity 文件(主要代码是第 51、57 行):

package com.example.gotoenablelocationservice;import androidx.annotation.Nullable;import androidx.appcompat.app.AppCompatActivity;import android.content.Context;import android.content.Intent;import android.location.LocationManager;import android.os.Bundle;import android.provider.Settings;import android.view.View;import android.widget.Button;import android.widget.Toast;public class MainActivity extends AppCompatActivity {         private static final int REQUEST_CODE_GPS = 1;    private Button button = null;    @Override    protected void onCreate(Bundle savedInstanceState) {             super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        initView();    }    @Override    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {             super.onActivityResult(requestCode, resultCode, data);        if (requestCode == REQUEST_CODE_GPS) {                 LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);            if (locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {                     Toast.makeText(MainActivity.this, "用户打开定位服务", Toast.LENGTH_LONG).show();            } else {                     Toast.makeText(MainActivity.this, "用户关闭定位服务", Toast.LENGTH_LONG).show();            }        }    }    private void initView() {             button = (Button) findViewById(R.id.button);        button.setOnClickListener(new View.OnClickListener() {                 @Override            public void onClick(View v) {                     Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);                // https://developer.android.google.cn/guide/components/intents-filters?hl=zh-cn#ExampleSend                // https://developer.android.google.cn/reference/android/content/Intent?hl=zh-cn#resolveActivity(android.content.pm.PackageManager)                // 判断是否有合适的应用能够处理该 Intent,并且可以安全调用 startActivity()。                if (intent.resolveActivity(getPackageManager()) != null) {                         startActivityForResult(intent, REQUEST_CODE_GPS);                } else {                         Toast.makeText(MainActivity.this, "该设备不支持位置服务", Toast.LENGTH_SHORT).show();                }            }        });    }}

参考

Android引导用户打开手机位置服务

更多相关文章

  1. android开机动画bootanimation
  2. android Gradle打包修改生成的apk文件名称
  3. Android(安卓)简单视频播放器(破烂版,后续更新)
  4. Android实现文件上传功能
  5. 解决Android(安卓)studio 启动报错java.lang.RuntimeException:
  6. Android中遍历文件夹、比较文件类型测试
  7. Android系统启动-SystemServer下篇
  8. Android存储数据到本地文件
  9. android的service中在后台弹出提示框

随机推荐

  1. Android网络编程之——Android登录系统模
  2. Android媒体扫描代码分析
  3. 什么是Anddroid ANR
  4. [置顶] 定义一个ImageVIew出现警告的去除
  5. 用SQLiteOpenHelper实现操作SQLite
  6. AdapterViewFlipper实现翻滚广告效果
  7. andorid跳过系统API获取以太网IP,网关,DNS,M
  8. 引入第三方库ic_launcher冲突
  9. Android热修复原理(一)热修复框架对比和代
  10. Android(安卓)Studio的GridLayout中使按