JWorld@TW the best professional Java site in Taiwan
      註冊 | 登入 | 全文檢索 | 排行榜  

» JWorld@TW » Java ME、Google Android 平台與 JavaCard 討論區 » Android  

按列印兼容模式列印這個話題 列印話題    把這個話題寄給朋友 寄給朋友    訂閱主題
reply to postflat modego to previous topicgo to next topic
本主題所含的標籤
作者 Re:如何在接收到封包時更新UI元件 [Re:罪惡感]
罪惡感





發文: 13
積分: 0
於 2013-09-12 15:49 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
感謝各位幫忙,我後來照各位所說,改用成Handler及handleMessage的方式就可以正常運作了

附上程式碼
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
package com.example.test2;
 
//------------------import------------------//
import android.app.Activity;
import android.app.Dialog;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;
 
//------------------主要介面------------------//
public class MainActivity extends Activity {
 
  private Dialog mLoginDlg;
  private ImageButton Config_btn;
  private TextView NowTemp_Rul;
  private TimerReceiver receiver;
  private Handler mHandler;
 
  // ------------------初始創建執行onCreat方法------------------//
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    // LoginCheck();
    findView();
    registerTimerReceiver();
  }
 
  @Override
  public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
  }
 
  // ------------------載入所需元件及註冊------------------//
  private void findView() {
    //
    ImageButton Config_btn = (ImageButton) findViewById(R.id.Config_btn);
    final TextView NowTemp_Rul = (TextView) findViewById(R.id.NowTemp_Rul);
    //
    Config_btn.setOnClickListener(new OnClickListener() {// Config_btn被按下後轉跳到config介面
          public void onClick(View arg0) {
            Intent Configintent = new Intent(MainActivity.this,
                config.class);
            startActivity(Configintent);
          }
        });
    Intent intent = new Intent(MainActivity.this, TimerService.class);
    startService(intent);
 
    mHandler = new Handler() {
 
      @Override
      public void handleMessage(Message msg) {
        switch (msg.what) {
        case 1:
          NowTemp_Rul.setText(String.valueOf(msg.getData().getInt(
              "Temp")));
          break;
        }
        super.handleMessage(msg);
      }
    };
  }
 
  // ------------------帳號密碼驗證------------------//
  private void LoginCheck() {
    // 帳號密碼驗證視窗
    mLoginDlg = new Dialog(MainActivity.this);
    mLoginDlg.setTitle("請輸入帳號密碼:");
    mLoginDlg.setCancelable(false);
    mLoginDlg.setContentView(R.layout.log);
    Button loginBtnOk = (Button) mLoginDlg.findViewById(R.id.btnOK);
    Button loginBtnCancel = (Button) mLoginDlg.findViewById(R.id.btnCancel);
 
    // 帳號登入確認鍵被按下
    loginBtnOk.setOnClickListener(new OnClickListener() {
      public void onClick(View arg0) {
        TextView mTxtResult = (TextView) mLoginDlg
            .findViewById(R.id.mTxtResult);
        EditText edtUserName = (EditText) mLoginDlg
            .findViewById(R.id.edtUserName);
        EditText edtPassword = (EditText) mLoginDlg
            .findViewById(R.id.edtPassword);
 
        if ("admin".equals(edtUserName.getText().toString())
            && "1234".equals(edtPassword.getText().toString()))
          mLoginDlg.cancel();
        else {
          mTxtResult.setText("帳號密碼輸入錯誤!!");
          edtUserName.setText("");
          edtPassword.setText("");
        }
      }
    });
    // 帳號登入取消鍵被按下
    loginBtnCancel.setOnClickListener(new OnClickListener() {
      public void onClick(View arg0) {
        MainActivity.this.finish();// 退出應用程式
      }
    });
    mLoginDlg.show();
    //
  }
 
  // ------------------退出程式時執行onDestroy方法------------------//
  public void onDestroy() {
    super.onDestroy();
    // 解除BroadcastReceiver的註冊
    unregisterReceiver(receiver);
  }
 
  public void registerTimerReceiver() {
    // 設定只攔截會發送指定字串的 Broadcast
    IntentFilter filter = new IntentFilter(TimerService.TIMER_ACTION);
    receiver = new TimerReceiver();
    // 註冊BroadcastReceiver,當欲攔截的Broadcast發送過來時,
    // 會呼叫對應的onReceive()
    registerReceiver(receiver, filter);
  }
 
  public class TimerReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
      Bundle bundle = new Bundle();
      bundle.putInt("Temp", intent.getExtras().getInt("Temp"));
      try {
        Message msg = new Message();
        msg.what = 1;
        msg.setData(bundle);
        mHandler.sendMessage(msg);
      } catch (Exception e) {
        e.printStackTrace();
      }
    }
  }
}


reply to postreply to post
話題樹型展開
人氣 標題 作者 字數 發文時間
2250 如何在接收到封包時更新UI元件 罪惡感 7971 2013-09-10 19:43
2097 Re:如何在接收到封包時更新UI元件 cutebaby 26 2013-09-10 22:49
2107 Re:如何在接收到封包時更新UI元件 lnmlee 324 2013-09-10 23:46
2094 Re:如何在接收到封包時更新UI元件 henry4343 245 2013-09-11 10:42
2091 Re:如何在接收到封包時更新UI元件 lnmlee 322 2013-09-11 11:10
2089 Re:如何在接收到封包時更新UI元件 罪惡感 2555 2013-09-11 16:32
2017 Re:如何在接收到封包時更新UI元件 cutebaby 49 2013-09-12 07:14
2103 Re:如何在接收到封包時更新UI元件 罪惡感 4437 2013-09-12 15:49
» JWorld@TW »  Java ME、Google Android 平台與 JavaCard 討論區 » Android

reply to postflat modego to previous topicgo to next topic
  已讀文章
  新的文章
  被刪除的文章
Jump to the top of page

JWorld@TW 本站商標資訊

Powered by Powerful JuteForum® Version Jute 1.5.8