android app以编程方式安装targetSDK 24,compileSDK 28,minSDK 19

时间:2018-09-03 08:16:56

标签: android install programmatically

我已经实现了autoUpdate功能,该功能会在有可用更新的情况下下载新应用,并删除旧应用并安装新应用。但是在目标SDK 24或更新API级别2以上的Phone os之后,此功能将无法正常工作。因为已经有旧的应用程序,因此我们正在尝试安装新的应用程序。

public class UpdateActivity extends AppCompatActivity implements 
AppConstants {


public static final String TAG = UpdateActivity.class.getSimpleName();
private String appName = "demo.apk";
private int UNINSTALL_REQUEST_CODE = 1;
private final Handler mHideHandler = new Handler();
private View mContentView;
private BiColoredProgress numberProgressBar;
private Runnable backgroundRunnable;
private Runnable finishRunnable;
private Handler handler;
private static final int INSTALL = 1;
private static final int DOWNLOAD = 0;
private static int flag = 0;
private String updateUrl = "";
private class BackgroundDownloadRunnable implements Runnable {
    BackgroundDownloadRunnable() {
    }

    public void run() {
        DownloadFileFromURL d = new DownloadFileFromURL();
        d.execute();
        return;

    }
}


private class FinishDownloadRunnable implements Runnable {
    FinishDownloadRunnable() {
    }

    public void run() {
        uninstallApp();
    }
}

private final Runnable mHidePart2Runnable = new Runnable() {
    @SuppressLint("InlinedApi")
    @Override
    public void run() {
        // Delayed removal of status and navigation bar

        mContentView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LOW_PROFILE
                | View.SYSTEM_UI_FLAG_FULLSCREEN
                | View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY
                | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION);
    }
};

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_update);
    flag = getIntent().getIntExtra("flag", 0);
    updateUrl = getIntent().getStringExtra("updateUrl");
    StrictMode.setThreadPolicy(new 
StrictMode.ThreadPolicy.Builder().permitAll().build());
    handler = new Handler();
    mContentView = findViewById(R.id.fullscreen_content);
    mHideHandler.post(mHidePart2Runnable);

    finishRunnable = new BackgroundDownloadRunnable();
    backgroundRunnable = new FinishDownloadRunnable();

    numberProgressBar =  findViewById(R.id.numberbar8);
    try {
        if (flag == DOWNLOAD) {
            DownloadFileFromURL d = new DownloadFileFromURL();
            d.execute();
            flag = 1;
        } else if (flag == INSTALL) {
            installApp();
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
  }

   private class DownloadFileFromURL extends AsyncTask<String, Integer, 
String> {

    DownloadFileFromURL() {
    }

    protected void onPreExecute() {
        super.onPreExecute();


    }

    protected String doInBackground(String... f_url) {
        try {
            HttpURLConnection c = (HttpURLConnection) new 
URL(updateUrl).openConnection();
            c.setRequestMethod("POST");
            c.setDoOutput(false);
            c.connect();
            int lengthOfFile = c.getContentLength();
            File file = new 
File(Environment.getExternalStorageDirectory().getPath() + "/Download/");
            file.mkdirs();
            File outputFile = new File(file, appName);
            if (outputFile.exists()) {
                outputFile.delete();
            }
            FileOutputStream fos = new FileOutputStream(outputFile);
            InputStream is = c.getInputStream();
            byte[] data = new 
byte[AccessibilityNodeInfoCompat.ACTION_NEXT_HTML_ELEMENT];
            long total1 = 0;
            while (true) {
                int count = is.read(data);
                if (count == -1) {
                    break;
                }
                total1 += (long) count;
                Integer[] strArr = new Integer[1];
                strArr[0] = DOWNLOAD_COUNT + ((int) ((100 * total1) / 
((long) lengthOfFile)));
                publishProgress(strArr);
                fos.write(data, 0, count);
            }
            fos.close();
            is.close();
            installApp();
            new Thread(backgroundRunnable, "VersionChecker").start();
            //uninstallApp();

        } catch (Exception e) {
            Log.e("Error: ", e.getMessage());
        }
        return null;
    }

    protected void onProgressUpdate(Integer... progress) {
        try {
            if (progress[0] > 0 && progress[0] <= 20) {
                numberProgressBar.setProgress(progress[0]);
                numberProgressBar.setColor(Color.parseColor("#d63232"));
            }
            if (progress[0] > 20 && progress[0] <= 40) {
                numberProgressBar.setProgress(progress[0]);
                numberProgressBar.setColor(Color.parseColor("#F1E84B"));
            }
            if (progress[0] > 40 && progress[0] <= 60) {
                numberProgressBar.setProgress(progress[0]);
                numberProgressBar.setColor(Color.parseColor("#2CACE3"));
            }
            if (progress[0] > 60 && progress[0] <= 80) {
                numberProgressBar.setProgress(progress[0]);
                numberProgressBar.setColor(Color.parseColor("#4CBB17"));
            }
            if (progress[0] > 80 && progress[0] <= 100) {
                numberProgressBar.setProgress(progress[0]);
                numberProgressBar.setColor(Color.parseColor("#39FF14"));
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    protected void onPostExecute(String file_url) {
        numberProgressBar.setVisibility(View.GONE);

    }
  }

   protected void onActivityResult(int requestCode, int resultCode, Intent 
data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == this.UNINSTALL_REQUEST_CODE && resultCode != -1) {
        if (resultCode == 0) {
            super.onBackPressed();
           startActivity(intent);
        } else if (resultCode == 1) {
            super.onBackPressed();
        }
     }
   }


  private void uninstallApp() {

    Intent intentDEl = new Intent("android.intent.action.DELETE");
    intentDEl.setData(Uri.parse("package:com.demo"));
    startActivityForResult(intentDEl, UNINSTALL_REQUEST_CODE);
  }

   private void installApp() {
    File apkFile = new 
 File(Environment.getExternalStorageDirectory().getPath() + "/Download/", 
 appName);
    Intent intent = new Intent(Intent.ACTION_VIEW);
    intent.setDataAndType(Uri.fromFile(apkFile), 
"application/vnd.android.package-archive");
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    startActivity(intent);
    finish();
  }
}

0 个答案:

没有答案
相关问题