2012-12-12 131 views
1

我需要將各種企業應用程序部署和更新到有限數量的用戶的Android設備。自動安裝Android應用程序

這些應用程序不應該在Google Play上發佈,但必須通過單獨的渠道進行分發。

我需要做的是一個「企業包管理器」應用程序,用於自動檢查新的應用程序/更新,並自動觸發安裝新的或更新的APK,而無需首先詢問用戶的同意。

我知道,根據設計,Android不允許第三方應用程序與安裝的應用程序進行交互。我也知道紮根的手機沒有這個問題,因爲您可以將任何APK注入設備。

  • 如果我做飯用「的企業包管理器」的ROM(甚至基於CWM)安裝爲系統應用,但沒有su二進制(它仍然是一個企業的電話......),將這個程序能自動安裝新應用程序?
  • 我該如何安裝應用程序而不要求同意?我的意思是,我需要一個基本的代碼示例和權限,如果需要的話
  • 無論如何,系統應用程序是否以root用戶身份運行?我記得如此

回答

2

如果你想檢查你的服務器上的某個地方的應用程序,你必須每24小時檢查一次更新,如果有任何更新可用,那麼它將導航到異步任務其中,更新後的版本版本將得到安裝

public void checkforUpdate() { 

     /* Get Last Update Time from Preferences */ 
     SharedPreferences prefs = getPreferences(0); 
     lastUpdateTime = prefs.getLong("lastUpdateTime", 0); 
     if ((lastUpdateTime + CommonString.timeCheckDuration) < System.currentTimeMillis() && System.currentTimeMillis()>lastUpdateTime) { 
      // Asynch task 
      new VersionCheckTask().execute(); 
     } 
     else{ 
      // do nothing 
     } 
    } 

現在將導航到:

private class VersionCheckTask extends AsyncTask<Void, Void, Void> { 
     ProgressDialog progressDialog; 
     @Override 
     protected void onPreExecute() { 
      super.onPreExecute(); 
      try { 
       progressDialog = new ProgressDialog(Login.this, android.R.style.Theme_Holo_Light_Dialog); 
       //progressDialog.setTitle("AppName"); 
       progressDialog.setMessage("Checking for updates..."); 
       progressDialog.setCancelable(false); 
       progressDialog.setIndeterminate(true); 
       progressDialog.show(); 
      } catch (Exception e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 
     } 
     protected Void doInBackground(Void... params) { 
      /** 
      * Simulates a background job. 
      */ 
      try { 
       Thread.sleep(1000); 
      } catch (InterruptedException e) { 
      } 

      HashMap<String, String> map = new HashMap<String, String>(); 
      map.put("build",CommonString.buildVersion); 
      map.put("en", CommonString.en); 

      responce = CommonFunction.PostRequest("updateCheck", map); 
      return null; 
     } 

     @Override 
     protected void onPostExecute(Void result) { 
      if (progressDialog != null && progressDialog.isShowing()) 
       progressDialog.dismiss(); 
      if(!CommonFunction.isNetworkAvailable()){ 
       Toast.makeText(ClaimColonyApplication.getAppContext(), CommonString.NO_NETWORK, Toast.LENGTH_SHORT).show(); 
       return; 
      } 
      ParseUpdateResponse(responce); 
      if(rCodeUpdate == 100 && ApkLink.length() >0){ 
       new AlertDialog.Builder(Login.this,android.R.style.Theme_Holo_Light_Dialog) 
       .setIcon(R.drawable.ic_launcher) 
       .setTitle("Update Available") 
       .setMessage(""+UpdateMessage) 
       .setPositiveButton("Yes", new DialogInterface.OnClickListener() { 
        public void onClick(DialogInterface dialog, int whichButton) { 
         //User clicked OK so do some stuff 
         new VersionCheckTaskDialog().execute(); 
        } 
       }) 
       .setNegativeButton("No", new DialogInterface.OnClickListener() { 
        public void onClick(DialogInterface dialog, int whichButton) { 
         //User clicked Cancel 
         finish(); 
        } 
       }) 
       .show(); 
      }else{ 
       if(rCodeUpdate == 100){ 
        lastUpdateTime = System.currentTimeMillis(); 
        SharedPreferences.Editor editor = getPreferences(0).edit(); 
        editor.putLong("lastUpdateTime", lastUpdateTime); 

        editor.commit(); 
       } 
      } 
      super.onPostExecute(result); 
     } 
    } 

