2016-12-26 108 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,所以一旦你的應用程序被安裝,你會收到這個廣播,並在該接收器中,你可以啓動你的應用程序 –

相關問題