Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

Android 應用中依據套件名稱啟動另一個 APP

編輯:關於 Android 程式設計

本篇以 TestIntentData 與 TestIntent 兩個工程為例,示範如何在一個應用中依據套件名稱(package name)啟動另一個 APP。核心思路是先取得目標套件的 PackageInfo,再透過 queryIntentActivities 解析出可啟動的 Activity,最後用 ComponentName 指定並啟動。

一、TestIntentData 工程的 MainActivity

按下按鈕後會呼叫 startAnotherApp 方法,傳入目標套件的名稱:

package cn.testintentdata;
import java.util.List;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.app.Activity;
import android.content.ComponentName;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager.NameNotFoundException;
import android.content.pm.ResolveInfo;
public class MainActivity extends Activity {
 private Button mButton;
 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.main);
  init();
 }
 private void init(){
  mButton=(Button) findViewById(R.id.button);
  mButton.setOnClickListener(new OnClickListener() {
   @Override
   public void onClick(View view) {
    startAnotherApp("cc.testintent");
   }
  });
 }
 private void startAnotherApp(String packageName){
  PackageInfo packageInfo = null;
  try {
   packageInfo = getPackageManager().getPackageInfo(packageName, 0);
  } catch (NameNotFoundException e) {
   e.printStackTrace();
  }
  Intent resolveIntent = new Intent(Intent.ACTION_MAIN, null);
  resolveIntent.setData(Uri.parse("app://jp.co.cybird.barcodefootballer/"));
  resolveIntent.addCategory(Intent.CATEGORY_LAUNCHER);
  resolveIntent.setPackage(packageInfo.packageName);
  List<ResolveInfo> resolveInfoList = getPackageManager().queryIntentActivities(resolveIntent, 0);
  ResolveInfo resolveInfo = resolveInfoList.iterator().next();
  if (resolveInfo != null ) {
   String activityPackageName = resolveInfo.activityInfo.packageName;
   String className = resolveInfo.activityInfo.name;
   Intent intent = new Intent(Intent.ACTION_MAIN);
   intent.addCategory(Intent.CATEGORY_LAUNCHER);
   ComponentName componentName = new ComponentName(activityPackageName, className);
   intent.setComponent(componentName);
   startActivity(intent);
  }
 }
}

對應的版面配置 main.xml 只包含一個置中的按鈕:

<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
   android:layout_width="match_parent"
   android:layout_height="match_parent">
   <Button
       android:id="@+id/button"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:text="click me"
       android:layout_centerInParent="true"
       />
</RelativeLayout>

二、TestIntent 工程(被啟動的目標)

這個工程的 MainActivity 內容相當簡單,只是載入自己的版面:

package cc.testintent;
import android.os.Bundle;
import android.app.Activity;
public class MainActivity extends Activity {
 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.main);
 }
}

其版面配置顯示一段文字:

<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
   android:layout_width="match_parent"
   android:layout_height="match_parent">
   <TextView
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:text="大 家 好"
       android:layout_centerInParent="true"
       />
</RelativeLayout>

三、AndroidManifest.xml 的關鍵設定

重點在於為目標 Activity 設定 intent-filter,特別是 android:scheme 與 LAUNCHER 類別,如此才能被正確解析並啟動:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="https://schemas.android.com/apk/res/android"
   package="cc.testintent"
   android:versionCode="1"
   android:versionName="1.0">
   <uses-sdk
       android:minSdkVersion="8"
       android:targetSdkVersion="8" />
   <application
       android:allowBackup="true"
       android:icon="@drawable/ic_launcher"
       android:label="@string/app_name"
       android:theme="@style/AppTheme">
       <activity
           android:name="cc.testintent.MainActivity"
           android:label="@string/app_name">
            <intent-filter>
               <action android:name="android.intent.action.MAIN" />
               <data android:scheme="app"/>
               <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
   </application>
</manifest>

透過以上兩個工程的搭配,就能實現應用之間依據套件名稱互相啟動的功能。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved