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
本主題所含的標籤
作者 如何在接收到封包時更新UI元件
罪惡感





發文: 13
積分: 0
於 2013-09-10 19:43 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
本人是想以Service接受資料後,傳到主Activity並顯示給使用者看,所以Service是每一秒送一個亂數產生的數值給主Activity,但我接收封包後想更動TextView的Text會直接退出程式

MainActivity.java
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
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.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;
//------------------初始創建執行onCreat方法------------------//
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    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);
    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);
    
    Runnable UpdataUI = new Runnable() {
      @Override
      public void run() {
        registerTimerReceiver();
        }
      };
      
      Thread t = new Thread(UpdataUI);
      t.start();
  }
  
  //------------------退出程式時執行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, final Intent intent) {
      NowTemp_Rul.setText(String.valueOf(intent.getExtras().getInt("Temp")));
    }  
  }
}


Service.java
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
package com.example.test2;
 
//------------------import------------------//
import java.util.Timer;
import java.util.TimerTask;
 
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Intent;
import android.os.Bundle;
import android.os.IBinder;
import android.os.Message;
import android.util.Log;
import android.widget.Toast;
 
//------------------TimerServuce類別------------------//
public class TimerService extends Service {
  private final static int NOTIFICATION_ID = 0;
  public final static String TIMER_ACTION = "com.example.test2";
  private Timer timer;
  private int d;
  Intent Timerintent = new Intent(TIMER_ACTION);
 
  @Override
  // 第一次啟動Service時會呼叫onCreate()
  public void onCreate() {
    super.onCreate();
  }
 
  @Override
  // 以startService()方式啟動會呼叫onStartCommand()
  public int onStartCommand(Intent intent, int flags, int startId) {
    super.onStartCommand(intent, flags, startId);
    // 建立Timer,並將使用者輸入的秒數設定成要延遲啟動的時間;
    // 時間一到便會執行TimerTask的run()內容— 傳送指定的Broadcast
    
      TimerTask task = new TimerTask() {
     
     @Override public void run() { d = (int)(Math.random()*40+1);
      Timerintent.putExtra("Temp", d); Log.v("", String.valueOf(d));
      sendBroadcast(Timerintent); } };
                  timer = new Timer();
      timer.schedule(task,0,1000);
     
    // 回傳START_STICKY可以保證再次建立新的Service時仍會呼叫onStartCommand()
     return START_STICKY;  
 
  }
  @Override
  // Service準備結束時會呼叫此方法
  public void onDestroy() {
    super.onDestroy();
    // 停止Timer與其指定的工作排程
    timer.cancel();
  }
 
  @Override
  public IBinder onBind(Intent intent) {
    return null;
  }
 
}


activity_main.xml
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
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    tools:context=".MainActivity" >
 
    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:orientation="vertical" >
 
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="60dp" >
 
            <TextView
                android:id="@+id/TempView"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="bottom|end"
                android:text="@string/Current_temperature"
                android:textSize="20sp" />
 
            <TextView
                android:id="@+id/NowTemp_Rul"
                android:layout_width="150dp"
                android:layout_height="wrap_content"
                android:layout_gravity="bottom"
                android:layout_weight="1.40"
                android:textSize="20sp" />
 
            <ImageButton
                android:id="@+id/Config_btn"
                android:layout_width="30dp"
                android:layout_height="30dp"
                android:layout_weight="1"
                android:background="@drawable/config"
                android:src="@drawable/config" />
 
        </LinearLayout>
 
    </LinearLayout>
 
</RelativeLayout>
 


AndroidManifest.xml
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
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.test2"
    android:versionCode="1"
    android:versionName="1.0" >
 
    <uses-sdk
        android:minSdkVersion="11"
        android:targetSdkVersion="18" />
 
    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.test2.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
 
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name=".config"
            android:label="@string/config_interface" >
        </activity>
 
        <service
            android:name=".TimerService"
            android:enabled="true" />
    </application>
 
</manifest>


主要是卡在,我收到封包後
1
2
3
4
5
6
public class TimerReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, final Intent intent) {
      NowTemp_Rul.setText(String.valueOf(intent.getExtras().getInt("Temp")));
    }  
  }


這段中的NowTemp_Rul.setText(String.valueOf(intent.getExtras().getInt("Temp")));會無法執行,這問題我已經查兩星期的,也試過蠻多種寫法最後都會卡在這行,不好意思,麻煩各位了


reply to postreply to post
話題樹型展開
人氣 標題 作者 字數 發文時間
2251 如何在接收到封包時更新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
2090 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