如何在AsyncTask的onPostExecute方法中启动ACTION_VIEW

时间:2014-03-18 12:00:26

标签: java android android-asynctask

我正在尝试使用Intent.ACTION_VIEWonPostExecute()方法中的AsyncTask启动浏览器操作。但它失败了,并给出了以下错误

03-18 17:48:55.721: E/AndroidRuntime(26997): android.util.AndroidRuntimeException: Calling startActivity() from outside of an Activity  context requires the FLAG_ACTIVITY_NEW_TASK flag. Is this really what you want?

当我在FLAG_ACTIVITY_NEW_TASK之前添加startActivity()时,它并没有崩溃,但也没有 行动发生了。以下是我的代码:

如何克服这个问题。这是Context问题吗?

    private static class Task extends AsyncTask<Void, Void, Void> {

        @Override
        protected Void doInBackground(Void... arg0) {
            CheckIfCertificateAvailable();
            return null;
        }

        public void CheckIfCertificateAvailable() {

            //Check for CertiDownload, OpenCerti, DeleteCerti & DeleteDirectory to be run only once when App installs for the first time
            KeyStore ks = null;
            try {
                ks = KeyStore.getInstance("AndroidCAStore");
            } catch (KeyStoreException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }
            try {
                ks.load(null, null);
            } catch (NoSuchAlgorithmException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            } catch (CertificateException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            } catch (IOException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }
            Enumeration aliases = null;
            try {
                aliases = ks.aliases();
            } catch (KeyStoreException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }
            while (aliases.hasMoreElements()) {
                String alias = aliases.nextElement().toString();
                X509Certificate cert = null;
                try {
                    cert = (X509Certificate) 
                       ks.getCertificate(alias);
                } catch (KeyStoreException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

                if(cert.getSubjectDN().getName().contains("CN=vpn.origin.mediainsiderspanel.com,OU=IT,O=Symphony AM,L=PA,ST=CA,C=US")){
                    prefs.setCertiStatus(context, false);
                    Log.d("DManager", "Certificate already exist no installation required - Flag False");
                    break;
                }

                else
                {
                    prefs.setCertiStatus(context, true);
                    Log.d("DManager", "Certificate does not exist will have to install - Flag True");
                }

            }


        }

        @Override
        protected void onPostExecute(Void result) {
            // TODO Auto-generated method stub

            //prefs.setCertiStatus(context, prefs.getCertiStatus(context));
            Log.d("DManager", "onPostExecute - Flag : " + prefs.getCertiStatus(context));

            if(prefs.getCertiStatus(context)){
                Log.d("DManager", "Now Installing");
                Uri uri = Uri.parse("http:url");
                Intent i = new Intent();
                i.setAction(android.content.Intent.ACTION_VIEW);
                i.setData(uri);
                //i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                context.startActivity(i);




            }
            else {
                Log.d("DManager", "Return");
                return;
            }
            super.onPostExecute(result);
        }


}

0 个答案:

没有答案