打开Android系统应用/实用程序

时间:2014-05-27 18:27:33

标签: java android android-intent

我正在学习Android。我学会了使用我的应用程序启动我写的另一个应用程序。使用以下代码:

public class MainActivity extends Activity {

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


    Button appOpener = (Button) findViewById(R.id.button1);
    appOpener.setOnClickListener(new View.OnClickListener(){
        public void onClick(View v){
            Intent i = new Intent(Intent.ACTION_MAIN);
            PackageManager firstApp = getPackageManager();
            i = firstApp.getLaunchIntentForPackage("com.assignment.projecttomiko");
        }
    });
}

这很直截了当,我很高兴。但是现在我想在Android平台上启动应用程序,比如浏览器,日历,计算器等。从上面看,我看到我只需要输入其他应用程序的包名。我假设我可以做同样的事情来开始计算器。或者可能不是。做这个的最好方式是什么?如果我可以使用包名称,那么这些内置应用程序的包名称是什么。如果有更好的方式我希望有人帮助我。

我读到了这个:Opening System Application Using Intent并且意识到有一个额外的LaunchComponent的介绍我不知道那是什么,但主要的是我在那个帖子上看到他们也放了一些包名称来调用系统应用程序。完成这项工作的最佳方法是什么? :)

编辑: 我搜索了几个小时并尝试了这段代码但不起作用:(

// activity name and package for stock calculator
 private static final String CALCULATOR_PACKAGE_NAME = "com.android.calculator2";
 private static final String CALCULATOR_CLASS_NAME = "com.android.calculator2.Calculator";


    public void launchCalculator() {

        Intent intent = new Intent();
        intent.setAction(Intent.ACTION_MAIN);
        intent.addCategory(Intent.CATEGORY_LAUNCHER);
        intent.setComponent(new ComponentName(CALCULATOR_PACKAGE_NAME,
                CALCULATOR_CLASS_NAME));
        try {
            this.startActivity(intent);
        } catch (ActivityNotFoundException noSuchActivity) {
            // handle exception where calculator intent filter is not registered
        }
    }

0 个答案:

没有答案