gpt4 book ai didi

android - Android打开另一个应用程序,然后单击按钮

转载 作者:行者123 更新时间:2023-11-29 00:03:13 25 4
gpt4 key购买 nike

我想知道是否有一种方法可以打开另一个应用程序,然后单击某个按钮。就我而言,我需要从我的应用程序中打开Play Market应用程序(使用意图),然后单击“更新”按钮。

拥有一个类似findViewById的方法,当我在另一个应用程序中时可以使用该方法,将是很棒的。

最佳答案

如果您知道软件包名称,这很容易,只需在按钮onClick中调用以下方法即可。

 /** Open another app.
* @param context current Context, like Activity, App, or Service
* @param packageName the full package name of the app to open
* @return true if likely successful, false if unsuccessful
*/
public static boolean openApp(Context context, String packageName) {
PackageManager manager = context.getPackageManager();
try {
Intent i = manager.getLaunchIntentForPackage(packageName);
if (i == null) {
return false;
//throw new ActivityNotFoundException();
}
i.addCategory(Intent.CATEGORY_LAUNCHER);
context.startActivity(i);
return true;
} catch (ActivityNotFoundException e) {
return false;
}
}


用法示例:

要打开Goog​​le Play的软件包名称为com.android.vending。

openApp(this, "com.android.vending");


如果要查询手机中的软件包或应用程序列表,请选中此 link

您需要像下面的代码一样在onCreate中调用onClickListener。

public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.view_spot);

playstoreLaunch = (Button) findViewById(R.id.launch_playstore);

// Launch button click event
playstoreLaunch.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View arg0) {
openApp(MainActivity.this, "com.android.vending");
}
});
}

关于android - Android打开另一个应用程序,然后单击按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48287412/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com