启动apk的常用方法
fmms
13年前
<p>方法一:最直接的就是知道apk的包名和启动类名, 直接启动</p> <pre class="brush:java; toolbar: true; auto-links: false;">Intent mIntent = new Intent( ); ComponentName comp = new ComponentName("包名", "类名"); mIntent.setComponent(comp); mIntent.setAction("android.intent.action.VIEW"); startActivity(mIntent);</pre> <p><span style="line-height:25px;font-family:verdana, Arial, Helvetica;font-size:14px;"><br /> </span></p> <p><span style="line-height:25px;font-family:verdana, Arial, Helvetica;font-size:14px;">方法二:如果只知道包名,在这种情况下通常也可以启动,通常调用</span><span style="line-height:15px;border-collapse:collapse;font-family:monospace;white-space:pre;color:#110000;font-size:12px;-webkit-border-horizontal-spacing:2px;-webkit-border-vertical-spacing:2px;"><span style="color:#000000;font-weight:bold;">public</span> <span style="color:#000000;font-weight:bold;">abstract</span> Intent getLaunchIntentForPackage <span style="color:#009900;">(</span><span style="color:#003399;">String</span> packageName<span style="color:#009900;">)</span></span></p> <p><span style="line-height:24px;font-family:'Lucida Grande', 'Lucida Sans Unicode', Verdana, sans-serif;letter-spacing:-1px;font-size:14px;">大概意思就是返回一个程序入口的Intent,就是Java程序的Main方法。直接startActivity(返回的intent)即可。<br /> </span></p> <p><span style="line-height:24px;font-family:'Lucida Grande', 'Lucida Sans Unicode', Verdana, sans-serif;letter-spacing:-1px;font-size:14px;">Intent mIntent = </span><span style="line-height:15px;border-collapse:collapse;white-space:pre;letter-spacing:-1px;font-size:12px;-webkit-border-horizontal-spacing:2px;-webkit-border-vertical-spacing:2px;"><span style="font-family:monospace;color:#110000;"> </span></span><span style="line-height:15px;border-collapse:collapse;font-family:monospace;white-space:pre;color:#110000;font-size:12px;-webkit-border-horizontal-spacing:2px;-webkit-border-vertical-spacing:2px;">getPackageManager<span style="color:#009900;">(</span><span style="color:#009900;">)</span></span><span style="line-height:15px;border-collapse:collapse;font-family:monospace;white-space:pre;color:#110000;font-size:12px;-webkit-border-horizontal-spacing:2px;-webkit-border-vertical-spacing:2px;"><span style="color:#006633;">getLaunchIntentForPackage</span><span style="color:#009900;">(</span>packageName<span style="color:#009900;">)</span><span style="color:#339933;">;</span></span></p> <p><span style="font-family:monospace;color:#339933;"><span style="line-height:15px;border-collapse:collapse;white-space:pre;font-size:12px;-webkit-border-horizontal-spacing:2px;-webkit-border-vertical-spacing:2px;">if(</span></span><span style="line-height:25px;border-collapse:separate;font-family:verdana, Arial, Helvetica;white-space:normal;color:#000000;font-size:14px;-webkit-border-horizontal-spacing:0px;-webkit-border-vertical-spacing:0px;">mIntent != null</span><span style="font-family:monospace;color:#339933;"><span style="line-height:15px;border-collapse:collapse;white-space:pre;font-size:12px;-webkit-border-horizontal-spacing:2px;-webkit-border-vertical-spacing:2px;">)</span></span> <span style="line-height:25px;font-family:verdana, Arial, Helvetica;font-size:14px;">startActivity(mIntent);<br /> </span></p> <p><span style="line-height:25px;font-family:verdana, Arial, Helvetica;font-size:14px;"><br /> </span></p> <p><span style="line-height:25px;font-family:verdana, Arial, Helvetica;font-size:14px;">方法三:就是如何只提供apk,如何启动呢?在这种情况下,通常只能在sdk源代码下来编译完成<br /> </span></p> <p><span style="line-height:25px;font-family:verdana, Arial, Helvetica;font-size:14px;">通常引入android.content.pm.PackageParser;<br /> </span></p> <p><span style="font-family:verdana, Arial, Helvetica;"><span style="line-height:25px;font-size:14px;">见code:</span></span><br /> </p> <p></p> <pre class="brush:java; toolbar: true; auto-links: false;"> /* * Utility method to get application information for a given packageURI */ public ApplicationInfo getApplicationInfo(Uri packageURI) { final String archiveFilePath = packageURI.getPath(); PackageParser packageParser = new PackageParser(archiveFilePath); File sourceFile = new File(archiveFilePath); DisplayMetrics metrics = new DisplayMetrics(); metrics.setToDefaults(); PackageParser.Package pkg = packageParser.parsePackage(sourceFile, archiveFilePath, metrics, 0); if (pkg == null) { return null; } return pkg.applicationInfo; } </pre> <p><span style="font-family:verdana, Arial, Helvetica;"><span style="line-height:25px;font-size:14px;"><br /> 这样就可以得到该apk的包名,接下来方式就和方法二一样则可</span></span></p>