Android启动第三方应用

作者: 慕涵盛华 | 来源:发表于2018-04-24 09:39 被阅读100次

    最新项目中需要启动第三方App,代码很简单,调用对应的API即可,主要是获取包名。可以使用sdk下自带的aapt来获取应用的一些信息。

    1.首先要配置aapt的环境变量,否则会报命令没有发现。

    使用open命令打开.bash_profile文件,在文件末尾追加以下代码行

    1.  AAPT_HOME=/Users/du/Library/Android/sdk/build-tools/27.0.1  
    2.  export AAPT_HOME  
    3.  export PATH=$PATH:$AAPT_HOME
    

    把AAPT_HOME换成自己的路径,27.0.1 可以是build-tools下面的任意一个版本

    2.通过aapt dump badging命令获取App信息

    aapt dump badging apkPath //apkPath:安装包的路径
    

    下面以京东为例:

    EDZdeMacBook-Pro:~ gfd$ aapt dump badging /Users/fj/Downloads/JDpad.apk
    package: name='com.jingdong.app.mall' versionCode='57940' versionName='6.6.9' platformBuildVersionName='8.0.0'
    sdkVersion:'14'
    targetSdkVersion:'25'
    uses-permission: name='android.permission.INTERNET'
    uses-permission: name='android.permission.ACCESS_WIFI_STATE'
    uses-permission: name='android.permission.ACCESS_NETWORK_STATE'
    uses-permission: name='android.permission.ACCESS_NETWORK_STATE'
    uses-permission: name='android.permission.READ_PHONE_STATE'
    uses-permission: name='android.permission.CALL_PHONE'
    uses-permission: name='android.permission.CAMERA'
    uses-permission: name='android.permission.FLASHLIGHT'
    uses-permission: name='android.permission.VIBRATE'
    uses-permission: name='android.permission.WRITE_EXTERNAL_STORAGE'
    uses-permission: name='android.permission.READ_EXTERNAL_STORAGE'
    uses-permission: name='android.permission.MOUNT_UNMOUNT_FILESYSTEMS'
    uses-permission: name='android.permission.DOWNLOAD_WITHOUT_NOTIFICATION'
    uses-permission: name='com.android.launcher.permission.INSTALL_SHORTCUT'
    uses-permission: name='com.android.launcher.permission.UNINSTALL_SHORTCUT'
    uses-permission: name='android.permission.RESTART_PACKAGES'
    uses-permission: name='android.permission.RECEIVE_BOOT_COMPLETED'
    uses-permission: name='android.permission.GET_TASKS'
    uses-permission: name='android.permission.KILL_BACKGROUND_PROCESSES'
    uses-permission: name='android.permission.READ_CONTACTS'
    uses-permission: name='android.permission.RECORD_AUDIO'
    uses-permission: name='android.permission.ACCESS_COARSE_LOCATION'
    uses-permission: name='android.permission.ACCESS_FINE_LOCATION'
    uses-permission: name='android.permission.ACCESS_LOCATION_EXTRA_COMMANDS'
    uses-permission: name='android.permission.READ_LOGS'
    uses-permission: name='android.permission.EXPAND_STATUS_BAR'
    uses-permission: name='android.permission.MODIFY_AUDIO_SETTINGS'
    uses-permission: name='android.permission.WAKE_LOCK'
    uses-permission: name='android.permission.SYSTEM_ALERT_WINDOW'
    uses-permission: name='com.jingdong.app.mall.permission.self_broadcast'
    uses-permission: name='com.jingdong.app.mall.permission.MIPUSH_RECEIVE'
    uses-permission: name='android.permission.INTERACT_ACROSS_USERS'
    uses-permission: name='android.permission.INTERACT_ACROSS_USERS_FULL'
    uses-permission: name='com.meizu.flyme.push.permission.RECEIVE'
    uses-permission: name='com.jingdong.app.mall.push.permission.MESSAGE'
    uses-permission: name='com.meizu.c2dm.permission.RECEIVE'
    uses-permission: name='com.jingdong.app.mall.permission.C2D_MESSAGE'
    uses-permission: name='getui.permission.GetuiService.com.jingdong.app.mall'
    uses-permission: name='android.permission.RECEIVE_USER_PRESENT'
    uses-permission: name='android.permission.BROADCAST_STICKY'
    uses-permission: name='android.permission.BLUETOOTH'
    uses-permission: name='android.permission.BATTERY_STATS'
    uses-permission: name='android.permission.DISABLE_KEYGUARD'
    uses-permission: name='com.jd.permissions.MSG_BROADCAST'
    uses-permission: name='android.permission.NFC'
    uses-permission: name='android.permission.BLUETOOTH_ADMIN'
    uses-permission: name='android.permission.BLUETOOTH_ADMIN'
    uses-permission: name='android.permission.CHANGE_WIFI_MULTICAST_STATE'
    uses-permission: name='android.permission.CHANGE_NETWORK_STATE'
    uses-permission: name='android.permission.CHANGE_WIFI_STATE'
    uses-permission: name='com.android.launcher.permission.READ_SETTINGS'
    uses-permission: name='com.coloros.mcs.permission.RECIEVE_MCS_MESSAGE'
    uses-permission: name='android.permission.USE_FINGERPRINT'
    application-label:'京东'
    application-icon-120:'r/b/jd_buy_icon.png'
    application-icon-160:'r/b/jd_buy_icon.png'
    application-icon-240:'r/b/jd_buy_icon.png'
    application-icon-320:'r/u/jd_buy_icon.png'
    application-icon-400:'r/s/jd_buy_icon.png'
    application-icon-420:'r/s/jd_buy_icon.png'
    application-icon-480:'r/s/jd_buy_icon.png'
    application-icon-560:'r/k/jd_buy_icon.png'
    application-icon-640:'r/k/jd_buy_icon.png'
    application-icon-65534:'r/b/jd_buy_icon.png'
    application-icon-65535:'r/k/jd_buy_icon.png'
    application: label='京东' icon='r/b/jd_buy_icon.png'
    launchable-activity: name='com.jingdong.app.mall.main.MainActivity'  label='' icon=''
    aapt(850,0x7fff9aa9f3c0) malloc: *** error for object 0x2: pointer being freed was not allocated
    *** set a breakpoint in malloc_error_break to debug
    Abort trap: 6
    EDZdeMacBook-Pro:~ gfd$ 
    

    从上面我们可以看到packagelaunchable-activity,有这两个信息我们就可以在自己的应用里启动它了。

    3.通过Intent启动第三方App

    Intent intent = new Intent(Intent.ACTION_MAIN);
    intent.addCategory(Intent.CATEGORY_LAUNCHER);
    ComponentName cn = new ComponentName("com.jingdong.app.mall",
                    "com.jingdong.app.mall.main.MainActivity");
    intent.setComponent(cn);
    if (intent.resolveActivityInfo(context.getPackageManager(), PackageManager.MATCH_DEFAULT_ONLY) != null) {//启动的intent存在
              context.startActivity(intent);
    } else {
          Toast.makeText(context, "应用未安装", Toast.LENGTH_SHORT).show();
    }
    

    intent.resolveActivityInfo(context.getPackageManager(), PackageManager.MATCH_DEFAULT_ONLY) != null是用来判断启动app是否安装了。看到网上很多都是获取packageInfo = this.getPackageManager().getPackageInfo(pkgName, 0);,然后通过判断packageInfo是否为空来判断是否安装过来。该Api在高版本上会报错。

    关注微信公众号获取更多相关资源

    Android行动派

    相关文章

      网友评论

        本文标题:Android启动第三方应用

        本文链接:https://www.haomeiwen.com/subject/ucedlftx.html