Android silent installation implementation scheme imitates 360 mobile assistant second installation and intelligent installation functions

Many friends have asked me before, how can I achieve silent installation in Android system? The so-called silent installation is to install the program without popping up the installation interface of the system and without affecting any operation of the user. Although this method does not seem to disturb the user, there is a problem, because the Android system will show the permissions declared by the program to the user in the installation interface. The user will evaluate these permissions and decide whether to install the program. However, if the silent installation method is used, there is no place for the user to see the permissions, It is equivalent to the user passively accepting these permissions. In the official view of Android, this display is a very dangerous behavior, so the silent installation of this behavior system will not be open to developers.

However, it is really a poor experience to always pop up an installation dialog box, which Google is aware of. Therefore, the Android system opens the silent installation permission to its own Google play store, that is, all applications downloaded from Google play do not need to pop up the installation dialog box. This fully illustrates the importance of having permissions. You can change your system as you want. Learning from Google's practice, many domestic mobile phone manufacturers also adopt a similar processing method. For example, Xiaomi mobile phone does not need to pop up the installation dialog box to download applications in Xiaomi store, because Xiaomi can customize the Android system in MIUI. Therefore, if we just make an ordinary application, we don't need to consider the function of silent installation, because we only need to put the application on the corresponding store, and we will automatically have the function of silent installation.

But what if what we want to do is a platform similar to a store? For example, 360 mobile assistant is widely installed on various mobile phones, but it exists as an ordinary application without special permissions such as Google or Xiaomi. How should 360 mobile assistant achieve a better installation experience? For this purpose, 360 mobile assistant provides two solutions, second installation (requiring root permission) and intelligent installation, as shown in the figure below:

Therefore, today we will imitate the implementation of 360 mobile assistant to provide you with a set of silent installation solutions.

1、 Second loading

The so-called second installation is actually a silent installation that requires root permission. In fact, the principle of silent installation is very simple, that is, it is OK to call the PM install command of the Android system, but the key problem is that the PM command system does not grant us permission to call, so we can only apply for permission on the mobile phone with root permission.

Let's start by creating a new installtest project, and then create a silentinstall class as the implementation class of the silent installation function. The code is as follows:

You can see that there is only one install () method in the silentinstall class, and all the logic of silent installation is in this method. Let's take a specific look at this method. First, the runtime. Getruntime(). Exec ("Su") method is called on line 21. Apply for root permission here, otherwise the subsequent operations will fail. Then start to assemble the silent installation command on line 24. The format of the command is PM install - R < APK Path >, - R parameter means to overwrite the installation if the APK to be installed already exists. APK path is passed in as a method parameter. The next few lines are the process of executing the above commands. Note that the installation process is synchronous, so we call the process. Waitfor () method below, that is, we will wait here as long as the installation takes. After waiting, it means that the installation process is over. Next, we need to read the installation results and analyze them. The logic of analysis is also very simple. If the installation results contain the word failure, it means that the installation fails. Otherwise, it means that the installation is successful.

The whole method is still very simple and easy to understand. Let's build an environment for calling this method. Modify activity_ The code in main.xml is as follows:

Here, we first determine the main interface of the program. There are four buttons on the main interface. The first button is used to select APK files, the second button is used to start second loading, the third button is used to start intelligent installation service, and the fourth button is used to start intelligent installation. Here, we can only use the first two buttons for the time being. Then, calling the install () method of silentinstall needs to pass in the APK path, so we need to implement the function of file selector and create a new activity_ file_ Explorer.xml and list_ As the layout file of the file selector, item.xml has the following codes:

Then create a fileexploreractivity as the activity of the file selector. The code is as follows:

Since this part of the code has nothing to do with the main idea of this document, it is mainly for the convenience of demo display, so I won't explain it.

Next, modify the code in mainactivity as follows:

It can be seen that in mainactivity, we have defined the callback methods of the four button click events. When you click the select installation package button, you will call onchooseapkfile() method, and when you click the second Install button, you will call onsilentinstall() method. In the onchooseapkfile() method, we open fileexploreractivity through intent, and then read the selected APK file path in the onactivityresult() method. In the onsilentinstall () method, first judge whether the device has root. If there is no root, return directly. Then judge whether the installation package has been selected. If not, return directly. Next, we started a thread to call the silentinstall. Install () method, because the installation process will be time-consuming. If the thread is not opened, the main thread will be stuck. Toast will be used to prompt whether the installation is successful or failed.

That's all the code. Finally, let's configure the androidmanifest.xml file:

There is nothing special. Since you need to read the SD card when selecting the APK file, remember to declare the permission to read the SD card in the androidmanifest.xml file.

In addition, it should be noted that in Android 6.0 system, the permission to read and write SD card is listed as dangerous permission. Therefore, if the targetsdkversion of the program is specified as 23, special 6.0 adaptation needs to be done. Here, for simplicity, I specify targetsdkversion as 22, because the adaptation for 6.0 is not within the scope of the article.