    private class VersionCheckTaskDialog extends AsyncTask<Void, Void, Void> { 
     ProgressDialog progressDialogUpdate; 
     @Override 
     protected void onPreExecute() { 
      super.onPreExecute(); 
      try { 
       progressDialogUpdate = new ProgressDialog(Login.this, android.R.style.Theme_Holo_Light_Dialog); 
       //progressDialog.setTitle("AppName"); 
       progressDialogUpdate.setMessage("Fetching updates..."); 
       progressDialogUpdate.setCancelable(false); 
       progressDialogUpdate.setIndeterminate(true); 
       progressDialogUpdate.show(); 
      } catch (Exception e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 
     } 
     protected Void doInBackground(Void... params) { 
      /** 
      * Simulates a background job. 
      */ 
      try { 
       Thread.sleep(1000); 
      } catch (InterruptedException e) { 
      } 

      String extStorageDirectory = Environment.getExternalStorageDirectory().toString(); 
      File folder = new File(extStorageDirectory, "pdf"); 
      folder.mkdir(); 
      File file = new File(folder, "AppName."+"apk"); 
      try { 
        file.createNewFile(); 
      } catch (IOException e1) { 
        e1.printStackTrace(); 
      } 
      /** 
      * replace url to ApkLink 
      */ 
      //DownloadFile(ApkLink, file); 
      DownloadFile("URL", file); 

      return null; 
     } 

     @Override 
     protected void onPostExecute(Void result) { 
      if (progressDialogUpdate != null && progressDialogUpdate.isShowing()) 
       progressDialogUpdate.dismiss(); 
      if(!CommonFunction.isNetworkAvailable()){ 
       Toast.makeText(ClaimColonyApplication.getAppContext(), CommonString.NO_NETWORK, Toast.LENGTH_SHORT).show(); 
       return; 
      } 
      try { 
       Intent intent = new Intent(Intent.ACTION_VIEW); 
       intent.setDataAndType(Uri.fromFile(new File(Environment.getExternalStorageDirectory() + "/pdf/" + "AppName.apk")), "application/vnd.android.package-archive"); 
       startActivity(intent); 
       lastUpdateTime = System.currentTimeMillis(); 
       SharedPreferences.Editor editor = getPreferences(0).edit(); 
       editor.putLong("lastUpdateTime", lastUpdateTime); 

       editor.commit(); 
      } catch (Exception e) { 
       // TODO Auto-generated catch block 
       System.out.println("Exception in start intent for launch app-------: "+e.toString()); 
       e.printStackTrace(); 
      } 
      super.onPostExecute(result); 
     } 
    } 

我在24小時一次檢查更新,如果有可用,那麼它會顯示彈出任何更新給你升級您的應用程序,否則會將您上次的檢查時間保存在首選項中 現在,這將允許您更新和安裝您的應用程序,這將檢查24小時後的下一次更新,您可能需要根據條件來檢查更新。請更改.apk文件和URL的名稱。

您將需要以下權限:

<uses-permission android:name="android.permission.INTERNET"/> 
    <uses-permission android:name="android.permission.READ_PHONE_STATE"/> 
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> 
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> 

好運。

+0

感謝您的代碼。這可能不是我真正想問的。我的意思是...查詢應用程序更新是非常簡單的任務。 VersionCheckTaskDialog的onPostExecute是最有趣的部分。我需要做的是觸發**未經監督的安裝。你的代碼*應該*要求用戶同意,但由於我沒有測試過它,因爲它應該是一個「紙上」概念證明我想問你是否從系統應用程序觸發ACTION_VIEW意圖可以觸發未被監督的安裝。謝謝 –

+1

我也這樣做,但我在這裏迫使用戶更新他的版本,直到他不會更新他將無法登錄應用程序,當有任何更新可用在我的服務器上時,我會得到我的一些網址新建和我顯示彈出,如果用戶允許更新將自動下載應用程序apk在設備中,並要求安裝,安裝後用戶將不得不再次啓動應用程序, – Puneet

+0

+1是一種可行的方式來執行最新版本規則 –