从服务器收到响应后完成活动

时间:2012-11-24 16:29:59

标签: android android-activity

我想在收到另一个java类服务器的完整响应时完成活动。

Category.java

这是主要活动

public void onListItemClick(ListView parent, View v, int position, long id)
    {
        // Get the selected category id & name.
        String catId = "";
        String catName = "";
        LinearLayout ll = (LinearLayout)findViewById(R.id.linearLayout1);
        View view = ll.getChildAt(0);
        if(view instanceof ListView) {
            ListView lView = (ListView) view;
            RowData rowData = (RowData) lView.getAdapter().getItem(position);
            catId = rowData.mCatId;
            catName = rowData.mTitle;
        }

        String url = "http://global.thinlenses.co.uk/virtualMirror/productlisting.php"; 
        String xml = "<?xml version=\"1.0\" encoding=\"utf-8\" ?>" + "<category><Id>" + catId + "</Id>"
                    + "<Data>" + "pList" + "</Data></category>";

        if(getIntent().getBooleanExtra("VM", false))
            new SpinnerHelper().serverCall(Category.this, "ProductListVM", url, xml, catName);
        else
            new SpinnerHelper().serverCall(Category.this, "ProductList", url, xml, catName);
    }

SpinnerHelper.java Regular Class not Activity

void serverCall(Context context, final String target, final String url, final String xml, String extraValue)
    {
        try {
            this.context = context;
            this.target = target;
            this.extraValue = extraValue;

            ConnectivityManager conMan = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
            if(conMan.getActiveNetworkInfo() != null && conMan.getActiveNetworkInfo().isConnected())
            {
                dialog = new ProgressDialog(context);
                dialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
                dialog.setCancelable(true);
                dialog.setTitle(R.string.app_name);
                dialog.setMessage(context.getString(R.string.progressing));
                dialog.show();

                // Create a thread for updating the spinner.
                Thread progressThread = new Thread(new Runnable() {
                    public void run() {
                        try {
                            while(!isExecuted) {
                                Thread.sleep(1000);                                 // Wait 1000ms between each update.
                                handler.sendMessage(handler.obtainMessage());       // Active the update handler.
                            }
                        }
                        catch (InterruptedException e) {
                            Log.e("VM", "Exception while spinner is updating at: " + e.getMessage());
                        }
                    }
                });
                progressThread.start();

                // Create a thread to get response from server.
                Thread registerThread = new Thread(new Runnable() {
                    public void run()
                    {
                        // Get response XML from server and parse it.
                        String resXML = new Connection().getResponse(url, xml);
                        XMLParser parser = new XMLParser();
                        Document doc = parser.getDomElement(resXML);
                        if(doc != null)
                        {
                            if(target.equals("Category")) {
                                NodeList nodeList = doc.getElementsByTagName("Category");
                                name = new String[nodeList.getLength()];
                                id = new String[nodeList.getLength()];

                                // Fetch all node values and store into arrays.
                                for(int index = 0; index < nodeList.getLength(); index++)
                                {
                                    Element element = (Element) nodeList.item(index);
                                    id[index] = parser.getValue(element, "id");
                                    name[index] = parser.getValue(element,"CategoryName");
                                }
                                result = "yes";
                            }
    }

}

当我从服务器收到完整的响应时,会调用此处理程序。

  Handler handler = new Handler()
  {
    public void handleMessage(Message message)
    {
        if(isExecuted)
        {
            dialog.dismiss();
            isExecuted = false;

            if(result.equals("yes"))
            {
                  /////HERE i want to start the next activity and finish the Category(previous activity).

             }

1 个答案:

答案 0 :(得分:1)

您可以使用您的上下文参考

Intent intent = new Intent(context,nextActivity.class);
context.startActivity(intent);
((Activity) context).finish();