What is the function of the startup page
Before I encountered this practical problem, I always thought that the function of the startup page was to beautify the product and improve the software's quality. But in fact, it is more important to play an interceptor. For example, when the app homepage needs to dynamically load data from the Internet, network permissions must be requested. If there is no startup page, the user will directly see a bunch of empty data. Therefore, the launch page has two main functions: 1. Intercept user access, and only when the user grants necessary permissions will be allowed; 2. Buy time for dynamic data loading of home page.
Start page jump
Before raising permissions, first solve the problem by delaying the start page. Through the following code, you can achieve a delay of 2 seconds.
new ().postDelayed(new Runnable() { @Override public void run() { Intent mainIntent = new Intent(,); (mainIntent); (); } },2000);
Also, please set the activity of the startup page as the default startup page in Manifests.
<activity android:name=".Splash" android:theme="@style/ThemeSplash"> <intent-filter> <action android:name="" /> <category android:name="" /> </intent-filter> </activity>
The XML of the startup page can be used as the default page. Here, the startup page has no title bar and replace the base map by introducing the theme.
Add in styles:
<style name="ThemeSplash" parent=""> <!--<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>
Dynamic permissions acquisition
First configure the required permissions in Manifests.
<uses-permission android:name=".ACCESS_COARSE_LOCATION" /> <!-- This permission is used to accessGPSposition --> <uses-permission android:name=".ACCESS_FINE_LOCATION" /> <!-- For accesswifiNetwork information,wifi信息会用于进行网络position --> <uses-permission android:name=".ACCESS_WIFI_STATE" /> <!-- Get operator information,Used to support the provision of operator information related interfaces --> <uses-permission android:name=".ACCESS_NETWORK_STATE" /> <!-- This permission is used to obtainwifiObtain permissions,wifi信息会用来进行网络position --> <uses-permission android:name=".CHANGE_WIFI_STATE" /> <!-- Write to extended storage,Write data to the expansion card,用于写入离线position数据 --> <uses-permission android:name=".WRITE_EXTERNAL_STORAGE" /> <!-- Visit the network,网络position需要上网 --> <uses-permission android:name="" />
Then in the Java file on the startup page, create a permission list:
String[] permissions = new String[]{ .ACCESS_COARSE_LOCATION, // GPS positioning permissions .ACCESS_FINE_LOCATION, // Access to wifi network permissions .ACCESS_WIFI_STATE, // Obtain operator permissions .ACCESS_NETWORK_STATE, // Get wifi location permissions .CHANGE_WIFI_STATE, // Storage permissions .WRITE_EXTERNAL_STORAGE, // Access to network permissions // Network permissions };
Set a function check permission and release it only after all passes (this condition can be modified according to actual conditions)
private void checkPermissions(){ if (.SDK_INT >= Build.VERSION_CODES.M) { int q1 = (getContext(), permissions[0]); int q2 = (getContext(), permissions[1]); int q3 = (getContext(), permissions[2]); int q4 = (getContext(), permissions[3]); int q5 = (getContext(), permissions[4]); int q6 = (getContext(), permissions[5]); int q7 = (getContext(), permissions[6]); // Is the permission authorized? GRANTED---Authorized DINIED---Deny 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) { // If this permission is not granted, prompt the user to request startRequestPermission(); } else{ //Get permissions successfully, jump new ().postDelayed(new Runnable() { @Override public void run() { Intent mainIntent = new Intent(,); (mainIntent); (); } },2000); } } }
If the user clicks Deny, the user is guided to make a permission request.
private void startRequestPermission() { (this, permissions, 321); }
@Override public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { (requestCode, permissions, grantResults); if (requestCode == 321) { if (.SDK_INT >= Build.VERSION_CODES.M) { if (grantResults[0] != PackageManager.PERMISSION_GRANTED) { //If you do not obtain permissions, you can prompt the user to set the interface--->Apply permissions to enable permissions Toast toast = (this, "Please go to the settings interface to grant permissions and start", Toast.LENGTH_LONG); (, 0, 0); (); } else { //Get permissions successfully, jump new ().postDelayed(new Runnable() { @Override public void run() { Intent mainIntent = new Intent(,); (mainIntent); (); } },2000); } } } }
Complete Start Page Control Program
package ; import ; import ; import ; import ; import ; import ; import ; import ; import ; import ; import ; import static ; public class Splash extends AppCompatActivity { String[] permissions = new String[]{ .ACCESS_COARSE_LOCATION, // GPS positioning permissions .ACCESS_FINE_LOCATION, // Access to wifi network permissions .ACCESS_WIFI_STATE, // Obtain operator permissions .ACCESS_NETWORK_STATE, // Get wifi location permissions .CHANGE_WIFI_STATE, // Storage permissions .WRITE_EXTERNAL_STORAGE, // Access to network permissions // Network permissions }; @Override protected void onCreate(Bundle savedInstanceState) { (savedInstanceState); setContentView(.activity_splash); checkPermissions(); } private void checkPermissions(){ if (.SDK_INT >= Build.VERSION_CODES.M) { int q1 = (getContext(), permissions[0]); int q2 = (getContext(), permissions[1]); int q3 = (getContext(), permissions[2]); int q4 = (getContext(), permissions[3]); int q5 = (getContext(), permissions[4]); int q6 = (getContext(), permissions[5]); int q7 = (getContext(), permissions[6]); // Is the permission authorized? GRANTED---Authorized DINIED---Deny 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) { // If this permission is not granted, prompt the user to request startRequestPermission(); } else{ //Get permissions successfully, jump new ().postDelayed(new Runnable() { @Override public void run() { Intent mainIntent = new Intent(,); (mainIntent); (); } },2000); } } } private void startRequestPermission() { (this, permissions, 321); } @Override public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { (requestCode, permissions, grantResults); if (requestCode == 321) { if (.SDK_INT >= Build.VERSION_CODES.M) { if (grantResults[0] != PackageManager.PERMISSION_GRANTED) { //If you do not obtain permissions, you can prompt the user to set the interface--->Apply permissions to enable permissions Toast toast = (this, "Please go to the settings interface to grant permissions and start", Toast.LENGTH_LONG); (, 0, 0); (); } else { //Get permissions successfully, jump new ().postDelayed(new Runnable() { @Override public void run() { Intent mainIntent = new Intent(,); (mainIntent); (); } },2000); } } } } }
This is the article about Android: startup page settings and dynamic permission jumps. For more related content on the dynamic permission jumps on Android startup page, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!