Android线程暂停

时间:2014-05-02 06:29:50

标签: android android-activity

android项目在主活动中有两个线程,在第二个活动中有一个线程。按钮单击必须将控件移动到第二个活动并暂停第一个活动,并在第二个活动关闭后恢复暂停的线程。 怎么办呢?

代码:

     public class MainActivity extends Activity implements OnItemSelectedListener{
//creating two sockets . one to connect to the server running on linino
//and the other for connecting to the computer to play music
Thread t1;
Thread t2;
//initialising socket variables
private Socket socket;
private Socket socket2;
//write the local ip of linino assigned by the dhcp
//make sure that the port number is the same used for the server 
//program
private static final int SERVERPORT = 8020;
private static final String SERVER_IP = "192.168.0.104";
//write the ip of the computer at which the music player is runnnigd

private static final int SERVERPORT2 = 8036;
private static final String SERVER_IP2 = "192.168.0.100";
private Spinner spinner;
  private static final String[]paths = {"Select","Happy", "Sad", "Party"};
  //voice app 
  protected static final int RESULT_SPEECH = 1;

  private ImageButton btnSpeak;
  private TextView txtText; //voice app

//second activity
public final static String EXTRA_MESSAGE = "com.example.voicetesting.MESSAGE";
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);     
    t1= new Thread(new ClientThread());
    t1.start();
    t2= new Thread(new ClientThread2());
    t2.start();


   //voice app
    txtText = (TextView) findViewById(R.id.txtText);

       btnSpeak = (ImageButton) findViewById(R.id.btnSpeak);

       btnSpeak.setOnClickListener(new View.OnClickListener() {

           @Override
           public void onClick(View v) {

               Intent intent = new Intent(
                       RecognizerIntent.ACTION_RECOGNIZE_SPEECH);

               intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, "en-US");
               try {
                   startActivityForResult(intent, RESULT_SPEECH);
                   txtText.setText("");
               } catch (ActivityNotFoundException a) {
                   Toast t = Toast.makeText(getApplicationContext(),
                           "Opps! Your device doesn't support Speech to Text",
                           Toast.LENGTH_SHORT);
                   t.show();
               }
           }
       }); 
       //voice app  
    spinner = (Spinner)findViewById(R.id.spinner);
    ArrayAdapter<String>adapter = new ArrayAdapter<String>(this,
            android.R.layout.simple_spinner_item,paths);

    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spinner.setAdapter(adapter);
    spinner.setOnItemSelectedListener((OnItemSelectedListener) this);




    //starting two threads which establishes the connection between the client 
    //to the two different servers

按钮btnopen =(按钮)findViewById(R.id.btnWindowAnimation);

    btnopen.setOnClickListener(new View.OnClickListener() {



        @Override
        public void onClick(View v) {

            Intent slideactivity = new Intent(MainActivity.this, SlideActivity.class);

            Bundle bndlanimation = 
                    ActivityOptions.makeCustomAnimation(getApplicationContext(), R.anim.animation,R.anim.animation2).toBundle();
            startActivity(slideactivity, bndlanimation);

        }
    });
}

@Override
   public boolean onCreateOptionsMenu(Menu menu) {
       getMenuInflater().inflate(R.menu.main, menu);
       return true;
   }

   @Override
   protected void onActivityResult(int requestCode, int resultCode, Intent data) {
       super.onActivityResult(requestCode, resultCode, data);

       switch (requestCode) {
       case RESULT_SPEECH: {
           if (resultCode == RESULT_OK && null != data) {

               ArrayList<String> text = data
                       .getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);

         txtText.setText(text.get(0));
         String abc=(String) txtText.getText();

         Toast t = Toast.makeText(getApplicationContext(),
                  abc,
                   Toast.LENGTH_SHORT);
           t.show();
           switch(abc)
           {
           case "light on": onClickon(null);
           break;

           case "light off": onClickoff(null);
           break;

           case "fan on": onClickon2(null);
           break;

           case "fan off": onClickoff2(null);
           break;
           }



           }
           break;
       }

       }

}

public void onItemSelected(AdapterView<?> parent, View v, int position, long id) {

    switch (position) {
        case 1:
            try{
                //sending a message to the linino to play the song 
                PrintWriter out = new PrintWriter(new BufferedWriter(
                        new OutputStreamWriter(socket2.getOutputStream())),
                        true);
                out.println("1");
                }
                catch (UnknownHostException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            break;
        case 2:
            try{
                //sending a message to the linino to play the song 
                PrintWriter out = new PrintWriter(new BufferedWriter(
                        new OutputStreamWriter(socket2.getOutputStream())),
                        true);
                out.println("2");
                }
                catch (UnknownHostException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            break;
        case 3:
            try{
                //sending a message to the linino to play the song 
                PrintWriter out = new PrintWriter(new BufferedWriter(
                        new OutputStreamWriter(socket2.getOutputStream())),
                        true);
                out.println("3");
                }
                catch (UnknownHostException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            break;

    }
}

public void onClickon(View view) {
    try{

    //sending the message to the linino as 0 to turn of the light
    PrintWriter out = new PrintWriter(new BufferedWriter(
            new OutputStreamWriter(socket.getOutputStream())),
            true);
    out.println("0"); 
    }
    catch (UnknownHostException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

public void onClickoff(View view) {
    try{
    //sending the message to the linino as 1 to turn on the light
    PrintWriter out = new PrintWriter(new BufferedWriter(
            new OutputStreamWriter(socket.getOutputStream())),
            true);
    out.println("1");
    }
    catch (UnknownHostException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } catch (Exception e) {
        e.printStackTrace();
    }
}


public void onClickon2(View view) {
    try{

    //sending the message to the linino as 0 to turn of the light
    PrintWriter out = new PrintWriter(new BufferedWriter(
            new OutputStreamWriter(socket.getOutputStream())),
            true);
    out.println("2"); 
    }
    catch (UnknownHostException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

public void onClickoff2(View view) {
    try{
    //sending the message to the linino as 1 to turn on the light
    PrintWriter out = new PrintWriter(new BufferedWriter(
            new OutputStreamWriter(socket.getOutputStream())),
            true);
    out.println("3");
    }
    catch (UnknownHostException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

/** Called when the user clicks the Send button */
@SuppressWarnings("deprecation")
public void sendMessage(View view) {

    /*try {
        socket.close();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    try {
        socket2.close();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }*/
    Intent intent = new Intent(this, DisplayMessageActivity.class);
    //EditText editText = (EditText) findViewById(R.id.edit_message);
   String message = "sdfsfdsf";
    intent.putExtra(EXTRA_MESSAGE,message);
    startActivity(intent);

}



class ClientThread implements Runnable {
    @SuppressWarnings("deprecation")
    @Override
    public void run() {

        try {
            InetAddress serverAddr = InetAddress.getByName(SERVER_IP);

            socket = new Socket(serverAddr, SERVERPORT);

        } catch (UnknownHostException e1) {
            e1.printStackTrace();
        } catch (IOException e1) {
            e1.printStackTrace();
        }

    }

}

class ClientThread2 implements Runnable {
    @Override
    public void run() {

        try {
            InetAddress serverAddr = InetAddress.getByName(SERVER_IP2);

            socket2 = new Socket(serverAddr, SERVERPORT2);

        } catch (UnknownHostException e1) {
            e1.printStackTrace();
        } catch (IOException e1) {
            e1.printStackTrace();
        }

    }

}

@Override
public void onNothingSelected(AdapterView<?> arg0) {
    // TODO Auto-generated method stub

}

}

0 个答案:

没有答案