【问题标题】:How to switch activities when an asynchronous connection returns?异步连接返回时如何切换活动?
【发布时间】:2011-11-18 18:51:41
【问题描述】:

无效的简单概念:

我正在尝试连接到网络并使用异步连接检索一些数据。当连接返回数据时,我想切换到另一个活动。我的代码不起作用。

我假设我需要使用某种回调,但 我是 Android / Java 新手,无法找到如何通过谷歌搜索来做到这一点。有人可以看看并建议我如何创建一个在数据返回时启动意图的回调吗?:

*更新:我在这里找到了这个不错的库 http://loopj.com/android-async-http/,这是在后台连接到网络的另一种(简单)方式。

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.mtmobtest"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk android:minSdkVersion="8" />
    <uses-permission android:name="android.permission.INTERNET" />

    <application
        android:icon="@drawable/icon"
        android:label="@string/app_name" >
        <activity
            android:label="@string/app_name"
            android:name=".MTMobTestActivity" >            
            <intent-filter >
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>


        <activity
            android:label="MainMenu"
            android:name=".MainMenu" >            
            <intent-filter >
                <action android:name="android.intent.action.MAINMENU" />
                <category android:name="android.intent.category.MAINMENU" />
            </intent-filter>
        </activity>

    </application>

</manifest>

MTMobTestActivity.java

package com.mtmobtest;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Window;
import android.widget.Toast;
import android.widget.ViewFlipper;
public class MTMobTestActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        requestWindowFeature(Window.FEATURE_NO_TITLE);

        postData("Sup yall");
        setContentView(R.layout.main);
    }

    public void postData(String toPost) {
        // Create a new HttpClient and Post Header
        HttpClient httpclient = new DefaultHttpClient();
        HttpPost httppost = new HttpPost("http://www.example.com/test.php");
        //This is the data to send
        String myName = "anybody there?"; //any data to send
        try {
        // Add your data
        List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1);
        nameValuePairs.add(new BasicNameValuePair("action", myName));
        httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
        // Execute HTTP Post Request
        ResponseHandler<String> responseHandler = new BasicResponseHandler();
        String response = httpclient.execute(httppost, responseHandler);
        //This is the response from a php application
        String reverseString = response;

        //ViewFlipper vf = (ViewFlipper) findViewById(R.layout.menu);
        // Set an animation from res/anim: I pick push left in
        //vf.setAnimation(AnimationUtils.loadAnimation(view.getContext(), R.anim.push_left_in));
        //vf.showNext();

        Toast.makeText(this, "response" + reverseString, Toast.LENGTH_LONG).show();

        } catch (ClientProtocolException e) {
        Toast.makeText(this, "CPE response " + e.toString(), Toast.LENGTH_LONG).show();
        // TODO Auto-generated catch block

        } catch (IOException e) {
        Toast.makeText(this, "IOE response " + e.toString(), Toast.LENGTH_LONG).show();
        // TODO Auto-generated catch block
        }

        Intent intent = new Intent(this, MainMenu.class);
        startActivity(intent);

        }
}

【问题讨论】:

    标签: java android xml asynchronous android-activity


    【解决方案1】:

    对作业使用AsyncTask,并在 AsyncTasks onPostExecute 方法调用中触发新的 Activity。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-12-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多