当前位置:操作系统 > 安卓/Android >>

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

以下为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.print

补充:移动开发 , Android ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,