Intent传参

xml 

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >


    <TextView
        android:id="@+id/textview"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="hello"
         />


</LinearLayout>


public class IntentActivity extends FragmentActivity {

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

this.findViewById(R.id.textview).setOnClickListener(new OnClickListener(){
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub

 /*   Intent intent =new Intent(IntentActivity.this,WebViewActivity.class);
    intent.putExtra("url", "http://wap.baidu.com");
IntentActivity.this.startActivity(intent);*/


   Intent intent =new Intent();
   intent.setClass(IntentActivity.this,WebViewActivity.class);
   intent.putExtra("extra", "这是页面一传来的值!");
   intent.putExtra("url", "http://wap.baidu.com");
IntentActivity.this.startActivity(intent);

}
});

}


}

@SuppressWarnings("deprecation")
public class MainToActivity extends Activity {

// 定义消息
private static final int NUMBER_FORMAT_ERROR = 0x0000;
private static final int QUERY_SUCCESS_MSG = 0x0001;
private static final int PHONE_MSG = 0x0002;


private String phoneSec;

private EditText phoneSecEditText;
private TextView resultView;
private Button queryButton;
private Thread thread;

private TextView mobileView;
private TextView yunyingView;
private TextView addressView;



private Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
switch (msg.what) {
case NUMBER_FORMAT_ERROR:
phoneSecEditText.setText("");
resultView.setText("您输入的号码格式有误");
break;
case QUERY_SUCCESS_MSG:
resultView.setText(msg.obj.toString());
break;
case PHONE_MSG:{
Object object = msg.obj;
if(object!=null){
PhoneBean phoneBean = (PhoneBean)object;
mobileView.setText("手机号码:"+phoneBean.telString);
yunyingView.setText("运营商:"+phoneBean.areaVid);
addressView.setText("地址:"+phoneBean.province+" - "+phoneBean.mts);

}


}break;
default:
break;
}
}
};

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.shjihaoma);

phoneSecEditText = (EditText) findViewById(R.id.phone_sec);

resultView = (TextView) findViewById(R.id.result_text);
mobileView = (TextView) findViewById(R.id.mobile);
yunyingView = (TextView) findViewById(R.id.yunying);
addressView = (TextView) findViewById(R.id.address);

queryButton = (Button) findViewById(R.id.query_btn);
queryButton.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub

phoneSec = phoneSecEditText.getText().toString().trim();

if ("".equals(phoneSec) || phoneSec.length() < 7) {
// 发送消息 显示查询结果的TextView清空
handler.sendEmptyMessage(NUMBER_FORMAT_ERROR);
return;

}else{

new Thread(){
public void run(){
getRemoteInfo(phoneSec);
}
}.start();
}

}
});


}



/**
* 手机号段归属地查询

* @param phoneSec
*            手机号段
*/
private void getRemoteInfo(String phoneSec) {
try {

List<NameValuePair> params = new LinkedList<NameValuePair>();
params.add(new BasicNameValuePair("tel", phoneSec));
params.add(new BasicNameValuePair("qq-pf-to", "pcqq.c2c"));
String param = URLEncodedUtils.format(params, "UTF-8");

String requestUrl = "https://tcc.taobao.com/cc/json/mobile_tel_segment.htm";
requestUrl =requestUrl+"?"+param;

HttpClient client = new DefaultHttpClient();
HttpGet httpGet = new HttpGet(requestUrl);
HttpResponse response = client.execute(httpGet);


if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {

String result = EntityUtils.toString(response.getEntity());
int startIndex = result.indexOf("{") - 1;
String result1 = result.substring(startIndex);

paseJson(result1);

sendMessage(QUERY_SUCCESS_MSG,result1);

}
} catch (Exception e) {
e.printStackTrace();
}
}


private void sendMessage(int what,Object msgBody){
if(handler!=null){
Message msg = new Message();
msg.what = what;
msg.obj = msgBody;
handler.sendMessage(msg);
}
}


private void paseJson(String json) throws JSONException {
// TODO Auto-generated method stub
if (json == null) {
return;
}
JSONObject Zhaojson = new JSONObject(json);
if (Zhaojson != null) {

PhoneBean phoneBean = new PhoneBean();

if (!Zhaojson.isNull("mts")) {
String mts = Zhaojson.optString("mts");
System.out.println(mts);
phoneBean.mts = mts;
}

if (!Zhaojson.isNull("province")) {

String province = Zhaojson.optString("province");
System.out.println(province);
phoneBean.province = province;
}
if (!Zhaojson.isNull("telString")) {

String telString = Zhaojson.optString("telString");
System.out.println(telString);
phoneBean.telString = telString;

}
if (!Zhaojson.isNull("areaVid")) {

String areaVid = Zhaojson.optString("areaVid");
System.out.println(areaVid);
phoneBean.areaVid = areaVid;
}

if (!Zhaojson.isNull("ispVid")) {

String ispVid = Zhaojson.optString("ispVid");
System.out.println(ispVid);
phoneBean.ispVid = ispVid;
}

if (!Zhaojson.isNull("carrier")) {
String carrier = Zhaojson.optString("carrier");
System.out.println(carrier);
phoneBean.carrier = carrier;
}

sendMessage(PHONE_MSG,phoneBean);
}
}

private class PhoneBean{
public String mts;
public String carrier;
public String ispVid;
public String areaVid;
public String telString;
public String province;
}


}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值