2014-10-10 29 views
0

我正在製作一個phonegap項目。我爲它製作了一個android插件。其中,AsyncTask也被調用。其中,我有context.startActivity(intent)函數。我想在完成此活動後執行任務。我找到了一個使用context.startActivityForResult(intent,i)的解決方案。但是這個功能是不可識別的。startActivityForResult在AsyncTask中未被識別爲函數

然後我嘗試,每

StartActivityForResult not working in AsyncTask

但在startActivityForResult時間

((Activity) context).startActivityForResult(intent,1); 

的,我得到的文件安裝錯誤。

我的代碼如下 -

public class MyPlugin extends CordovaPlugin 
    { 
     Context context; 
     CallbackContext callback; 
     @Override 
     public void initialize(CordovaInterface cordova, CordovaWebView webView) 
     { 
      super.initialize(cordova, webView);  
     } 
     @Override 
     public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException 
     { 
       context=this.cordova.getActivity().getApplicationContext(); 
       callback=callbackContext; 

       if (action.equals("plugin1")) 
       { 
        System.out.println("Inside plugin1"); 
        String myurl = args.getString(0); 
        if(haveNetworkConnection()==true) 
        { 
         System.out.println("Network connected"); 
         new DownloadManager().execute(myurl); 
         return true; 
        } 
        else 
        { 
         System.out.println("Network not connected"); 
         callbackContext.error("Internet connection is not available."); 
         return false; 
        } 
       } 

       callbackContext.error("Some error has occured.Please try later."); 
       return false; 

     } 

     public class DownloadManager extends AsyncTask<String, String, String> 
     { 
      @Override 
      public String doInBackground(String... arg0) 
      { 
       try 
       { 
        //Knowing imei number 
        TelephonyManager mngr = (TelephonyManager)context.getSystemService(context.TELEPHONY_SERVICE); 
        String imei_device=mngr.getDeviceId(); 
        //downloading and installing app 
        downloadapk(arg0[0]); 
        installapk(); 
        PluginResult result = new PluginResult(PluginResult.Status.OK, "success"); 
        result.setKeepCallback(true); 
        callback.success("Operation completed!!"); 
        return null; 
       } 
       catch(Exception e) 
       { 
        callback.error("Some problem occured.Try again later"); 
        return null; 
       } 
      } 
     } 
     public void installapk() 
     { 
      try 
      { 
       Intent intent=new Intent(Intent.ACTION_VIEW); 
       intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
       intent.setDataAndType(
       Uri.fromFile(new File(Environment.getExternalStorageDirectory()+"/"+fileName)),"application/vnd.android.package-archive"); 
       ((Activity) context).startActivityForResult(intent,1); 
      } 
      catch (Exception e) 
      { 
       Log.e("File installing Error", e.getMessage()); 
       System.out.println(e.getMessage()); 
       callback.error("Installation Failed.Please try later"); 
      } 
     } 
     @Override 
     public void onActivityResult(int requestCode, int resultCode, Intent data) { 

       if (requestCode == 1) //check if the request code is the one you've sent 
       { 
        if (resultCode == Activity.RESULT_OK) 
        { 
         System.out.println("Result is OK"); 
         } 
        else 
        { 
         System.out.println("Result is not OK");  
         } 
       } 


      super.onActivityResult(requestCode, resultCode, data); 

     } 

Thanx提前。

+0

嘗試移動installapk();在onPostExecute()行。 – 2014-10-10 11:23:14

+0

嗨,即使我這樣做,問題在於我開始的意圖。我想在Activitry完成後執行操作。 – megha 2014-10-10 11:30:19

+0

什麼是例外? – ToYonos 2014-10-10 13:16:50

回答

0

是該錯誤的ClassCastException

如果是這樣,這是因爲你與應用程序的上下文,這也是一個Context但不是一個Activity,並且因此不能被鑄造以獲得startActivityForResult方法替換Activity(這是一個Context)。

context = this.cordova.getActivity().getApplicationContext(); 

有:

context = this.cordova.getActivity(); 

請問,如果你更換工作?

Here是關於不同類型的上下文以及何時使用它們的解釋。

+0

謝謝先生。它現在有效。 :)我無法掌握這一分鐘的錯誤。但現在,我正面臨另一個問題.. onActivityResult()沒有被解僱。 – megha 2014-10-10 12:14:43

+0

我不知道包管理器是否以及何時發回代碼。偵聽ACTION_PACKAGE_ADDED/ACTION_PACKAGE_CHANGED廣播可能更健壯:https://developer.android.com/reference/android/content/Intent.html#ACTION_PACKAGE_ADDED。 – bwt 2014-10-10 12:37:17

0

試試這種方式,希望這會幫助你解決你的問題。

public class MyPlugin extends CordovaPlugin { 
    Context context; 
    CallbackContext callback; 

    @Override 
    public void initialize(CordovaInterface cordova, CordovaWebView webView) { 
     super.initialize(cordova, webView); 
    } 

    @Override 
    public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException { 
     context = this.cordova.getActivity().getApplicationContext(); 
     callback = callbackContext; 

     if (action.equals("plugin1")) { 
      System.out.println("Inside plugin1"); 
      String myurl = args.getString(0); 
      if (haveNetworkConnection() == true) { 
       System.out.println("Network connected"); 
       new DownloadManager().execute(myurl); 
       return true; 
      } else { 
       System.out.println("Network not connected"); 
       callbackContext.error("Internet connection is not available."); 
       return false; 
      } 
     } 

     callbackContext.error("Some error has occured.Please try later."); 
     return false; 

    } 

    public class DownloadManager extends AsyncTask<String, String, String> { 
     @Override 
     public String doInBackground(String... arg0) { 
      try { 
       //Knowing imei number 
       TelephonyManager mngr = (TelephonyManager) context.getSystemService(context.TELEPHONY_SERVICE); 
       String imei_device = mngr.getDeviceId(); 
       //downloading and installing app 
       downloadapk(arg0[0]); 
       PluginResult result = new PluginResult(PluginResult.Status.OK, "success"); 
       result.setKeepCallback(true); 
       callback.success("Operation completed!!"); 
       return null; 
      } catch (Exception e) { 
       callback.error("Some problem occured.Try again later"); 
       return null; 
      } 
     } 

     @Override 
     protected void onPostExecute(String s) { 
      super.onPostExecute(s); 
      installapk(); 
     } 
    } 

    public void installapk() { 
     try { 
      Intent intent = new Intent(Intent.ACTION_VIEW); 
      intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
      intent.setDataAndType(
        Uri.fromFile(new File(Environment.getExternalStorageDirectory() + "/" + fileName)), "application/vnd.android.package-archive"); 
      ((Activity) context).startActivityForResult(intent, 1); 
     } catch (Exception e) { 
      Log.e("File installing Error", e.getMessage()); 
      System.out.println(e.getMessage()); 
      callback.error("Installation Failed.Please try later"); 
     } 
    } 

    @Override 
    public void onActivityResult(int requestCode, int resultCode, Intent data) { 

     if (requestCode == 1) //check if the request code is the one you've sent 
     { 
      if (resultCode == Activity.RESULT_OK) { 
       System.out.println("Result is OK"); 
      } else { 
       System.out.println("Result is not OK"); 
      } 
     } 


     super.onActivityResult(requestCode, resultCode, data); 

    } 
} 
+0

嗨,我也試過這樣做。我仍然收到同樣的錯誤。 – megha 2014-10-10 11:57:41