【问题标题】:Android silent apk updateAndroid静默apk更新
【发布时间】:2014-04-11 14:28:41
【问题描述】:

我想在我的应用程序中进行静默更新,无需任何用户交互。 但我总是收到错误代码 139。 硬件扎根! 任何人都可以帮忙吗?

代码如下:

public class UpdateAPK extends Activity {

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.updateapk);
    if (isRooted() == true) {
        Toast.makeText(UpdateAPK.this, "Hardware is rooted", Toast.LENGTH_LONG).show();
        try {
            Process install = Runtime.getRuntime().exec(new String[] {"su", "-c", "pm install -r /mnt/sdcard/app.apk"});
            install.waitFor();
            if (install.exitValue() == 0) {
                // Success :)
                Toast.makeText(UpdateAPK.this, "Success!", Toast.LENGTH_LONG).show();
            } else {
                // Fail
                Toast.makeText(UpdateAPK.this, "Failure. Exit code: " + String.valueOf(install.exitValue()), Toast.LENGTH_LONG).show();
            }
        } catch (IOException e) {
            System.out.println(e.toString());
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    } else {

        //Do soemthing else

    }
}

谢谢!

【问题讨论】:

标签: android auto-update silent


【解决方案1】:

我会避免对整个 SD 卡路径进行硬编码。试试这样的:

String filePath = Environment.getExternalStorageDirectory().toString() + "/your_app_directory/your_app_filename.apk";
Process installProcess = null;
int installResult = -666;

try
{
    installProcess = Runtime.getRuntime().exec("su -c pm install -r " + filePath);
}
catch (IOException e)
{
    // Handle IOException the way you like.
}

if (installProcess != null)
{
    try
    {
        installResult = installProcess.waitFor();
    }
    catch(InterruptedException e)
    {
        // Handle InterruptedException the way you like.
    }
}

if (installResult == 0)
{
    // Success!
}
else
{
    // Failure. :-/
}

另外,请注意权限...您可以添加:

<uses-permission android:name="android.permission.ACCESS_SUPERUSER" />

【讨论】:

    【解决方案2】:

    首先声明这个变量,然后在任何你想要的地方调用函数。然后授予超级用户,在您的超级用户应用程序上,选中始终授予选项以进行非用户交互。

    final String libs = "LD_LIBRARY_PATH=/vendor/lib:/system/lib ";
    
    final String commands = libs + "pm install -r " + "your apk directory"+ "app.apk";
    
    instalarApk(commands);
    
    
    
    private void instalarApk( String commands ) {
        try {
    
            Process p = Runtime.getRuntime().exec( "su" );
            InputStream es = p.getErrorStream();
            DataOutputStream os = new DataOutputStream(p.getOutputStream());
    
            os.writeBytes(commands + "\n");
    
            os.writeBytes("exit\n");
            os.flush();
    
            int read;
            byte[] buffer = new byte[4096];
            String output = new String();
            while ((read = es.read(buffer)) > 0) {
                output += new String(buffer, 0, read);
            }
    
            p.waitFor();
    
        } catch (IOException e) {
            Log.v(Debug.TAG, e.toString());
        } catch (InterruptedException e) {
            Log.v(Debug.TAG, e.toString());
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-12-20
      相关资源
      最近更新 更多