Now run the program to try the second installation function. Remember that the mobile phone must have root. The effect is shown in the figure below:

It can be seen that the Netease News installation package we selected here has been successfully installed on the mobile phone, and the installation interface of the system does not pop up, which proves that the second installation function has been successfully realized.

2、 Intelligent installation

For mobile phones with root, the second installation function can indeed avoid popping up the system installation interface and realize silent installation without affecting the user's operation, but this function is not available for most mobile phones without root. So what should we do? To this end, 360 mobile assistant provides a compromise solution, which is to realize intelligent installation with the help of barrier free services provided by Android. In fact, the so-called intelligent installation is not a real silent installation, because it still needs to pop up the system installation interface, but the user's operation can be released in the installation interface. The intelligent installation function simulates the user's click, and the interface will be closed automatically after the installation is completed. This function needs to be manually enabled by the user, and only supports phones after Android 4.1, as shown in the figure below:

OK, then let's imitate the 360 mobile assistant to realize similar intelligent installation functions.

The implementation principle of intelligent installation function should rely on the barrier free service provided by Android. For a detailed explanation of barrier free service, please refer to the official document: http://developer.android.com/guide/topics/ui/accessibility/services.html 。

First, create a new accessibility in the RES / XML directory_ service_ Config.xml file, the code is as follows:

Among them, packagenames specifies the window activity under which application we want to listen. Here, write com android. Package installer means to monitor the installation interface of Android system. Description specifies the description information displayed to the user in the accessibility service. A large section of 360 mobile assistant in the above figure is specified here. Accessibilityeventtypes specifies which events can be simulated in the listening window. Here, typeallmask means that all events can be simulated. Accessibilityflags can specify some additional parameters of barrier free service. Here, we can pass the default value flagdefault. Accessibilityfeedbacktype specifies the feedback method of accessibility service. In fact, accessibility service is provided by Android for some disabled people. For example, if it is inconvenient for blind people to use mobile phones, they can operate mobile phones with Accessibility Service and voice feedback. In fact, we don't need feedback, so just pass a value, Feedbackgeneric is passed in here. Finally, canretrievewindowcontent specifies whether our program is allowed to read the nodes and contents in the window. It must write true.

Remember to write the content specified in description in the string.xml file, as shown below:

Next, modify the androidmanifest.xml file and configure the accessibility service in it:

Most of the configuration contents in this part are fixed, and an android.permission.bind must be declared_ ACCESSIBILITY_ Service, and there must be an action with the value android.accessibilityservice.accessibilityservice. Then we specify the configuration file just created through < meta data >.

The next step is to implement the specific logic of intelligent installation function. Create a myaccessibilityservice class and inherit it from accessibilityservice. The code is as follows:

The code is not complicated. Let's analyze it. Whenever there is activity in the window, a message will be called back to the onaccessibilityevent () method, so all logic starts here. First, we can get the type of the current event through the accessibilityevent parameter passed in. There are many kinds of events, but we only need to listen to type_ WINDOW_ CONTENT_ Changed and type_ WINDOW_ STATE_ The changed events are OK, because one of the two events must be triggered during the whole installation process. Of course, it is also possible that both are triggered at the same time. In order to prevent secondary processing, we use a map to filter out duplicate events.

The next step is to call the iteratenodesandhandle() method to parse the nodes in the current interface. Here, we traverse all the child nodes in the installation interface recursively. When we find the button node, we will judge whether the text on the button is "Install", "finish" and "OK", If so, simulate the click event, which is equivalent to helping the user automatically operate these buttons. In addition, starting from Android 4.4 system, users need to read all the permissions applied by the application before clicking install. Therefore, if we find Scrollview in the node, simulate the sliding event and slide the interface to the bottom, so that the installation button can be clicked.

Finally, go back to mainactivity to add calls to intelligent installation functions, as shown below:

When we click the start intelligent installation service button, we jump to the barrier free service interface of the system through intent, where we start the intelligent installation service. When the intelligent installation button is clicked, we will jump to the system installation interface through intent, and then all installation operations will be completed automatically.

Now you can run the program again, and the effect is shown in the following figure:

It can be seen that after opening the installation interface of Netease News, we do not need to carry out any manual operation. The sliding of the interface, the clicking of the installation button and the completion button are completed automatically, and will eventually automatically return to the original interface state of the mobile phone. This is the intelligent installation function modeled on the 360 mobile assistant.

OK, that's all for the content of this article. Although we can't completely implement the silent installation, we have completed it as far as possible within the permission, and the 360 mobile assistant can only achieve this step. Are the programmers forced by the product manager to implement the silent installation also justified?

Source code download: http://xiazai.jb51.net/201611/yuanma/AndroidInstallTest (jb51.net).rar

The above is the whole content of this article. I hope it will be helpful to your study, and I hope you can support programming tips.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>