2016-09-06 2 views
0

완벽하게 작동하는 프로그램을 업데이트하려면이 클래스를 사용하고 있습니다. 당신이 클래스를 볼 수있는Android 패키지 관리자 - APK 설치 및 열기

public static class UpdateApp extends AsyncTask < String, Void, Void > { 
private Context context; 
public void setContext(Context contextf) { 
    context = contextf; 
} 

@Override 
protected Void doInBackground(String...arg0) { 
    try { 
    URL url = new URL(arg0[0]); 
    HttpURLConnection c = (HttpURLConnection) url.openConnection(); 
    c.setRequestMethod("GET"); 
    c.setDoOutput(true); 
    c.connect(); 

    String PATH = "/mnt/sdcard/Download/"; 
    File file = new File(PATH); 
    file.mkdirs(); 
    File outputFile = new File(file, "update.apk"); 
    if (outputFile.exists()) { 
    outputFile.delete(); 
    } 
    FileOutputStream fos = new FileOutputStream(outputFile); 

    InputStream is = c.getInputStream(); 

    byte[] buffer = new byte[1024]; 
    int len1 = 0; 
    while ((len1 = is.read(buffer)) != -1) { 
    fos.write(buffer, 0, len1); 
    } 
    fos.close(); 
    is.close(); 

    String filename = "/mnt/sdcard/Download/update.apk"; 
    File filez = new File(filename); 
    if (filez.exists()) { 
    try { 
    final String command = "pm install -r " + filez.getAbsolutePath(); 
    Process proc = Runtime.getRuntime().exec(new String[] { 
     "su", 
     "-c", 
     command 
    }); 
    proc.waitFor(); 
    } catch (Exception e) { 
    e.printStackTrace(); 
    } 
    } 



    } catch (Exception e) { 
    //Log.e("UpdateAPP", "Update error! " + e.getMessage()); 
    } 
    return null; 
} 
} 

"pm install" 명령을 사용한다.

내가하고 싶은 일은 설치가 완료 되 자마자 업데이트 된 응용 프로그램을 실행하여 사용자가 응용 프로그램 외부에 있지 않도록하려는 것입니다. 패키지 관리자 또는 adb를 통해이를 수행 할 수있는 방법이 있습니까?

+0

나는 루팅 된 기기를 사용하고 있다고 가정합니다. "Permission denied"예외가 생기기 때문에 – Maragues

답변

1
String cmd = "am start -n " + getApplicationContext().getPackageName() + "/." + getActivity().getClass().getSimpleName(); 
... 
os.writeBytes("pm install -r " + filename + " && " + cmd); 
... 
관련 문제