判断应用或Activity是否存在

一、判断应用是否存在
方法一:

通过usb连接的方式 使用adb命令查看已安装的文件列表

adb shell pm list package

然后通过查看列表里是否含有对应的应用名来判断是否安装某应用,列表如下:

$ adb shell pm list package
package:com.google.android.carriersetup
package:com.sangfor..client.awork.std
package:com.github.s
package:com.android.cts.priv.ctsshim
package:com.google.android.youtube
package:com.vzw.apnlib
package:com.android.internal.display.cutout.emulation.corner
package:com.google.android.ext.services
package:com.android.internal.display.cutout.emulation.double
package:com.google.android.overlay.pixelconfig2017
package:com.android.providers.telephony
package:com.android.dynsystem
package:com.android.sdm.plugins.connmo
package:com.google.android.googlequicksearchbox
package:com.android.providers.calendar
package:com.android.providers.media
package:com.google.android.apps.docs.editors.docs
package:com.android.theme.icon.square
package:com.google.android.onetimeinitializer
package:com.google.android.ext.shared
package:com.android.internal.systemui.navbar.gestural_wide_back
package:com.qualcomm.ltebc_vzw
package:com.quicinc.cne.CNEService
package:com.android.theme.color.cinnamon
package:com.htc.omadm.trigger
package:com.google.euiccpixel
package:com.android.theme.icon_pack.rounded.systemui
package:com.android.externalstorage
package:com.google.android.apps.enterprise.dmagent
package:com.android.htmlviewer
package:com.qualcomm.qti.uceShimService
package:com.android.companiondevicemanager
package:com.verizon.obdm_permissions
package:com.android.mms.service
package:com.android.providers.downloads
package:com.sangfor..demo
package:com.google.android.apps.messaging
package:com.google.android.soundpicker
package:com.android.theme.icon_pack.rounded.android
package:com.sangfor.securereader
package:com.sangfor.example.sfauthdemo
package:com.sunrisedutyfree.apps.sunrisemembers
package:android.autoinstalls.config.google.nexus
package:com.example.android.jniclient
package:com.qualcomm.qti.telephonyservice
package:com.android.theme.icon_pack.circular.themepicker
package:great.com.test
package:com.google.android.configupdater
package:com.google.android.vr.inputmethod
package:com.google.pixel.wahoo.gfxdrv
package:com.infraware.office.link.china
package:com.google.android.overlay.googlewebview
package:com.google.android.apps.safetyhub
package:com.google.ar.core
package:com.android.providers.downloads.ui
package:com.android.vending
package:com.android.pacprocessor
package:com.android.simappdialog
package:com.android.internal.display.cutout.emulation.tall
package:com.android.certinstaller
package:com.android.theme.color.black
package:com.google.android.marvin.talkback
package:com.android.theme.color.green
package:com.android.theme.color.ocean
package:com.android.theme.color.space
package:com.android.internal.systemui.navbar.threebutton
package:com.google.android.apps.work.oobconfig
package:com.qti.qualcomm.datastatusnotification
package:android
package:com.android.hotwordenrollment.xgoogle
package:com.google.android.pixel.setupwizard
package:com.sangfor..client.phone
package:com.android.theme.icon_pack.rounded.launcher
package:com.google.android.imaging.easel.service
package:com.sangfor.example.android10namespace
package:com.android.egg
package:com.android.mtp
package:com.android.nfc
package:com.android.ons
package:com.android.stk
package:com.android.launcher3
package:com.android.backupconfirm
package:com.google.android.deskclock
package:com.android.internal.systemui.navbar.twobutton
package:org.codeaurora.ims
package:com.android.statementservice
package:com.beichen.fakelinker
package:com.android.hotspot2
package:com.google.android.as
package:com.google.android.gm
package:com.google.android.carrier
package:com.google.android.apps.tachyon
package:com.yozo.office
package:com.android.sdm.plugins.sprintdm
package:com.jybank.mobile.jynsbank
package:com.sangfor.ssl.safeapp
package:com.google.android.vr.home
package:com.android.internal.systemui.navbar.gestural_extra_wide_back
package:com.google.android.permissioncontroller
package:com.google.intelligence.sense
package:com.google.android.setupwizard
package:com.qualcomm.qcrilmsgtunnel
package:com.android.providers.settings
package:com.android.sharedstoragebackup
package:com.verizon.services
package:com.google.android.angle
package:com.google.android.euicc
package:com.google.android.music
package:com.android.printspooler
package:com.android.hotwordenrollment.okgoogle
package:com.android.sdm.plugins.diagmon
package:com.android.theme.icon_pack.filled.settings
package:com.android.dreams.basic
package:com.android.se
package:com.android.inputdevices
package:com.google.android.apps.wellbeing
package:com.estrongs.android.pop
package:com.google.android.dialer
package:com.android.bips

加入管道个grep命令快速寻找想要的应用包名(如下):

$ adb shell pm list package|grep applica
package:com.example.myapplication1

方法二:

    public boolean checkApplication(String packageName) {
        if (TextUtils.isEmpty(packageName)) {
            return false;
        }

        try {
            ApplicationInfo info = getPackageManager().getApplicationInfo(packageName, PackageManager.GET_UNINSTALLED_PACKAGES);
            return true;
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
            return false;
        }
    }

方法三:

通过PackageManager判断是否存在该包的程序

    private boolean isApkInstalled(String packageName) {
        PackageInfo packageInfo = null;
        try {
            packageInfo = this.getPackageManager().getPackageInfo(packageName, 0);
        } catch (NameNotFoundException e) {
            packageInfo = null;
            e.printStackTrace();
        }
        return packageInfo != null;
    }

方法四:

此方法既可以判断应用又能获取某应用的入口activity,对于一般有界面的应用来说,如果没有获取到他对应的入口activity,那么我们也可以认为他没有安装(没有入口activity的应用除外)

        Intent resIntent = this.getPackageManager().getLaunchIntentForPackage("包名");
        if (resIntent == null) {
            //该情况下,既找不到指定包的入口activity
        }


二、判断Activity是否存在

 

 Intent intent = new Intent();
  intent.setClassName("包名", "类名");

方法一:

if (getPackageManager().resolveActivity(intent, 0) == null) {  

       // 说明系统中不存在这个activity
  }

方法二:

 if(intent.resolveActivity(getPackageManager()) == null) {  

       // 说明系统中不存在这个activity
  }

方法三:

List list = getPackageManager().queryIntentActivities(intent, 0);
        if (list.size() == 0) {
            // 说明系统中不存在这个activity
        }

参考自:

https://www.cnblogs.com/devcjq/articles/7688202.html


 

你可能感兴趣的:(android,android开发实战)