Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
158 views
in Technique[技术] by (71.8m points)

android - Finish the activity after receiving response from server

I want to finish the activity when received complete response from server in another java class.

Category.java

This is main activity

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";
                            }
    }

}

This handler is called when i recieved complete reponse from server.

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

             }
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

you can use your context reference for this

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...