报警生成时更改textview值

时间:2015-10-21 04:23:00

标签: android android-intent broadcastreceiver alarmmanager

我有两个类LCDashBoard2.java和AlarmReceiver.java。我正在设置警报 LCDashBoard2.java并将其接收到AlarmReceiver.java中。         它将导致在onReceive方法中的AlarmReceiver类中显示toast消息。我在LCDashBoard2.java中有一个带有textViewBadge的textview。现在我要做的是在生成警报时更改textview的值。

public class socket_cl implements Serializable{



public void connectToServer() throws IOException {

    // Get the server address from a dialog box.


    // Make connection and initialize streams
    Socket socket = new Socket("Host", 9898);
    OutputStream outputStream = socket.getOutputStream();
    InputStream inputStream = socket.getInputStream();
    ObjectInputStream   in =   new ObjectInputStream (socket.getInputStream());
    ObjectOutputStream os = new ObjectOutputStream(outputStream);


    // Consume the initial welcoming messages from the server
    Double[][] arrays = new Double[15][15000];
    try {
        for(int i=0; i< 15; i++){
            for(int j=0; j<15000;j++){
            arrays[i][j]=Math.random()*100; 

        }

        }
    os.writeObject(arrays);
      //Grab the max from server and display it
    Double max= in.readObject();
     System.out.println(max);

            os.close();

        Thread.sleep(20 * 1000);
    } catch (InterruptedException e) {
        e.printStackTrace();
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    System.out.println("Stopping Server");

}




public static void main(String[] args) {


  socket_cl cl = new socket_cl();
  try {
    cl.connectToServer();
} catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}




}

3 个答案:

答案 0 :(得分:1)

将textview global声明为:

public static TextView textViewBadge;

并且在您的接收器中,警报响起后:

LCDashBoard2.textViewBadge.setText("YOUR TEXT");

答案 1 :(得分:0)

在您的LCDashBoard2类中创建一个函数,然后从您的AlarmReceiver onReceive中调用它。

在该函数中使用textViewBadge。

修改

在您的LCDashBoard2中添加以下功能:

public static void changeText(String args){
    textViewBadge.setText(args);
}

在你的onReceive中只需调用该函数:

LCDashBoard2.changeText("Your New Text");

答案 2 :(得分:0)

使textView成为全局变量。然后使用.setText()方法在必要时更改UI中显示的文本。