Android 在一个应用中如何启动另外一个已安装的应用

如何在一个应用中 通过某个事件,而去启动另外一个已安装的应用。

而为了能让大家更加容易的理解,我写了一个简单的Demo,我们的程序有俩个按钮,其中一个点击会启动我自己写的应用(一个3D应用为例),而另外一个按钮会启动系统自带的应用(如,日历,闹钟,计算器等等).这里我一日历为例子!

首先看一下我们的效果图(点击第一个按钮为例):

下面是Demo的详细步骤:

一、新建一个Android工程命名为StartAnotherApplicationDemo.

二、修改main.xml布局,代码如下:

  1. <?xmlversion="1.0"encoding="utf-8"?>
  2. <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
  3. android:orientation="vertical"
  4. android:layout_width="fill_parent"
  5. android:layout_height="fill_parent"
  6. >
  7. <TextView
  8. android:layout_width="fill_parent"
  9. android:layout_height="wrap_content"
  10. android:text="WelcometoMrWei'sBlog."
  11. />
  12. <Button
  13. android:id="@+id/button"
  14. android:layout_width="fill_parent"
  15. android:layout_height="wrap_content"
  16. android:text="StartAnotherApplication"
  17. />
  18. <Button
  19. android:id="@+id/start_calender"
  20. android:layout_width="fill_parent"
  21. android:layout_height="wrap_content"
  22. android:text="StartCalendar"
  23. />
  24. </LinearLayout>

三、修改主程序StartAnotherApplicationDemo.java代码如下:

  1. packagecom.android.tutor;
  2. importandroid.app.Activity;
  3. importandroid.content.ComponentName;
  4. importandroid.content.Intent;
  5. importandroid.os.Bundle;
  6. importandroid.view.View;
  7. importandroid.widget.Button;
  8. publicclassStartAnotherApplicationDemoextendsActivity{
  9. privateButtonmButton01,mButton02;
  10. publicvoidonCreate(BundlesavedInstanceState){
  11. super.onCreate(savedInstanceState);
  12. setContentView(R.layout.main);
  13. mButton01=(Button)findViewById(R.id.button);
  14. mButton02=(Button)findViewById(R.id.start_calender);
  15. //-----启动我们自身写的程序------------------
  16. mButton01.setOnClickListener(newButton.OnClickListener(){
  17. publicvoidonClick(Viewv){
  18. //-----核心部分-----前名一个参数是应用程序的包名,后一个是这个应用程序的主Activity名
  19. Intentintent=newIntent();
  20. intent.setComponent(newComponentName("com.droidnova.android.games.vortex",
  21. "com.droidnova.android.games.vortex..Vortex"));
  22. startActivity(intent);
  23. }
  24. });
  25. //-----启动系统自带的应用程序------------------
  26. mButton02.setOnClickListener(newButton.OnClickListener(){
  27. publicvoidonClick(Viewv){
  28. Intentintent=newIntent();
  29. intent.setComponent(newComponentName("com.android.calendar","com.android.calendar.LaunchActivity"));
  30. startActivity(intent);
  31. }
  32. });
  33. }
  34. }

四、执行之,将得到如上效果!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值