2016-12-26 111 views
1

我的代码使用pm install(root)从下载文件夹安装apk。问题是,在安装应用程序后,我需要自动启动已安装的应用程序。我怎么做?如何在安装后启动我的应用程序?

File sdCard = Environment.getExternalStorageDirectory(); 
    String fileStr = sdCard.getAbsolutePath() + "/download";// + 
                  // "app-release.apk"; 

    File file = new File(fileStr, "xadb-build.apk"); 

    if (file.exists()) { 
     try { 
      String command; 
      command = "pm install -r " + file; 
      Process proc = Runtime.getRuntime().exec(
        new String[] { "su", "-c", command }); 
      proc.waitFor(); 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
    } 

} 

回答

0

您可以注册行动PACKAGE_INSTALLED广播接收器,在该接收器,你可以写逻辑用于启动该应用程序的启动活动

public class InstallReceiver extends BroadcastReceiver { 

     public InstallReceiver() 
     { 

     } 
     @Override 
     public void onReceive(Context context, Intent intent) { 

      Log.d("InstallReceiver", "Install detected."); 
      String packageName = intent.getPackage(); 

      if ("your_app_packageName".equalsIgnoreCase(packageName)) { 
       try { 
        Intent i = ctx.getPackageManager().getLaunchIntentForPackage(packageName); 
        i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
        ctx.startActivity(i); 
       } catch (NameNotFoundException e) { 
        // TODO Auto-generated catch block 
       } 
      } 
     } 

    } 
+0

感谢,我在哪里打电话这段代码? –

+0

@badmom你可以注册一个广播接收器用于动作PACKAGE_INSTALLED,所以一旦你的应用程序被安装,你会收到这个广播,并在该接收器中,你可以启动你的应用程序 –

相关问题