zoukankan      html  css  js  c++  java
  • 判断Android应用是否安装、运行

    http://www.open-open.com/lib/view/open1409191580182.html

     

    本文介绍3个方法,判断手机上是否安装了某应用、该应用是否正在运行、获取手机某个APK文件的安装Intent.启动该Intent就可以直接安装该APK。

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    /**
     * 判断应用是否已安装
     
     * @param context
     * @param packageName
     * @return
     */ 
    private boolean isInstalled(Context context, String packageName) { 
        boolean hasInstalled = false
        PackageManager pm = context.getPackageManager(); 
        List<PackageInfo> list = pm 
                .getInstalledPackages(PackageManager.PERMISSION_GRANTED); 
        for (PackageInfo p : list) { 
            if (packageName != null && packageName.equals(p.packageName)) { 
                hasInstalled = true
                break
            
        
        return hasInstalled; 
    }

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    /**
     * 获取文件安装的Intent
     
     * @param file
     * @return
     */ 
    private Intent getFileIntent(File file) { 
        Uri uri = Uri.fromFile(file); 
        String type = "application/vnd.android.package-archive"
        Intent intent = new Intent("android.intent.action.VIEW"); 
        intent.addCategory("android.intent.category.DEFAULT"); 
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
        intent.setDataAndType(uri, type); 
        return intent; 
    }
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    /**
     * 判断应用是否正在运行
     
     * @param context
     * @param packageName
     * @return
     */ 
    private boolean isRunning(Context context, String packageName) { 
        ActivityManager am = (ActivityManager) context 
                .getSystemService(Context.ACTIVITY_SERVICE); 
        List<RunningAppProcessInfo> list = am.getRunningAppProcesses(); 
        for (RunningAppProcessInfo appProcess : list) { 
            String processName = appProcess.processName; 
            if (processName != null && processName.equals(packageName)) { 
                return true
            
        
        return false
    }
  • 相关阅读:
    mysql自动增长的有关问题,怎么恢复从1开始
    java攻城狮之路--复习JDBC
    读书笔记:《写给大家看的面向对象设计》,《程序员的职业素养》,《设计模式其实很简单》
    《浪潮之巅,第二版》,《黑客与画家》,《暗时间》读后感
    查看SharePoint文档库是,显示层次目录,可以点击返回层次
    浪潮之巅 -- 读后感与杂谈
    InfoPath分别定义New/Edit 表单
    SharePoint js操作原生的New/Edit表单
    nodejs中req.body为空的问题
    请求uniRequest,会请求两次
  • 原文地址:https://www.cnblogs.com/bigben0123/p/4267045.html
Copyright © 2011-2022 走看看