关于动态更新 安卓 TExtview的 代码

来源:互联网 发布:unity3d 室内漫游 编辑:程序博客网 时间:2024/05/29 13:23
package com.example.newtest;

import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.widget.TextView;

public class addressActivity extends Activity{
 
 private TextView address_location;

 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.address_activity);
  address_location = (TextView)findViewById(R.id.address_now);
  
  address_location.setText("定位中...");
   new Thread(){
          public void run(){
            HttpClientUtil httpClient = new HttpClientUtil();
           
      
      String getjsonstring = httpClient.getJsonContent("http://ip.taobao.com/service/getIpInfo2.php?ip=myip").toString();
      
      try {
       final JSONObject obj = new JSONObject(getjsonstring);
           final String token = obj.getString("data");
       
       try {
         final JSONObject obj2 = new JSONObject(token);
         String token1 = obj2.getString("region");
         String token2 = obj2.getString("city");
         
            SharedPreferences settings = getSharedPreferences("persondata",MODE_PRIVATE);
       SharedPreferences.Editor editor = settings.edit();
       Message message = new Message();
       message.what = 1;
       Bundle bundle = new Bundle();  
       bundle.putString("region",token1);  //往Bundle中存放数据 
       bundle.putString("city",token2);  //往Bundle中存放数据 
       message.setData(bundle);//mes利用Bundle传递数据  
       handler.sendMessage(message);
       editor.putString("region",token1);
       editor.putString("city",token2);
       editor.commit();
        } catch (JSONException e) {
         // TODO Auto-generated catch block
         e.printStackTrace();
        }
      } catch (JSONException e) {
       // TODO Auto-generated catch block
       e.printStackTrace();
      }  
      
          }
         }.start();        
      
       
      
      
   
 }
 private Handler handler = new Handler(){
  public void handleMessage(Message msg){
   String str1 = msg.getData().getString("region");//接受msg传递过来的参数 
   String str2 = msg.getData().getString("city");//接受msg传递过来的参数
   address_location = (TextView)findViewById(R.id.address_now);
   
   address_location.setText(str1+" "+ str2);

  } 
 };
}
0 0
原创粉丝点击