Android高手進階教程(十二)之—-Android 在一個應用中如何啟動另外一個已安裝的應用!

來源:互聯網
上載者:User

今天晚上Jimmy問了我一個問題,就是如何在一個應用中 通過某個事件,而去啟動另外一個已安裝的應用。所以願意和大家分享一下!

而為了能讓大家更加容易的理解,我寫了一個簡單的Demo,我們的程式有倆個按鈕,其中一個點擊會啟動我自己寫的應用(一個3D應用為例),而另外一個按鈕會啟動系統內建的應用(如,日曆,鬧鐘,計算機等等).這裡我一日曆為例子!

首先看一下我們的(點擊第一個按鈕為例):

 

下面是Demo的詳細步驟:

一、建立一個Android工程命名為StartAnotherApplicationDemo.

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

 

view plaincopy to clipboardprint?
  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"?><br /><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"<br /> android:orientation="vertical"<br /> android:layout_width="fill_parent"<br /> android:layout_height="fill_parent"<br /> ><br /><TextView<br /> android:layout_width="fill_parent"<br /> android:layout_height="wrap_content"<br /> android:text="Welcome to Mr Wei's Blog."<br /> /><br /><Button<br /> android:id="@+id/button"<br /> android:layout_width="fill_parent"<br /> android:layout_height="wrap_content"<br /> android:text="Start Another Application"<br />/><br /><Button<br /> android:id="@+id/start_calender"<br /> android:layout_width="fill_parent"<br /> android:layout_height="wrap_content"<br /> android:text="Start Calendar"<br />/><br /></LinearLayout>

 

三、修改主程式StartAnotherApplicationDemo.java代碼如下:

 

view plaincopy to clipboardprint?
  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;<br />import android.app.Activity;<br />import android.content.ComponentName;<br />import android.content.Intent;<br />import android.os.Bundle;<br />import android.view.View;<br />import android.widget.Button;<br />public class StartAnotherApplicationDemo extends Activity { </p><p> private Button mButton01,mButton02; </p><p> public void onCreate(Bundle savedInstanceState) {<br /> super.onCreate(savedInstanceState);<br /> setContentView(R.layout.main); </p><p> mButton01 = (Button)findViewById(R.id.button);<br /> mButton02 = (Button)findViewById(R.id.start_calender); </p><p> //-----啟動我們自身寫的程式------------------<br /> mButton01.setOnClickListener(new Button.OnClickListener(){<br /> public void onClick(View v) {<br /> //-----核心部分----- 前名一個參數是應用程式的包名,後一個是這個應用程式的主Activity名<br /> Intent intent=new Intent();<br /> intent.setComponent(new ComponentName("com.droidnova.android.games.vortex",<br /> "com.droidnova.android.games.vortex..Vortex"));<br /> startActivity(intent);<br /> }<br /> });<br /> //-----啟動系統內建的應用程式------------------<br /> mButton02.setOnClickListener(new Button.OnClickListener(){<br /> public void onClick(View v) {<br /> Intent intent=new Intent();<br /> intent.setComponent(new ComponentName("com.android.calendar", "com.android.calendar.LaunchActivity"));<br /> startActivity(intent);<br /> }<br /> });<br /> }<br />}

 

四、執行之,將得到如上效果!

好了今天就到這裡了,夜深了,收工睡覺!有什麼不明白的,希望大家多留言,我會耐心解答!謝謝~

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.