Android高手进阶教程(十二)--Android 在一个应用中如何启动另外一个已安装的应用!...

news/2024/7/11 0:38:46 标签: Android, OS, XML, Blog

今天晚上Jimmy问了我一个问题,就是如何在一个应用中 通过某个事件,而去启动另外一个已安装的应用。所以愿意和大家分享一下!

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

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

下面是Demo的详细步骤:

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

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

 

view plain copy to clipboard print ?
  1. <?xml version= "1.0"  encoding= "utf-8" ?>   
  2. <LinearLayout xmlns: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= "Welcome to Mr Wei's Blog."   
  11.     />   
  12. <Button   
  13.     android:id= "@+id/button"   
  14.     android:layout_width= "fill_parent"     
  15.     android:layout_height= "wrap_content"     
  16.     android:text= "Start Another Application"   
  17. />   
  18. <Button   
  19.     android:id= "@+id/start_calender"   
  20.     android:layout_width= "fill_parent"     
  21.     android:layout_height= "wrap_content"     
  22.     android:text= "Start Calendar"   
  23. />   
  24. </LinearLayout>  
复制代码 复制 打印代码 打印
  1. <?xml version= "1.0"  encoding= "utf-8" ?>  
  2. <LinearLayout xmlns: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="Welcome to Mr Wei's Blog."   
  11.     />  
  12. <Button  
  13.     android:id="@+id/button"   
  14.     android:layout_width="fill_parent"    
  15.     android:layout_height="wrap_content"    
  16.     android:text="Start Another Application"   
  17. />  
  18. <Button  
  19.     android:id="@+id/start_calender"   
  20.     android:layout_width="fill_parent"    
  21.     android:layout_height="wrap_content"    
  22.     android:text="Start Calendar"   
  23. />  
  24. </LinearLayout>  

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="Welcome to Mr Wei's Blog." /> <Button android:id="@+id/button" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="Start Another Application" /> <Button android:id="@+id/start_calender" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="Start Calendar" /> </LinearLayout>

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

 

view plain copy to clipboard print ?
  1. package  com.android.tutor;   
  2. import  android.app.Activity;   
  3. import  android.content.ComponentName;   
  4. import  android.content.Intent;   
  5. import  android.os.Bundle;   
  6. import  android.view.View;   
  7. import  android.widget.Button;   
  8. public   class  StartAnotherApplicationDemo  extends  Activity {   
  9.       
  10.      private  Button mButton01,mButton02;   
  11.        
  12.      public   void  onCreate(Bundle savedInstanceState) {   
  13.          super .onCreate(savedInstanceState);   
  14.         setContentView(R.layout.main);   
  15.            
  16.         mButton01 = (Button)findViewById(R.id.button);   
  17.         mButton02 = (Button)findViewById(R.id.start_calender);   
  18.            
  19.          //-----启动我们自身写的程序------------------   
  20.         mButton01.setOnClickListener( new  Button.OnClickListener(){   
  21.              public   void  onClick(View v) {   
  22.                  //-----核心部分----- 前名一个参数是应用程序的包名,后一个是这个应用程序的主Activity名   
  23.                 Intent intent= new  Intent();   
  24.                 intent.setComponent( new  ComponentName( "com.droidnova.android.games.vortex" ,    
  25.                                                       "com.droidnova.android.games.vortex..Vortex" ));   
  26.                 startActivity(intent);   
  27.             }              
  28.         });   
  29.        //-----启动系统自带的应用程序------------------   
  30.         mButton02.setOnClickListener( new  Button.OnClickListener(){   
  31.              public   void  onClick(View v) {   
  32.                 Intent intent= new  Intent();   
  33.                 intent.setComponent( new  ComponentName( "com.android.calendar" "com.android.calendar.LaunchActivity" ));   
  34.                 startActivity(intent);   
  35.             }              
  36.         });   
  37.     }   
  38. }  
复制代码 复制 打印代码 打印
  1. package  com.android.tutor;  
  2. import  android.app.Activity;  
  3. import  android.content.ComponentName;  
  4. import  android.content.Intent;  
  5. import  android.os.Bundle;  
  6. import  android.view.View;  
  7. import  android.widget.Button;  
  8. public   class  StartAnotherApplicationDemo  extends  Activity {  
  9.      
  10.     private  Button mButton01,mButton02;  
  11.       
  12.     public   void  onCreate(Bundle savedInstanceState) {  
  13.         super .onCreate(savedInstanceState);  
  14.         setContentView(R.layout.main);  
  15.           
  16.         mButton01 = (Button)findViewById(R.id.button);  
  17.         mButton02 = (Button)findViewById(R.id.start_calender);  
  18.           
  19.         //-----启动我们自身写的程序------------------   
  20.         mButton01.setOnClickListener(new  Button.OnClickListener(){  
  21.             public   void  onClick(View v) {  
  22.                 //-----核心部分----- 前名一个参数是应用程序的包名,后一个是这个应用程序的主Activity名   
  23.                 Intent intent=new  Intent();  
  24.                 intent.setComponent(new  ComponentName( "com.droidnova.android.games.vortex" ,   
  25.                                                      "com.droidnova.android.games.vortex..Vortex" ));  
  26.                 startActivity(intent);  
  27.             }             
  28.         });  
  29.       //-----启动系统自带的应用程序------------------   
  30.         mButton02.setOnClickListener(new  Button.OnClickListener(){  
  31.             public   void  onClick(View v) {  
  32.                 Intent intent=new  Intent();  
  33.                 intent.setComponent(new  ComponentName( "com.android.calendar""com.android.calendar.LaunchActivity" ));  
  34.                 startActivity(intent);  
  35.             }             
  36.         });  
  37.     }  
  38. }  

