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();
} //<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如下:
<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如下:
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如下:
<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如下:
<?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>
Android在应用中依据包名启动另外一个APP的更多相关文章
- Android Studio修改项目中整体包名
莫名的需求,要把之前的apk分成三个不同的apk,还要在应用市场能够上线,麻麻滴这样一听那还不要各个apk包的包名不同以及apk签名文件也不同嘛(签名文件一般也用不同,为防止上线冲突嘛).所以就亲自尝 ...
- Android通过包名启动其他应用,若该应用已启动,则直接将应用切到前台
CommUtil.startActivityForPackage(mContext, "com.autonavi.minimap");//打开高德 CommUtil.java /* ...
- Android Studio修改项目的包名
android studio的修改包名,没有有Eclipse环境中那么好操作.也可能你对Eclipse的操作比较熟悉,对Andoid Studio的操作还不太熟悉.在项目的开发中,你可能遇到需要更改模 ...
- Eclipse中修改包名,提交SVN时报 is out of date,怎么办?
问题:Eclipse中修改包名,提交SVN时报 is out of date,怎么办?描述: Hi,大家好! 我的问题如题,在不考虑用TortoiseSVN客户端直接删除目录这个方法的情况下,有什么方 ...
- eclipse批量修改package、import中的包名
问题:想把以前开发的包,用到新项目中,怎么操作呢? 解决方案: 把文件夹复制到新项目包中,同时需要更改很多package.import中的包名第一步:打开一个java文件,选中要替换的字段: 第二步: ...
- Java 错误:找不到或无法加载主类(源文件中含有包名 package)
1. 问题定位 编译(javac)和执行(java)java 程序时,出现这种类型的错误:找不到或无法加载主类: 首先排除是否是环境变量配置不当造成的问题,只要保证,命令行界面能够识别 javac/j ...
- Android启动另一个APP时,注意disable与enable的问题
在写游戏sdk时候遇到了一个需要在sdk中通过scheme来启动支付宝的免密支付功能,所以需要在设备中通过包名检查一下支付宝是否存在. 此时遇到了一个问题,在三星设备中可以将app给处于disable ...
- Android项目实战(二十二):启动另一个APP or 重启本APP
一.启动另一个APP 目前公司项目需求,一个主APP,需要打开某些小APP,这些小APP是整合了Unity的,但是还是android程序(所有小APP的包名是已知的). 以前没做过,查询了一下实现方法 ...
- Android 一个app启动另一个app
最近,一个app启动另一个app,这个玩法挺火的嘛,有没有试过更新QQ到5.1版本,QQ的健康里面就可以添加其他app,实现从QQ跳转到其他app应用.这个挺好玩的,一下子带来了多少流量啊. 一.先来 ...
随机推荐
- WIFI无线adb调试android
有个需求需要支持android插上键盘,鼠标等外设,但是这样就不能使用microusb口进行adb调试了. 研究了一番,发现可以利用wifi进行adb无线调试(adb应该本身已经支持无线调试). WI ...
- J2SE知识点摘记(三)
1. 在java只允许单继承,而不允许多重继承,也就是说一个子类只能有一个父类.但在java中却允许多层继承. 2. 非内部Class只能被public或者defaul ...
- Oracle EBS-SQL (WIP-7):检查当月任务发放记录.sql
select WE.DESCRIPTION 任务说明, DECODE(WE.S ...
- 文本导出到pdf文件
程序中数据导出是经常有的需求,今天学习把文本导出到pdf文件.主要是用QPrinter,QPainter TextEditToPdf::TextEditToPdf(QWidget *parent, Q ...
- C++实现线程池 .
C++实现线程池. 欢迎转载,转载请注明原出处:http://blog.csdn.net/ithzhang/article/details/9020283 代码地址:https://github.co ...
- Python导入模块的三种形式
Python导入模块的3中方式: 1.import module_name 这样在程序里就可以通过module_name.metnod_name()的方式访问模块里的函数了 Example: > ...
- struts 2 三目运算
<s:set id="styleList" value="{'default','success','warning'}"/> <s:iter ...
- hdu 5578 Friendship of Frog(multiset的应用)
Problem Description N frogs . Two frogs are friends if they come from the same country. The closest ...
- lua的string库与强大的模式匹配
lua原生解释器对字符串的处理能力是十分有限的,强大的字符串操作能力来自于string库.lua的string函数导出在string module中.在lua5.1,同一时候也作为string类型的成 ...
- jcenter那些事儿
jcenter是一个server托管在bintray.com的maven仓库. in project's build.gradle file allprojects { repositories { ...