Android 跳转到华为钱包、小米钱包、Samsung Pay主页面

最近做的一个金融项目中有一个跳转到手机自带的钱包APP的首页的需求,咋一看这还不简单,但是网上一搜资料特别少,连华为钱包、小米钱包、SamsungPay这些应用的包名都找不到,就更别说这些应用的主页面的全路径了。最终经过真机连通Android Studio 查看日志才获取这些关键信息,以下是实现的核心代码,请诸位参阅!

package com.yongbo.wallet;

import android.content.ComponentName;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    private static final String PKG_HUAWEI_PAY = "com.huawei.wallet";
    private static final String PKG_XIAOMI_PAY = "com.mipay.wallet";
    private static final String PKG_SAMSUNG_PAY = "com.samsung.android.spay";

    private static final String HOME_HUAWEI_PAY = "com.huawei.wallet.view.MainActivity";
    private static final String HOME_XIAOMI_PAY = "com.mipay.wallet.ui.MipayEntryActivity";
    private static final String HOME_SAMSUNG_PAY = "com.samsung.android.spay.ui.SpayMainActivity";

    private Button btn_wallet;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        btn_wallet = findViewById(R.id.btn_wallet);

        btn_wallet.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (isAvilible(MainActivity.this, PKG_HUAWEI_PAY)) {
                    Intent intent = new Intent(Intent.ACTION_MAIN);
                    intent.addCategory(Intent.CATEGORY_LAUNCHER);
                    ComponentName componentName = new ComponentName(PKG_HUAWEI_PAY, HOME_HUAWEI_PAY);
                    intent.setComponent(componentName);
                    startActivity(intent);
                } else if (isAvilible(MainActivity.this, PKG_XIAOMI_PAY)) {
                    Intent intent = new Intent(Intent.ACTION_MAIN);
                    intent.addCategory(Intent.CATEGORY_LAUNCHER);
                    ComponentName componentName = new ComponentName(PKG_XIAOMI_PAY, HOME_XIAOMI_PAY);
                    intent.setComponent(componentName);
                    startActivity(intent);
                } else if (isAvilible(MainActivity.this, PKG_SAMSUNG_PAY)) {
                    Intent intent = new Intent(Intent.ACTION_MAIN);
                    intent.addCategory(Intent.CATEGORY_LAUNCHER);
                    ComponentName componentName = new ComponentName(PKG_SAMSUNG_PAY, HOME_SAMSUNG_PAY);
                    intent.setComponent(componentName);
                    startActivity(intent);
                } else {
                    showDialog();
                }
            }
        });

    }


    public void showDialog() {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setMessage("您的设备尚未安装钱包应用!");
        builder.setTitle("提示");
        builder.setPositiveButton("确认", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
            }
        });
        builder.create().show();
    }

    /**
     * 检查手机上是否安装了指定的软件
     *
     * @param context
     * @param packageName:应用包名
     * @return
     */
    private boolean isAvilible(Context context, String packageName) {
        //获取packagemanager
        final PackageManager packageManager = context.getPackageManager();
        //获取所有已安装程序的包信息
        List packageInfos = packageManager.getInstalledPackages(0);
        //用于存储所有已安装程序的包名
        List packageNames = new ArrayList();
        //从pinfo中将包名字逐一取出,压入pName list中
        if (packageInfos != null) {
            for (int i = 0; i < packageInfos.size(); i++) {
                String packName = packageInfos.get(i).packageName;
                packageNames.add(packName);
            }
        }
        //判断packageNames中是否有目标程序的包名,有TRUE,没有FALSE
        return packageNames.contains(packageName);
    }
}

 

你可能感兴趣的:(跳转到华为钱包,跳转到小米钱包,跳转到Samsung,Pay,Android)