Check whether the application is installed on the Android system or not
12 minute(s) read
|
Published on: May 01, 2021
Updated on: Dec 14, 2021
|
Among these settings, we can mention whether the application designed on the operating system is installed or not. For example, using user interfaces, users can easily use them to be informed whether the app is installed on the system or not. In this part of the tutorial, we want to tell users whether the application is installed on the system. Also, if the app is not installed on the system, go to the application's virtual stores at high speed to purchase and install the requested version of the application. In this case, it will first check whether the app has been installed on the system or not, and if the app is not installed, refer to virtual stores such as Google Play or stores to purchase and install.
Here are some things you will learn in this part of the tutorial:
- When and in what cases do we need to know if an application is fixed on the system?
- Create a project to ensure and check whether the application is installed on the system
- How to direct and transfer users to the page and window of virtual stores to purchase and install applications
- When and in what cases do we need to know if an app is fixed on the system?
In this part of the tutorial, we will give you examples to know why we need to know if the application is fixed on our system or not. For example, consider the situation that the existence and installation of an application depend on other applications. This application must be fixed on the system for other applications to function properly. That's why we have to program in a way that I can go through to make sure they are fixed or not. If the desired app, on which the performance of other applications also depends, is not fixed on the system, a page and a window should be opened to take the user to the download and installation page of the desired app.
In other cases, consider applications for online taxis, of which there are two versions of the app. One version is for passengers, and the other is for online taxi drivers. Suppose one of the versions of these applications is not fixed on the Android system of the driver or passenger, to continue and complete the process. In that case, it is necessary to ensure that both versions of the app are installed on the systems because the performance of this type of applications depends on each other. Must have both types of installation fixed. After installing both versions of the app, if the installation of each of these versions is not fixed, the relevant window and page will open so that users can download the app.
So, we got acquainted with the reasons that needed to be checked and made sure that the applications were installed or not fixed on the system, and we knew why we must check whether the applications were installed. We also knew that after completing the process of checking whether the app is installed or not on the system, if it is not installed, the window and the page related to it will open and direct the user to the window of virtual stores to buy and download the application with the desired version. Transmits.
Creating a project to look over whether the app is installed on the system
To do this, I need to create a project to check whether the app is installed on our system by using some code and applying some settings. There are two ways to do this, and after creating the project, we can use these two methods to check whether the applications are installed.
To create a project first, in this tutorial, the name of the created project is app installed.
The activity we want to use in this project is selected as Empty Activity. It should be noted that the language to be used in this tutorial and the project to check whether the app is installed or not is Java.
The first way to look over if the app is installed on the getPackageInfo system
In this method, which is the first method to check whether the application is installed on the system, we use the getPackageInfo method and try catch commands to search for the name of the desired application package on the list of applications installed on the system. In other words, it can be said that the procedure of the first method to check whether the app is installed on the system is to check the list of installed programs and applications, and if the package name and app name are in the list, the relevant output Shows to the user. In the first method, we search the name of the desired application that we want to install or not on the system from the list of installed programs, and if the name of the desired application is in the list, the user will tell that the desired app on The system is installed. Otherwise, it will give the appropriate message to the user that the desired app is not installed on the system. If the application is not installed on the system, it will take the user to the page and window of virtual stores to buy or download the desired version of the application from a virtual store.
The codes inserted below do all the explanations we provided above, and after checking, they give the appropriate message to the user.
In this case, if the package's name and the app were in the list of installed programs, the message would be displayed inside the try block. Otherwise, the message will be displayed and executed in the catch section.
In the code above, we have defined a boolean method with the name of your choice and installedOrNot after the onCreate method and inside the activity. This defined method has two parameters, the first is the context type, and the second is the string type.
Inside this method, we first create an object of the PackageManager class with the desired name pm. Then we defined a boolean variable called appInstalled.
The package's name and application in the try block are checked using the getPackageInfo method. If the application is already installed on the system, the value true will be inside the appInstalled variable.
It should be noted that for the catch block, an Exception of PackageManager.NameNotFoundException type is defined, related to not finding the package name in the list of fixed programs. This means that if the name of the package and application is not found, this Exception will be executed. Then the false value will be placed inside the variable appInstalled and will message the user that the desired program is not installed on the system.
Then we have to return the variable because it is boolean and appInstalled needs to be returned.
We define the method in onCreate, as follows:
The first parameter is related to context. The second parameter is the package name, which must be entered as a string.
In this section, we give Instagram the Package name to check whether this application is installed or not. The codes that should be included in the activity are as follows:
MainActivity.java
Then, we have to run the project.
After running the program on the emulator software, we will see that the result it gives us is that the application is not installed on the system, and we will see the message "The application has not been installed before."
Then we reinstall this application on the system and check the result again.
After we install the desired application on the system and run it again, unlike the previous part, we will encounter the message "The program is already installed" because we fixed the application on the system.
The second method is to analyze whether the applications are fixed on the intent system.
In this method, I first save the names of all packages and applications installed on the system in a list, and then inside the same list that we have saved, we check the name of the package and application.
The code below adds two methods to the activity.
In the code above, we have defined a function of List type with the desired name getInstalledAppsPackage. Inside this function, there is an activity that is a type of launcher used to specify each of the applications fixed on the system and device using Intent.CATEGORY_LAUNCHER. Their package names are then retrieved using actInfo.applicationInfo. packageName inside the for loop and added to the packageNameList list.
Then I define another method called appStatus, which also uses this method to check the inside of the list to see if the mPackageName we want is in the list or not. In appropriate cases, the relevant message is fixed or not fixed. Offer the user.
Next, we need to call the appStatus method in onCreate.
In this method, we check the installation or non-installation of the VLC player.
It should be noted that the result that we will receive after executing these codes is the message of not installing the application because this VLC player application is not fixed on the system.
How to direct and transfer users to the page and window of virtual stores to purchase and install applications
In this part of the training, direct the user to the page and window of virtual stores to buy and download the desired application. In this case, after checking whether the application is fixed or not, take the necessary steps to transfer and direct the user to the virtual store page. If the application is not fixed on the system, it will take the user to the virtual store page to download the application. It is worth mentioning that the virtual store page where the user will be transferred to purchase and download the application is the default Android virtual store and is pre-installed. Even if there is no virtual store on the system, the user will be redirected to a link and Internet address.
In this part of the tutorial on guiding the user to virtual stores, we will guide the user to a virtual store installed on all Android devices by default.
We will do all this using the try catch commands, and if the VLC Player application is not installed on the system, to install, go to the market and virtual store, which is installed on Android by default. If there is no virtual store and market on the system, refer to the link and Internet address.
I added a method called installApp to the activity and added the following code:
To redirect the user to the application introduction pages in Android markets, we must define the Uri as follows:
For the second part, which is the conditional part, I put the link to the application introduction page in the Play Store in this tutorial.
How to determine and define the page address in Android applications is as follows:
Then, I take the necessary steps in the condition section related to appStatus and call the related methods, as follows:
After running the program, we will see that it directs the user to the market page for download because the VLC application is not fixed in the system. Note: The Play Store is not fixed in the simulation software system by default. You can use other suitable Android markets for the emulator software system.
If there is no virtual market and store in the system, after the necessary checks, it directs the user to an address and internet link to take the necessary steps and download the app. For example, it will redirect the user to play.google.com, the web address.
All the code that should be inside the activity is as follows: