我正在製作一個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提前。
嘗試移動installapk();在onPostExecute()行。 – 2014-10-10 11:23:14
嗨,即使我這樣做,問題在於我開始的意圖。我想在Activitry完成後執行操作。 – megha 2014-10-10 11:30:19
什麼是例外? – ToYonos 2014-10-10 13:16:50