繁体   English   中英

在新线程中从类开始活动

[英]Start Activity from Class in new Thread

基本上,我的想法是检查Web会话是否仍然有效,如果不是,我将启动自动登录用户的主要活动。

我有这个工作,不确定是否是最好的方法。 如果有人有更好的方法,请告诉我。

谢谢

用途:

    @Override
    public void onResume() {
         super.onResume();
         new LoginCheck(this,new Intent(this,MyActivity.class));

    }

班级

 public class LoginCheck extends Application {


Intent home;
Activity activity;

public LoginCheck(Activity activity, Intent home) {
    this.activity = activity;
    this.home = home;
    new Check().execute();
}

public class Check extends AsyncTask {

    @Override
    protected Object doInBackground(Object... objects) {
        try {
            InputStream is = null;
            String result = "";
            JSONObject jArray = null;
            PersistentCookieStore myCookieStore = new PersistentCookieStore(MyApp.getAppContext());
            //http post
            DefaultHttpClient mClient = AppSettings.getClient();
            try {
                HttpPost request = new HttpPost(MyApp.getServiceUrl() + "/Api/Login/AmILoggedIn");
                mClient.setCookieStore(myCookieStore);
                HttpResponse response = mClient.execute(request);
                HttpEntity entity = response.getEntity();
                is = entity.getContent();
            } catch (Exception e) {
                Log.e("log_tag", "Error in http connection " + e.toString());
            }

            //convert response to string
            try {

                BufferedReader reader = new BufferedReader(new InputStreamReader(is, "iso-8859-1"), 8);
                StringBuilder sb = new StringBuilder();
                String line = null;
                while ((line = reader.readLine()) != null) {
                    sb.append(line + "\n");
                }
                is.close();
                result = sb.toString();
            } catch (Exception e) {
                Log.e("log_tag", "Error converting result " + e.toString());
            }
            final String r = result;
            final Intent i = home;
            activity.runOnUiThread(new Runnable() {

                @Override
                public void run() {

                    try {

                        JSONObject j = new JSONObject(r);
                        if (!j.getBoolean("Success")) {
                            try {
                                activity.startActivity(i);
                            } catch (Exception e) {
                                Log.e("log_tag", e.getMessage());
                            }
                        }


                    } catch (JSONException e) {

                        Log.e("log_tag", "Error parsing data " + e.toString());
                    }
                }
            });

            return true;

        } catch (Exception e) {

        }

        return null;
    }
}

}

这是清理版本

采用:

new LoginCheck() {
        @Override
        public void LoggedIn() {
            //To change body of implemented methods use File | Settings | File Templates.
        }

        @Override
        public void LoggedOut() {
            //To change body of implemented methods use File | Settings | File Templates.
        }
    };

public abstract class LoginCheck {
public LoginCheck(){
    new Check().execute(true);
}
public  class Check extends AsyncTask<Boolean,Boolean,Boolean>{
 @Override
 protected Boolean doInBackground(Boolean... objects) {

     JSONStringer jsonSend = null;

     try {
         jsonSend = new JSONStringer()
                 .object()
                 .endObject();
     } catch (JSONException e) {
         Log.e("log_tag", "Error creating Json " + e.toString());
     }

     JSONObject result = JsonPost.postJSONtoURL(MyApp.getServiceUrl() + "/Api/Login/AmILoggedIn", jsonSend);

     try {
         if (result.getBoolean("Success")) {
             return true;
         }
     } catch (JSONException e) {
         Log.e("log_tag", "Error parsing data " + e.toString());
     }
     try {
         if (!result.getBoolean("Success")) {
             return false;
         }
     } catch (JSONException e) {
         Log.e("log_tag", "Error parsing data " + e.toString());
     }

     return false;
 }



 @Override
 protected void onPostExecute(Boolean result){
     if(result)
         LoggedIn();
     if(!result)
         LoggedOut();

 }
  }




public abstract void LoggedIn();
public abstract void LoggedOut();

 }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM