Android的沉默APK更新沉默、Android、APK

2023-09-03 23:17:09 作者:再无一世长安

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

I would like to have a silent update in my app without any user interaction. But I always get the error code 139. The hardware is rooted! Can anyone help?

下面是code:

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

    }
}

感谢您!

推荐答案

我想避免硬编码整个SD卡路径。尝试是这样的:

I'd avoid hard-coding entire SD card path. Try something like this:

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. :-/
}

另外,要注意有关权限......你可以添加:

Also, be careful about permissions... You could add:

<uses-permission android:name="android.permission.ACCESS_SUPERUSER" />
 
精彩推荐
图片推荐