Android在應用中依據包名啟動另外一個APP

來源:互聯網
上載者:User

以下為TestIntentData工程

MainActivity如下:

[java]
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; 
/**
 * Demo描述:
 * 應用中依據包名啟動另外一個APP
 *
 */ 
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); 
            if (packageInfo==null) { 
                System.out.println("packageInfo==null"); 
            } else { 
                System.out.println("packageInfo!=null"); 
            } 
        } catch (NameNotFoundException e) { 
            e.printStackTrace(); 
        } 
          
         
        //<data android:scheme="app" android:host="jp.co.cybird.barcodefootballer/" />  
         
         
        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); 
        System.out.println("packageInfo.packageName="+packageInfo.packageName); 
         
        List<ResolveInfo> resolveInfoList =  
        getPackageManager().queryIntentActivities(resolveIntent, 0); 
         
        System.out.println("resolveInfoList.size()="+resolveInfoList.size()); 
          
        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); 
        } 
         
    } 
 

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;
/**
 * Demo描述:
 * 應用中依據包名啟動另外一個APP
 *
 */
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);
   if (packageInfo==null) {
    System.out.println("packageInfo==null");
   } else {
    System.out.println("packageInfo!=null");
   }
  } catch (NameNotFoundException e) {
   e.printStackTrace();
  }
  
  
  //<data android:scheme="app" android:host="jp.co.cybird.barcodefootballer/" />
  
  
  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);
  System.out.println("packageInfo.packageName="+packageInfo.packageName);
  
  List<ResolveInfo> resolveInfoList =
  getPackageManager().queryIntentActivities(resolveIntent, 0);
  
  System.out.println("resolveInfoList.size()="+resolveInfoList.size());
  
  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如下:

[html]
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    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> 

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    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如下:

[java]
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); 
    } 
 

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);
 }

}

 

main.xml如下:

[html]
<RelativeLayout  
    xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    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> 

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    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如下:

[html]
<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://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> 

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://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>
 

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.