今天晚上Jimmy問了我一個問題,就是如何在一個應用中 通過某個事件,而去啟動另外一個已安裝的應用。所以愿意和大家分享一下!
而為了能讓大家更加容易的理解,我寫了一個簡單的Demo,我們的程序有倆個按鈕,其中一個點擊會啟動我自己寫的應用(一個3D應用為例),而另外一個按鈕會啟動系統自帶的應用(如,日歷,鬧鐘,計算器等等).這里我一日歷為例子!
?
首先看一下我們的效果圖(點擊第一個按鈕為例):
?

?
?
?
下面是Demo的詳細步驟:
?
一、新建一個Android工程命名為StartAnotherApplicationDemo.
?
二、修改main.xml布局,代碼如下:
?
~~~
<?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代碼如下:
?
~~~
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); } }); }}
~~~
?
?
四、執行之,將得到如上效果!
?
?
好了今天就到這里了,夜深了,收工睡覺!有什么不明白的,希望大家多留言,我會耐心解答!謝謝~
- 前言
- (一)Android常用名令集錦(圖文并茂)!
- (二)Android Launcher抽屜類SlidingDrawer的使用!
- (三)Android 中自定義View的應用.
- (四)Android 中自定義屬性(attr.xml,TypedArray)的使用!
- (五)Android 中LayoutInflater的使用!
- (六)Android 中MenuInflater的使用(布局定義菜單)!
- (七)Android 中Preferences的使用!
- (八)Android Widget開發案例(世界杯倒計時!)
- (九)Android Handler的使用!!!
- (十)Android PopupWindow的使用!!!
- (十一)Android 通用獲取Ip的方法(判斷手機是否聯網的方法)!!!
- (十二)Android 在一個應用中如何啟動另外一個已安裝的應用!!!
- (十三)Android 數據庫SQLiteDatabase的使用!!
- (十四)Android Location的使用!!
- (十五)通過Location獲取Address的使用!
- (十六)Android中萬能的BaseAdapter(Spinner,ListView,GridView)的使用!
- Android 中的拿來主義(編譯,反編譯,AXMLPrinter2,smali,baksmali)!
- (十七)Android中Intent傳遞對象的兩種方法(Serializable,Parcelable)!
- (十八)列出Android設備中所有啟動的服務,及判斷某個服務是否開啟!
- (十九)Android開發中,使用線程應該注意的問題!
- (二十)Android與JavaScript方法相互調用!
- (二十一)Android中創建與幾種解析xml的方法!
- (二十二)Android中幾種圖像特效處理的集錦!!
- (二十三)Android中的日歷讀寫操作!!!
- (二十四)Android WebView的緩存!!!
- (二十五)Android 中的AIDL!!!