方法一:最直接的就是知道apk的包名和启动类名, 直接启动。apk的包名和类名可以通过APK Manager 来获取
1 | Intent mIntent = new Intent( ); |
2 | ComponentName comp = new ComponentName( "包名" , "类名" ); |
3 | mIntent.setComponent(comp); |
4 | mIntent.setAction( "android.intent.action.VIEW" ); |
5 | |
6 | startActivity(mIntent); |
方法二:如果只知道包名,在这种情况下通常也可以启动,通常调用public abstract Intent getLaunchIntentForPackage (String packageName)
大概意思就是返回一个程序入口的Intent,就是Java程序的Main方法。直接startActivity(返回的intent)即可。
Intent mIntent = getPackageManager()getLaunchIntentForPackage(packageName);
if(mIntent != null) startActivity(mIntent);
方法三:就是如何只提供apk,如何启动呢?在这种情况下,通常只能在sdk源代码下来编译完成
通常引入android.content.pm.PackageParser;
见code:
01 | /* |
02 |
03 | * Utility method to get application information for a given packageURI |
04 |
05 | */ |
06 |
07 | public ApplicationInfo getApplicationInfo(Uri packageURI) { |
08 |
09 | final String archiveFilePath = packageURI.getPath(); |
10 |
11 | PackageParser packageParser = new PackageParser(archiveFilePath); |
12 |
13 | File sourceFile = new File(archiveFilePath); |
14 |
15 | DisplayMetrics metrics = new DisplayMetrics(); |
16 |
17 | metrics.setToDefaults(); |
18 |
19 | PackageParser.Package pkg = packageParser.parsePackage(sourceFile, archiveFilePath, metrics, 0 ); |
20 |
21 | if (pkg == null ) { |
22 |
23 | return null ; |
24 |
25 | } |
26 |
27 | return pkg.applicationInfo; |
28 |
29 | } |
这样就可以得到该apk的包名,接下来方式就和方法二一样则可