我想问一下,android开发时要引用第三方应用,我要怎么知道程序的入口包名和主Activity呢,比较急呢
2个回答
展开全部
先通过下面的getPackages()方法获得所有系统安装的应用,根据打印出来的日志找到你要找的应用的对应的包名,在调用startAppByPackageName()方法启动应用就可以了,主activity不需要的
private ArrayList<PInfo> getPackages() {
ArrayList<PInfo> apps = getInstalledApps();
final int max = apps.size();
for (int i=0; i<max; i++) {
apps.get(i).prettyPrint();
}
return apps;
}
class PInfo {
private String appname = "";
private String pname = "";
private String versionName = "";
private int versionCode = 0;
private Drawable icon;
private void prettyPrint() {
Log.e("zhm",appname + "\t" + pname + "\t" + versionName + "\t" + versionCode);
}
}
private ArrayList<PInfo> getInstalledApps() {
ArrayList<PInfo> res = new ArrayList<PInfo>();
List<PackageInfo> packs = parent.getPackageManager().getInstalledPackages(0);
for(int i=0;i<packs.size();i++) {
PackageInfo p = packs.get(i);
if (p.versionName == null) {
continue ;
}
PInfo newInfo = new PInfo();
newInfo.appname = p.applicationInfo.loadLabel(parent.getPackageManager()).toString();
newInfo.pname = p.packageName;
newInfo.versionName = p.versionName;
newInfo.versionCode = p.versionCode;
newInfo.icon = p.applicationInfo.loadIcon(parent.getPackageManager());
res.add(newInfo);
}
return res;
}
public void startAppByPackageName(String packageName){
PackageInfo pi = null;
try {
pi = getPackageManager().getPackageInfo(packageName, 0);
} catch (NameNotFoundException e) {
e.printStackTrace();
}
Intent resolveIntent = new Intent(Intent.ACTION_MAIN, null);
resolveIntent.addCategory(Intent.CATEGORY_LAUNCHER);
resolveIntent.setPackage(pi.packageName);
List<ResolveInfo> apps = getPackageManager().queryIntentActivities(resolveIntent, 0);
ResolveInfo ri = apps.iterator().next();
if (ri != null ) {
String packageName1 = ri.activityInfo.packageName;
String className = ri.activityInfo.name;
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_LAUNCHER);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
ComponentName cn = new ComponentName(packageName1, className);
intent.setComponent(cn);
startActivity(intent);
}
}
private ArrayList<PInfo> getPackages() {
ArrayList<PInfo> apps = getInstalledApps();
final int max = apps.size();
for (int i=0; i<max; i++) {
apps.get(i).prettyPrint();
}
return apps;
}
class PInfo {
private String appname = "";
private String pname = "";
private String versionName = "";
private int versionCode = 0;
private Drawable icon;
private void prettyPrint() {
Log.e("zhm",appname + "\t" + pname + "\t" + versionName + "\t" + versionCode);
}
}
private ArrayList<PInfo> getInstalledApps() {
ArrayList<PInfo> res = new ArrayList<PInfo>();
List<PackageInfo> packs = parent.getPackageManager().getInstalledPackages(0);
for(int i=0;i<packs.size();i++) {
PackageInfo p = packs.get(i);
if (p.versionName == null) {
continue ;
}
PInfo newInfo = new PInfo();
newInfo.appname = p.applicationInfo.loadLabel(parent.getPackageManager()).toString();
newInfo.pname = p.packageName;
newInfo.versionName = p.versionName;
newInfo.versionCode = p.versionCode;
newInfo.icon = p.applicationInfo.loadIcon(parent.getPackageManager());
res.add(newInfo);
}
return res;
}
public void startAppByPackageName(String packageName){
PackageInfo pi = null;
try {
pi = getPackageManager().getPackageInfo(packageName, 0);
} catch (NameNotFoundException e) {
e.printStackTrace();
}
Intent resolveIntent = new Intent(Intent.ACTION_MAIN, null);
resolveIntent.addCategory(Intent.CATEGORY_LAUNCHER);
resolveIntent.setPackage(pi.packageName);
List<ResolveInfo> apps = getPackageManager().queryIntentActivities(resolveIntent, 0);
ResolveInfo ri = apps.iterator().next();
if (ri != null ) {
String packageName1 = ri.activityInfo.packageName;
String className = ri.activityInfo.name;
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_LAUNCHER);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
ComponentName cn = new ComponentName(packageName1, className);
intent.setComponent(cn);
startActivity(intent);
}
}
追问
那我是单独写一个工程用来找包名,所显示的包名是对应的运行所连接的安卓设备里面安装的程序的包名,是吗
追答
直接在你的工程里写也可以的,打印出来log只是一个使用方式,你要是想一步到位的话,可以这样,因为你肯定知道你要启动的应用的应用名称,通过比对应用名称直接获得该应用名称的包名,再通过包名启动第三方应用,不用另建工程的
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询