如何从android中的另一个应用程序启动一个应用程序 [英] how to launch an app from another app in android

查看:444
本文介绍了如何从android中的另一个应用程序启动一个应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个应用程序,我想在其中启动设备上安装的所有应用程序.我已经尝试了以下代码.

I am developing an app in which i want to launch any application installed on my device. I have tried the following code.

Button bClock = (Button) findViewById(R.id.button1);
String app="com.whatsapp";
bClock.setOnClickListener(new OnClickListener() {
  public void onClick(View v) {
    Intent i = new Intent(Intent.ACTION_MAIN);
    PackageManager managerclock = getPackageManager();
    i = managerclock.getLaunchIntentForPackage(app);
    i.addCategory(Intent.CATEGORY_LAUNCHER);
    startActivity(i);
  }
});

显示错误:

无法在定义的内部类中引用非最终变量应用程序 用另一种方法

Cannot refer to a non-final variable app inside an inner class defined in a different method

但是,如果我直接使用"com.whatsapp"而不是存储在String中,则它可以正常工作.帮我解决这个问题

But if I directly use "com.whatsapp" instead of storing in String, it is working. Help me to solve this issue

推荐答案

为进行代码更正,请使String app="com.whatsapp";为最终变量,或者您可以像下面这样直接使用包名称

For your code correction please make String app="com.whatsapp"; a final variable or you can use package name directly like following

您应该使用包管理器的功能.

You should use the function of the package manager.

Context ctx=this; // or you can replace **'this'** with your **ActivityName.this**
try {
Intent i = ctx.getPackageManager().getLaunchIntentForPackage("com.whatsapp");
ctx.startActivity(i);
} catch (NameNotFoundException e) {
    // TODO Auto-generated catch block
}

这篇关于如何从android中的另一个应用程序启动一个应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