有關Android中Service實現UI更新(Binder的運用)

來源:互聯網
上載者:User

我們知道Android的程式架構本身即是遵循MVC模式設計的,將顯示和邏輯操作進行了很好的分離。xml檔案進行view的添加和布局,Activity來實現各種View的展示,而service實現將資料按一定邏輯在View中顯示。基於這樣的原則我們設計程式時,就需要做到讓他們各司其職,合理搭配,如此才能使我們設計的Android程式更高效,更安全以及易於維護,當然這是一個很大很大很大的話題,此處我只對service和Activity的職責進行簡單闡述,希望能起到拋磚引玉的作用,當然若內容如有雷同不勝榮幸.

    Service是在Android程式後台啟動並執行組件,比如音樂播放,網路下載等,這些操作的確都可以在service中完成,但並不是說我們只可以再service中完成,在Activity中一樣可以實現,那為什麼我們還要一個service呢,曾經我也疑惑過,後來我知道了在Android中又五個進程等級(1.Foreground Process: 2.Visible
Process 3.ServiceProcess4.Background
Process 5.Empty Procecc)在系統記憶體資源不夠的時候,系統會首先將等級較低的進程殺死來滿足其他高等級的進程正常運行,而service正處於第三等級,而被覆蓋住的Activiy處於第四等級,這樣當我們在運行程式時因為某種原因而將當前Activity覆蓋,那我們在該Activity中的很多操作尤其需要網路互動的很可能會因為系統記憶體資源不足,而將其殺掉。這樣就會導致資料的不完成,使程式的魯棒性不夠強,而如果將他們都放在service中實現那就穩妥多了,程式也相對穩定多了。當然程式的穩定性是由很多因素構成的,這隻是其中之一而已。那好,既然說放在service中操作,那就在那兒操作吧,這不就完了嗎,可是就在我們準備這樣做時,總會意識到一個問題,當我們把這些操作統統放到service中實現時,那Activity的中控制項的更新怎麼辦,比如因為播放總會有進度條的,檔案下載也是要時時更新下載量的,不知道各位怎麼處理這個問題的,我在網上查了查,看到的方法都是通過廣播,即在
Activity中註冊一個廣播,然後通過廣播進行service和Activity間的資料傳遞,同時以達到更新UI的目的,雖然我沒這麼做過,但我知道這是可以的,但我總覺的,這樣有點勞師動眾了,而且我曾經用了一次廣播,根據我的使用的效果來說覺得廣播不適合做一些時時更新的操作(具體原因我沒有深入研究過,不敢過多評論),反應不夠及時。所以我自己就試著用別的方法進行UI更新,最後我覺得通過Binder對象實現,怎麼實現我就不用文字說明了,下面我就隨便寫了個例子簡單說明。拋磚引玉吧

用來更新UI的service

package com.gqs.service;  import android.app.Service; import android.content.Intent; import android.os.Binder; import android.os.Handler; import android.os.IBinder; import android.os.Message; import android.widget.TextView;  public class UpdateService extends Service {     private int data;     private Handler handler;      @Override     public IBinder onBind(Intent intent) {         // TODO Auto-generated method stub         return new MyBinder();     }      public class MyBinder extends Binder {         public void setDate(final TextView tv, final UpdateData updata) {             new Thread(new MyThread()).start();             handler = new Handler() {                 public void handleMessage(Message msg) {                     updata.update(tv, data);                 }             };         }     }      public class MyThread implements Runnable {          @Override         public void run() {             while (true) {                 data++;                 Message msg = handler.obtainMessage();                 msg.arg1 = data;                 handler.sendMessage(msg);                 try {                     Thread.sleep(1000);                 } catch (InterruptedException e) {                     // TODO Auto-generated catch block                     e.printStackTrace();                 }             }         }      }      public interface UpdateData {         public void update(TextView tv, int data);      } }

package com.gqs.activity;import android.app.Activity;import android.content.ComponentName;import android.content.Context;import android.content.Intent;import android.content.ServiceConnection;import android.os.Bundle;import android.os.IBinder;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.TextView;import android.widget.Toast;import com.gqs.service.UpdateService;import com.gqs.service.UpdateService.MyBinder;import com.gqs.service.UpdateService.UpdateData;public class ServiceToActivityActivity extends Activity {    /** Called when the activity is first created. */    private TextView tv;    private UpdateService.MyBinder binder;    private Button btnStart;    private ServiceConnection conn=new ServiceConnection() {                @Override        public void onServiceDisconnected(ComponentName name) {            // TODO Auto-generated method stub                    }                @Override        public void onServiceConnected(ComponentName name, IBinder service) {            // TODO Auto-generated method stub            binder=(MyBinder) service;            tv.setText("已串連");        }    };    @Override    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.main);        btnStart=(Button)findViewById(R.id.btnStart);        tv=(TextView)findViewById(R.id.textView);        Intent intent=new Intent(this,UpdateService.class);        bindService(intent, conn, Context.BIND_AUTO_CREATE);        btnStart.setOnClickListener(new OnClickListener() {            @Override            public void onClick(View v) {                // TODO Auto-generated method stub                if(binder!=null)                {                    binder.setDate(tv, new UpdateData() {                                                @Override                        public void update(TextView tv, int data) {                            // TODO Auto-generated method stub                            tv.setText(data+"");                        }                    });                }                else                {                    Toast.makeText(getApplicationContext(), "串連失敗", 1).show();                }            }        });    }    @Override    protected void onDestroy() {        // TODO Auto-generated method stub        unbindService(conn);        super.onDestroy();    }}

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    android:orientation="vertical" >    <TextView        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:id="@+id/textView"        android:text="@string/hello" />    <Button         android:text="開始"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:id="@+id/btnStart"        /></LinearLayout>

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.