Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/206.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android startActivityForResult未被识别为AsyncTask中的函数_Android_Cordova_Android Asynctask - Fatal编程技术网

Android startActivityForResult未被识别为AsyncTask中的函数

Android startActivityForResult未被识别为AsyncTask中的函数,android,cordova,android-asynctask,Android,Cordova,Android Asynctask,我正在做一个phonegap项目。我已经为它制作了一个android本地插件。在其中,AsyncTask也被调用。在其中,我有context.startActivity(intent)函数。我想在此活动完成后执行一项任务。我找到了一个使用context.startActivityForResult(intent,I)的解决方案。但是这个功能是不可识别的 然后我试着 ((Activity) context).startActivityForResult(intent,1); 依照 但在启动策略

我正在做一个phonegap项目。我已经为它制作了一个android本地插件。在其中,AsyncTask也被调用。在其中,我有
context.startActivity(intent)
函数。我想在此活动完成后执行一项任务。我找到了一个使用
context.startActivityForResult(intent,I)
的解决方案。但是这个功能是不可识别的

然后我试着

((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);

        }
公共类MyPlugin扩展了CordovaPlugin
{
语境;
回调上下文回调;
@凌驾
公共无效初始化(cordova接口cordova、CordovaWebView webView)
{
super.initialize(cordova、webView);
}
@凌驾
公共布尔执行(字符串操作、JSONArray参数、CallbackContext CallbackContext)抛出JSONException
{   
context=this.cordova.getActivity().getApplicationContext();
callback=callbackContext;
if(action.equals(“plugin1”))
{ 
System.out.println(“内部插件1”);
字符串myurl=args.getString(0);
if(haveNetworkConnection()==true)
{
System.out.println(“网络连接”);
新建DownloadManager().execute(myurl);
返回true;
}
其他的
{
System.out.println(“网络未连接”);
callbackContext.error(“Internet连接不可用”);
返回false;
}
}
callbackContext.error(“发生了一些错误,请稍后再试。”);
返回false;
}
公共类DownloadManager扩展异步任务
{
@凌驾
公共字符串doInBackground(字符串…arg0)
{
尝试
{
//知道imei号码
TelephonyManager mngr=(TelephonyManager)context.getSystemService(context.TELEPHONY_服务);
字符串imei_device=mngr.getDeviceId();
//下载和安装应用程序
下载apk(arg0[0]);
installapk();
PluginResult结果=新的PluginResult(PluginResult.Status.OK,“success”);
result.setKeepCallback(true);
callback.success(“操作已完成!!”);
返回null;
}
捕获(例外e)
{
callback.error(“出现了一些问题,请稍后再试”);
返回null;
}   
}
}
public void installapk()
{
尝试
{ 
意向意向=新意向(意向.行动\视图);
intent.addFlags(intent.FLAG\u活动\u新任务);
intent.setDataAndType(
fromFile(新文件(Environment.getExternalStorageDirectory()+“/”+fileName)),“application/vnd.android.package archive”);
((活动)上下文)。startActivityForResult(意图,1);
}
捕获(例外e)
{
Log.e(“文件安装错误”,e.getMessage());
System.out.println(e.getMessage());
callback.error(“安装失败,请稍后再试”);
}
}
@凌驾
ActivityResult上的公共void(int请求代码、int结果代码、意图数据){
if(requestCode==1)//检查请求代码是否是您发送的代码
{
if(resultCode==Activity.RESULT\u确定)
{
System.out.println(“结果正常”);
}
其他的
{
System.out.println(“结果不正常”);
}
}
super.onActivityResult(请求代码、结果代码、数据);
}

提前使用Thanx。

试试这种方法,希望这能帮助您解决问题。

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);

    }
}
公共类MyPlugin扩展了CordovaPlugin{
语境;
回调上下文回调;
@凌驾
公共无效初始化(cordova接口cordova、CordovaWebView webView){
super.initialize(cordova、webView);
}
@凌驾
公共布尔执行(字符串操作、JSONArray参数、CallbackContext CallbackContext)抛出JSONException{
context=this.cordova.getActivity().getApplicationContext();
callback=callbackContext;
if(action.equals(“plugin1”)){
System.out.println(“内部插件1”);
字符串myurl=args.getString(0);
if(haveNetworkConnection()==true){
System.out.println(“网络连接”);
新建DownloadManager().execute(myurl);
返回true;
}否则{
System.out.println(“网络未连接”);
callbackContext.error(“Internet连接不可用”);
返回false;
}
}
callbackContext.error(“发生了一些错误,请稍后再试。”);
返回false;
}
公共类DownloadManager扩展异步任务{
@凌驾
公共字符串doInBackground(字符串…arg0){
试一试{
//知道imei号码
TelephonyManager mngr=(TelephonyManager)context.getSystemService(context.TELEPHONY_服务);
字符串imei_device=mngr.getDeviceId();
//下载和安装
context = this.cordova.getActivity().getApplicationContext();
context = this.cordova.getActivity();