Android静音apk更新

时间:2014-03-08 08:38:28

标签: android auto-update silent

我想在我的应用中进行无提示更新,无需任何用户互动。 但我总是得到错误代码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

    }
}

谢谢!

2 个答案:

答案 0 :(得分:2)

我避免硬编码整个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" />

答案 1 :(得分:1)

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

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());
    }
}