时间:2022-12-11 11:53:54 | 栏目:Android代码 | 点击:次
在我遇到这个实际问题之前,我一直认为启动页的作用是美化产品,提升软件逼格。但实际上,它更重要的是起到了一个拦截器的作用。比如,当App首页需要动态得从网上加载数据时,就必须请求网络权限,如果没有启动页,用户将直接看到一堆空数据。因此,启动页主要有两个作用:1、拦截用户访问,只有用户授予必要的权限才准入;2、为主页动态数据加载争取时间。
在提权限之前,首先把启动页延时跳转解决。通过下面的代码,可以实现延迟2秒钟跳转。
new android.os.Handler().postDelayed(new Runnable() { @Override public void run() { Intent mainIntent = new Intent(Splash.this,MainActivity.class); Splash.this.startActivity(mainIntent); Splash.this.finish(); } },2000);
另外注意把启动页的这个Activity在Manifests中设置成默认启动页面。
<activity android:name=".Splash" android:theme="@style/ThemeSplash"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity>
启动页的xml使用默认的页面即可,这里通过引入主题的方式来设置启动页无标题栏,并更换底图。
在styles中添加:
<style name="ThemeSplash" parent="Theme.AppCompat.Light.NoActionBar"> <!--<item name="android:background">@mipmap/ic_launcher</item>--> <item name="android:windowBackground">@drawable/qidong</item> <item name="android:windowNoTitle">true</item> <item name="android:windowFullscreen">true</item> <item name="windowActionBar">false</item> <item name="windowNoTitle">true</item> </style>
首先在Manifests中配置需要的权限。
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> <!-- 这个权限用于访问GPS定位 --> <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> <!-- 用于访问wifi网络信息,wifi信息会用于进行网络定位 --> <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" /> <!-- 获取运营商信息,用于支持提供运营商信息相关的接口 --> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> <!-- 这个权限用于获取wifi的获取权限,wifi信息会用来进行网络定位 --> <uses-permission android:name="android.permission.CHANGE_WIFI_STATE" /> <!-- 写入扩展存储,向扩展卡写入数据,用于写入离线定位数据 --> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> <!-- 访问网络,网络定位需要上网 --> <uses-permission android:name="android.permission.INTERNET" />
然后在启动页的Java文件中,创建权限列表:
String[] permissions = new String[]{ Manifest.permission.ACCESS_COARSE_LOCATION, // GPS定位权限 Manifest.permission.ACCESS_FINE_LOCATION, // 访问wifi网络权限 Manifest.permission.ACCESS_WIFI_STATE, // 获取运营商权限 Manifest.permission.ACCESS_NETWORK_STATE, // 获取wifi定位权限 Manifest.permission.CHANGE_WIFI_STATE, // 存储权限 Manifest.permission.WRITE_EXTERNAL_STORAGE, // 访问网络权限 Manifest.permission.INTERNET // 网络权限 };
设定一个函数检查权限,全部通过才放行(这个条件可根据实际情况修改)
private void checkPermissions(){ if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { int q1 = ContextCompat.checkSelfPermission(getContext(), permissions[0]); int q2 = ContextCompat.checkSelfPermission(getContext(), permissions[1]); int q3 = ContextCompat.checkSelfPermission(getContext(), permissions[2]); int q4 = ContextCompat.checkSelfPermission(getContext(), permissions[3]); int q5 = ContextCompat.checkSelfPermission(getContext(), permissions[4]); int q6 = ContextCompat.checkSelfPermission(getContext(), permissions[5]); int q7 = ContextCompat.checkSelfPermission(getContext(), permissions[6]); // 权限是否已经授权 GRANTED---授权 DINIED---拒绝 if (q1 != PackageManager.PERMISSION_GRANTED || q2 != PackageManager.PERMISSION_GRANTED || q3 != PackageManager.PERMISSION_GRANTED || q4 != PackageManager.PERMISSION_GRANTED || q5 != PackageManager.PERMISSION_GRANTED || q6 != PackageManager.PERMISSION_GRANTED || q7 != PackageManager.PERMISSION_GRANTED) { // 如果没有授予该权限,就去提示用户请求 startRequestPermission(); } else{ //获取权限成功,跳转 new android.os.Handler().postDelayed(new Runnable() { @Override public void run() { Intent mainIntent = new Intent(Splash.this,MainActivity.class); Splash.this.startActivity(mainIntent); Splash.this.finish(); } },2000); } } }
如果用户点击拒绝,则引导用户进行权限请求。
private void startRequestPermission() { ActivityCompat.requestPermissions(this, permissions, 321); }
@Override public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { super.onRequestPermissionsResult(requestCode, permissions, grantResults); if (requestCode == 321) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { if (grantResults[0] != PackageManager.PERMISSION_GRANTED) { //如果没有获取权限,那么可以提示用户去设置界面--->应用权限开启权限 Toast toast = Toast.makeText(this, "请到设置界面授予权限再启动", Toast.LENGTH_LONG); toast.setGravity(Gravity.CENTER, 0, 0); toast.show(); } else { //获取权限成功,跳转 new android.os.Handler().postDelayed(new Runnable() { @Override public void run() { Intent mainIntent = new Intent(Splash.this,MainActivity.class); Splash.this.startActivity(mainIntent); Splash.this.finish(); } },2000); } } } }
Splash.java
package com.baidu.location.demo; import androidx.annotation.NonNull; import androidx.appcompat.app.AppCompatActivity; import androidx.core.app.ActivityCompat; import androidx.core.content.ContextCompat; import android.Manifest; import android.content.pm.PackageManager; import android.os.Build; import android.os.Bundle; import android.content.Intent; import android.view.Gravity; import android.widget.Toast; import static com.baidu.mapapi.BMapManager.getContext; public class Splash extends AppCompatActivity { String[] permissions = new String[]{ Manifest.permission.ACCESS_COARSE_LOCATION, // GPS定位权限 Manifest.permission.ACCESS_FINE_LOCATION, // 访问wifi网络权限 Manifest.permission.ACCESS_WIFI_STATE, // 获取运营商权限 Manifest.permission.ACCESS_NETWORK_STATE, // 获取wifi定位权限 Manifest.permission.CHANGE_WIFI_STATE, // 存储权限 Manifest.permission.WRITE_EXTERNAL_STORAGE, // 访问网络权限 Manifest.permission.INTERNET // 网络权限 }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_splash); checkPermissions(); } private void checkPermissions(){ if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { int q1 = ContextCompat.checkSelfPermission(getContext(), permissions[0]); int q2 = ContextCompat.checkSelfPermission(getContext(), permissions[1]); int q3 = ContextCompat.checkSelfPermission(getContext(), permissions[2]); int q4 = ContextCompat.checkSelfPermission(getContext(), permissions[3]); int q5 = ContextCompat.checkSelfPermission(getContext(), permissions[4]); int q6 = ContextCompat.checkSelfPermission(getContext(), permissions[5]); int q7 = ContextCompat.checkSelfPermission(getContext(), permissions[6]); // 权限是否已经授权 GRANTED---授权 DINIED---拒绝 if (q1 != PackageManager.PERMISSION_GRANTED || q2 != PackageManager.PERMISSION_GRANTED || q3 != PackageManager.PERMISSION_GRANTED || q4 != PackageManager.PERMISSION_GRANTED || q5 != PackageManager.PERMISSION_GRANTED || q6 != PackageManager.PERMISSION_GRANTED || q7 != PackageManager.PERMISSION_GRANTED) { // 如果没有授予该权限,就去提示用户请求 startRequestPermission(); } else{ //获取权限成功,跳转 new android.os.Handler().postDelayed(new Runnable() { @Override public void run() { Intent mainIntent = new Intent(Splash.this,MainActivity.class); Splash.this.startActivity(mainIntent); Splash.this.finish(); } },2000); } } } private void startRequestPermission() { ActivityCompat.requestPermissions(this, permissions, 321); } @Override public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { super.onRequestPermissionsResult(requestCode, permissions, grantResults); if (requestCode == 321) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { if (grantResults[0] != PackageManager.PERMISSION_GRANTED) { //如果没有获取权限,那么可以提示用户去设置界面--->应用权限开启权限 Toast toast = Toast.makeText(this, "请到设置界面授予权限再启动", Toast.LENGTH_LONG); toast.setGravity(Gravity.CENTER, 0, 0); toast.show(); } else { //获取权限成功,跳转 new android.os.Handler().postDelayed(new Runnable() { @Override public void run() { Intent mainIntent = new Intent(Splash.this,MainActivity.class); Splash.this.startActivity(mainIntent); Splash.this.finish(); } },2000); } } } } }