从其他应用程序打开活动(从我的应用程序到MiFit活动)

时间:2019-05-27 18:09:22

标签: android android-activity google-fit

我想在Google Fit Syncronize中打开MiFit应用程序,但我遇到了麻烦。如何打开按钮?

这是我的代码:

@OnClick(R.id.div_mi_band)
public void miBandIntegration() {
        Intent intent = new Intent();
        intent.setComponent(new ComponentName("com.xiaomi.hm.health.thirdbind",".thirdbind.ThirdBindActivity"));
        startActivity(intent);
}

2 个答案:

答案 0 :(得分:0)

尝试一下。

Intent launchIntent = getPackageManager().getLaunchIntentForPackage("com.xiaomi.hm.health.thirdbind");
if (launchIntent != null) { 
    startActivity(launchIntent);//null pointer check in case package name was not found
}

答案 1 :(得分:0)

尝试以下方法:

            try{
                boolean isAppInstalled = isPackageInstalled(MainActivity.this,"com.xiaomi.hm.health");
                if (isAppInstalled) {
                    Intent navigationIntent = new Intent();
                    navigationIntent.setPackage("com.xiaomi.hm.health");
                    startActivity(navigationIntent);
                }else{
                    Toast.makeText(MainActivity.this,"MI Fit is not currently installed",Toast.LENGTH_SHORT).show();
                }
            }catch (Exception e){
                Toast.makeText(MainActivity.this,"MI Fit Application is not currently installed",Toast.LENGTH_SHORT).show();
                e.printStackTrace();
            }

要检查是否已安装应用程序:

public static boolean isPackageInstalled(Context context, String packageName) {
        final PackageManager packageManager = context.getPackageManager();
        Intent intent = packageManager.getLaunchIntentForPackage(packageName);
        if (intent == null) {
            return false;
        }
        List <ResolveInfo> list = packageManager.queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY);
        return !list.isEmpty();
    }

根据需要更改软件包名称。

希望它对您有用。