android中如何关联另外一个包,Android在应用中依据包名启动另外一个APP

以下为TestIntentData工程

MainActivity如下:

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

}

//

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 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如下:

xmlns:tools="http://schemas.android.com/tools"

android:layout_width="match_parent"

android:layout_height="match_parent"

>

android:id="@+id/button"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="click me"

android:layout_centerInParent="true"

/>

以下为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);

}

}

main.xml如下:

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"

>

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="大 家 好"

android:layout_centerInParent="true"

/>

AndroidManifest.xml如下:

package="cc.testintent"

android:versionCode="1"

android:versionName="1.0" >

android:minSdkVersion="8"

android:targetSdkVersion="8" />

android:allowBackup="true"

android:icon="@drawable/ic_launcher"

android:label="@string/app_name"

android:theme="@style/AppTheme" >

android:name="cc.testintent.MainActivity"

android:label="@string/app_name" >

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值