package com.android.tutor; import android.app.Activity; import android.content.ComponentName; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.widget.Button; public class StartAnotherApplicationDemo extends Activity { private Button mButton01,mButton02; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); mButton01 = (Button)findViewById(R.id.button); mButton02 = (Button)findViewById(R.id.start_calender); //-----启动我们自身写的程序------------------ mButton01.setOnClickListener(new Button.OnClickListener(){ public void onClick(View v) { //-----核心部分----- 前名一个参数是应用程序的包名,后一个是这个应用程序的主Activity名 Intent intent=new Intent(); intent.setComponent(new ComponentName("com.droidnova.android.games.vortex", "com.droidnova.android.games.vortex..Vortex")); startActivity(intent); } }); //-----启动系统自带的应用程序------------------ mButton02.setOnClickListener(new Button.OnClickListener(){ public void onClick(View v) { Intent intent=new Intent(); intent.setComponent(new ComponentName("com.android.calendar", "com.android.calendar.LaunchActivity")); startActivity(intent); } }); } }

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

好了今天就到这里了,夜深了,收工睡觉!有什么不明白的,希望大家多留言,我会耐心解答!谢谢~


http://www.niftyadmin.cn/n/1002735.html

相关文章

苹果帝国会随着手机补贴取消而崩溃吗?

智能手机补贴将彻底消失。预付费移动运营商一直拒绝对手机提供补贴&#xff0c;目前这一趋势已经蔓延至主流运营商&#xff0c;T-Mobile已经不再对手机提供补贴&#xff0c;AT&T也暗示将取消手机补贴。 苹果对移动运营商补贴的依赖程度很高——在移动运营商提供补贴的市场上…

如何去除Visual Studio 中文下的红线

点集菜单栏的工具 -> 选项 -> 环境-> 区域设置-> 语言-> 与Microsoft Windows相同。 英文版&#xff1a; tools-options-Environment-International Settings-Language由english改为Same as Microsoft Windows。

hdu 2454 Degree Sequence of Graph G

Problem - 2454 模拟&#xff0c;利用图的性质&#xff0c;将当前度数最大的点&#xff08;假设度数为d(x)&#xff09;删除的同时&#xff0c;把紧接着的前d(x)大的度数分别减一。如果最终可以全部相消&#xff0c;那么就是一个简单图&#xff0c;否则不是。 View Code 1 #in…

Android OpenGL学习笔记(一)

大家好&#xff0c;今天我讲一下Android OpenGL&#xff0c;这个系列是我的学习笔记&#xff0c;希望对大家有所帮助&#xff01;这一节将给大家简洁的介绍一下术语&#xff0c;以及第一个Android OpenGL程序. 首先让我看们看一下术语: Vertex (顶点) A vertex is a point in…

emos中垃圾邮件处理

1、设置不使用系统默认的垃圾箱原因很简单&#xff1a;如果用客户端outlook等收取的话&#xff0c;垃圾箱的邮件是收不到的&#xff0c;如果垃圾箱的邮件是正常的&#xff0c;那么会造成客户邮件丢失。所以我们把所有邮件送到收件箱&#xff0c;然后由客户端来处理所有邮件。修…

opencv 3.0 Mat 初始化 eye ones zeros create randn

Mat在opencv中起到很重要的作用&#xff0c;是数据的容器 1. 首先是 eye, ones, zeros Mat eye Mat::eye(4,4,CV_8U);Mat ones Mat::ones(4,4,CV_8U);Mat zeros Mat::zeros(4,4,CV_8U);2. 直接初始化Mat的元素&#xff0c;小矩阵很方便 Mat img (Mat_<double>(3,…

华为:Access、Hybrid和Trunk三种模式的理解

简介&#xff1a;【IT动力源原创文章】如欲转载&#xff0c;请注明原创作者&#xff0c;及文章出处&#xff08;IT动力源&#xff09;。违者&#xff0c;IT动力源将保留追究其法律责任的权利&#xff01;来源&#xff1a;IT动力源(ItZero.COM)(http://www.itzero.com/)作者&…

博士生如何进行文献阅读和文献整理?

一、阅读文献之前,先了解写文章的规则! SCI的架构: TITLE ABSTRACT。main message。 INTRODUCTION。why did you do this job? METHODS. how did you do it? RESULTS. what did you do? DISCUSSION AND CONCLUSION. whats the benefit of your job? 二、阅读文献的…