如何在Android中以程式設計方式檢查應用程式是否已安裝?


介紹

在Android應用程式中,我們經常需要藉助其他應用程式來完成某些功能。例如,如果我們想從我們的應用程式中開啟谷歌地圖來顯示兩個位置之間的路線。在開啟谷歌地圖之前,我們首先需要檢查使用者裝置上是否安裝了谷歌地圖應用程式。本文將介紹如何檢查應用程式是否已安裝。

實現

我們將建立一個簡單的應用程式,其中包含一個用於顯示應用程式標題的TextView和一個用於檢查是否安裝了谷歌地圖的按鈕。

步驟 1:在Android Studio中建立一個新專案

開啟Android Studio,點選“新建專案”來建立一個新的Android Studio專案。

點選“新建專案”後,您將看到如下介面。

在這個介面中,選擇“Empty Activity”,然後點選“下一步”。點選下一步後,您將看到如下介面。

在這個介面中,只需指定專案名稱即可。包名將自動生成。

注意 - 請確保選擇Java作為程式語言。

指定所有詳細資訊後,點選“完成”以建立一個新的Android Studio專案。

專案建立完成後,您將看到兩個開啟的檔案:activity_main.xml和MainActivity.java。

步驟 2:處理activity_main.xml

開啟activity_main.xml檔案。(如果此檔案不可見,請在左側面板中導航到app>res>layout>activity_main.xml)。開啟檔案後,新增以下程式碼。程式碼中添加了註釋以便於理解。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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">
   <!-- creating a text view on below line-->
   <TextView
       android:id="@+id/idTVHeading"
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       android:layout_centerInParent="true"
       android:layout_marginStart="20dp"
       android:layout_marginTop="20dp"
       android:layout_marginEnd="20dp"
       android:layout_marginBottom="20dp"
       android:padding="4dp"
       android:text="Check weather Application is installed or not in Android"
       android:textAlignment="center"
       android:textColor="@color/black"
       android:textSize="20sp"
       android:textStyle="bold" />
   <!-- creating a button on below line -->
   <Button
       android:id="@+id/idBtnCheckMaps"
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       android:layout_below="@id/idTVHeading"
       android:layout_margin="15dp"
       android:text="Check Google Maps Application"
       android:textAllCaps="false" />
</RelativeLayout>

說明:在上面的程式碼中,我們建立了一個RelativeLayout作為根佈局。在這個佈局中,我們建立了一個TextView來顯示應用程式標題,還有一個按鈕用於檢查裝置上是否安裝了谷歌地圖。

步驟 3:處理MainActivity.java檔案

開啟MainActivity.java檔案。(如果此檔案不可見,請在左側面板中導航到app>java>您的包名>MainActivity.java)。開啟檔案後,新增以下程式碼。程式碼中添加了註釋以便於理解。

package com.example.java_test_application;

import android.content.Context;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

   // creating variables on below line for text view.
   private Button checkAppBtn;
   @Override
   protected void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.activity_main);
       // initializing variables on below line.
       checkAppBtn = findViewById(R.id.idBtnCheckMaps);

       // on below line adding click listener
       checkAppBtn.setOnClickListener(new View.OnClickListener() {
           @Override
           public void onClick(View v) {
               // on below line we are checking if the google maps is installed or not by specifying package name of google maps.
               if (checkInstallation(MainActivity.this, "com.google.android.apps.maps")) {
                   // on below line displaying a toast message if maps is installed.
                   Toast.makeText(MainActivity.this, "Google Maps is installed on this device..", Toast.LENGTH_SHORT).show();
               } else {
                   // on below line displaying toast message if google maps is not installed.
                   Toast.makeText(MainActivity.this, "Google Maps is not installed on this device..", Toast.LENGTH_SHORT).show();
               }
           }
       });
   }
   public static boolean checkInstallation(Context context, String packageName) {
       // on below line creating a variable for package manager.
       PackageManager pm = context.getPackageManager();
       try {
           // on below line getting package info
           pm.getPackageInfo(packageName, PackageManager.GET_ACTIVITIES);
           // on below line returning true if package is installed.
           return true;
       } catch (PackageManager.NameNotFoundException e) {
           // returning false if package is not installed on device.
           return false;
       }
   }
}

說明:在上面的程式碼中,我們首先為按鈕建立變數。然後是onCreate方法,這是每個Android應用程式的預設方法,在應用程式檢視建立時呼叫。在這個方法中,我們設定ContentView,即activity_main.xml佈局檔案,以設定UI。在onCreate方法中,我們使用在activity_main.xml檔案中給定的ID初始化按鈕變數。然後,我們為按鈕新增一個點選監聽器。在點選監聽器中,我們透過傳遞谷歌地圖應用程式的包名來檢查谷歌地圖應用程式是否已安裝。根據結果顯示Toast訊息。

新增上述程式碼後,只需點選頂部工具欄中的綠色圖示即可在移動裝置上執行應用程式。

注意 - 確保已連線到您的真實裝置或模擬器。

輸出

結論

在本教程中,我們學習瞭如何在Android中以程式設計方式檢查應用程式是否已安裝。

更新於:2023年5月8日

3K+ 瀏覽量

開啟您的職業生涯

完成課程獲得認證

開始學習
廣告
© . All rights reserved.