android application 的使用

参考http://oyeal.iteye.com/blog/941183

由于intent能够传送的对象类型非常有限  因此有些很多类都要用到的变量我们放在Application中  很像web中的session

使用自定义的Application  首先需要在Manifest中更改  在原有的<application 中添加这么一个属性

 android:name="MyStream"

这里的MyStream就是自己的Application的类名

 

MainActivity

package mypackage.hello;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class MainActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        initViews();
        connectToServer();
    }

    Button btnAdd;
    Button btnDel;
    Button btnSearch;

    Socket socket;
    BufferedReader in;
    PrintWriter out;
    MyStream myStream; //全局变量//就像是Web中的session你 application一样
    String recvStr = null;
    String sendStr = null;

    public void initViews() {
        btnAdd = (Button) findViewById(R.id.buttonAdd);
        btnDel = (Button) findViewById(R.id.buttonDel);
        btnSearch = (Button) findViewById(R.id.buttonSearch);

        btnAdd.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {

                Intent intent = new Intent();
                intent.setClass(MainActivity.this, AddActivity.class);  
                startActivity(intent);  
            }
        });
        


    }

    public void connectToServer() {
        try {
            // 同时 为了使程序拥有网络权限 在manifest中要有 <uses-permission
            // android:name="android.permission.INTERNET" />
            // 否则提示 permission denied
            // 安卓模拟器将127.0.0.1默认为模拟器的本机地址 要得到计算机中真正的地址是 localhost/127.0.0.1 或者
            // 10.0.0.2
            socket = new Socket("10.0.2.2", 8081);
            System.out.println("after socket");
            in = new BufferedReader(new InputStreamReader(
                    socket.getInputStream()));
            out = new PrintWriter(socket.getOutputStream(), true);
            // 这里是给Application中的对象赋值
            myStream=(MyStream)this.getApplication();
            myStream.setIn(in);
            myStream.setOut(out);
            
            BufferedReader line = new BufferedReader(new InputStreamReader(
                    System.in));
        } catch (UnknownHostException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

}

AddActivity

package mypackage.hello;

import java.io.IOException;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class AddActivity extends Activity {
  //.....
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.add);
        initViews();
    }

    void initViews() {

        subBtn = (Button) findViewById(R.id.submit);
        subBtn.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {
                MyStream myStream = (MyStream) getApplication();
                
                //发送信息
                try {
                    System.out.println("add clicked");
                    sendStr = "add,xxx,xxx,xxx";
            //从Application的对象中取值 myStream.getOut().println(sendStr); recvStr
= myStream.getIn().readLine(); System.out.println("response is " + recvStr); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }); } }

 

 

 

posted @ 2013-10-23 23:20  cart55free99  阅读(242)  评论(0编辑  收藏  举报