Android在应用中依据包名启动另外一个APP

以下为TestIntentData工程

MainActivity如下:

[java][/java] view plaincopy

  1. package cn.testintentdata;
  2. import java.util.List;
  3. import android.net.Uri;
  4. import android.os.Bundle;
  5. import android.view.View;
  6. import android.view.View.OnClickListener;
  7. import android.widget.Button;
  8. import android.app.Activity;
  9. import android.content.ComponentName;
  10. import android.content.Intent;
  11. import android.content.pm.PackageInfo;
  12. import android.content.pm.PackageManager.NameNotFoundException;
  13. import android.content.pm.ResolveInfo;
  14. /**
  15.  * Demo描述:
  16.  * 应用中依据包名启动另外一个APP
  17.  *
  18.  */
  19. public class MainActivity extends Activity {
  20.     private Button mButton;
  21.     @Override
  22.     protected void onCreate(Bundle savedInstanceState) {
  23.         super.onCreate(savedInstanceState);
  24.         setContentView(R.layout.main);
  25.         init();
  26.     }
  27.     private void init(){
  28.         mButton=(Button) findViewById(R.id.button);
  29.         mButton.setOnClickListener(new OnClickListener() {
  30.             @Override
  31.             public void onClick(View view) {
  32.                 startAnotherApp(“cc.testintent”);
  33.             }
  34.         });
  35.     }
  36.     private void startAnotherApp(String packageName){
  37.         PackageInfo packageInfo = null;
  38.         try {
  39.             packageInfo = getPackageManager().getPackageInfo(packageName, 0);
  40.             if (packageInfo==null) {
  41.                 System.out.println(“packageInfo==null”);
  42.             } else {
  43.                 System.out.println(“packageInfo!=null”);
  44.             }
  45.         } catch (NameNotFoundException e) {
  46.             e.printStackTrace();
  47.         }
  48.         //<data android:scheme=”app” android:host=”jp.co.cybird.barcodefootballer/” />
  49.         Intent resolveIntent = new Intent(Intent.ACTION_MAIN, null);
  50.         resolveIntent.setData(Uri.parse(“app://jp.co.cybird.barcodefootballer/”));
  51.         resolveIntent.addCategory(Intent.CATEGORY_LAUNCHER);
  52.         resolveIntent.setPackage(packageInfo.packageName);
  53.         System.out.println(“packageInfo.packageName=”+packageInfo.packageName);
  54.         List<ResolveInfo> resolveInfoList =
  55.         getPackageManager().queryIntentActivities(resolveIntent, 0);
  56.         System.out.println(“resolveInfoList.size()=”+resolveInfoList.size());
  57.         ResolveInfo resolveInfo = resolveInfoList.iterator().next();
  58.         if (resolveInfo != null ) {
  59.             String activityPackageName = resolveInfo.activityInfo.packageName;
  60.             String className = resolveInfo.activityInfo.name;
  61.             Intent intent = new Intent(Intent.ACTION_MAIN);
  62.             intent.addCategory(Intent.CATEGORY_LAUNCHER);
  63.             ComponentName componentName = new ComponentName(activityPackageName, className);
  64.             intent.setComponent(componentName);
  65.             startActivity(intent);
  66.         }
  67.     }
  68. }

main.xml如下:

[html][/html] view plaincopy

  1. <RelativeLayout xmlns:android=”http://schemas.android.com/apk/res/android”
  2.     xmlns:tools=”http://schemas.android.com/tools”
  3.     android:layout_width=”match_parent”
  4.     android:layout_height=”match_parent”
  5.     >
  6.     <Button
  7.         android:id=”@+id/button”
  8.         android:layout_width=”wrap_content”
  9.         android:layout_height=”wrap_content”
  10.         android:text=”click me”
  11.         android:layout_centerInParent=”true”
  12.         />
  13. </RelativeLayout>

以下为TestIntent工程

MainActivity如下:

[java][/java] view plaincopy

  1. package cc.testintent;
  2. import android.os.Bundle;
  3. import android.app.Activity;
  4. public class MainActivity extends Activity {
  5.     @Override
  6.     protected void onCreate(Bundle savedInstanceState) {
  7.         super.onCreate(savedInstanceState);
  8.         setContentView(R.layout.main);
  9.     }
  10. }

main.xml如下:

[html][/html] view plaincopy

  1. <RelativeLayout
  2.     xmlns:android=”http://schemas.android.com/apk/res/android”
  3.     xmlns:tools=”http://schemas.android.com/tools”
  4.     android:layout_width=”match_parent”
  5.     android:layout_height=”match_parent”
  6.    >
  7.     <TextView
  8.         android:layout_width=”wrap_content”
  9.         android:layout_height=”wrap_content”
  10.         android:text=”大 家 好”
  11.         android:layout_centerInParent=”true”
  12.     />
  13. </RelativeLayout>

AndroidManifest.xml如下:

[html][/html] view plaincopy

  1. <?xml version=”1.0″ encoding=”utf-8″?>
  2. <manifest xmlns:android=”http://schemas.android.com/apk/res/android”
  3.     package=”cc.testintent”
  4.     android:versionCode=”1″
  5.     android:versionName=”1.0″ >
  6.     <uses-sdk
  7.         android:minSdkVersion=”8″
  8.         android:targetSdkVersion=”8″ />
  9.     <application
  10.         android:allowBackup=”true”
  11.         android:icon=”@drawable/ic_launcher”
  12.         android:label=”@string/app_name”
  13.         android:theme=”@style/AppTheme” >
  14.         <activity
  15.             android:name=”cc.testintent.MainActivity”
  16.             android:label=”@string/app_name” >
  17.             <intent-filter>
  18.                 <action android:name=”android.intent.action.MAIN” />
  19.                 <data android:scheme=”app”/>
  20.                 <category android:name=”android.intent.category.LAUNCHER” />
  21.             </intent-filter>
  22.         </activity>
  23.     </application>
  24. </manifest>

标签