Android底层结构-SDK Service线程知多少

  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

二、
如果属于 Remote Service。
---- 则 myActivity 与 myService 各在不同进程里执行, 两者都是由各 进程的 main thread 所执行。亦即,两者是由不同的线程所执行。此情 形下, 两个类别里的函数也不宜太费时(例如不宜超过 5 秒钟); 但必要 时可诞生子线程去执行较费时的函数。 ---- 所以由 main thread(myService 所在的进程的 main thread)执行 myService::onCreate()、myBinder::myBinder()和 myService::onBind()。 ---- 在 Binding-time 时,Binder System 会建立 myActivity 与 myBinder(即 myService 的 Interface)之间的连结(Connection)。 ---- 在 IPC calling-time 时,myActivity 的线程与 myBinder 的线程 会同步(Synchronize), 让 myActivity 开发者觉得 IPC 远程呼叫、跨进 程的两个线程,就如同单一线程一般。 ----在送出 IPC call 时,执行 myBinder 对象的线程会执行 myBinder::onTransact()函数。 兹写个程序来说明之,此程序执行时,出现下述画面:
//AndroidManifest.xml <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.misoo.kx02c"> <application android:icon="@drawable/icon"> <activity android:name=".ac01" android:label="@string/app_name">
按下上一个 Button, 显示出 main thread 的执行路径:
按下第二个 Button, 显示出 main thread 执行 onBind()函数:
按下第三个 Button, 显示出 Binder System 启用独立的 thread 执行 onTransact()函数:
兹列出程序代码如下:
//ac01.java
package com.misoo.kx02c; import android.app.Activity; import android.content.ComponentName; import android.content.Context; import android.content.Intent; import android.content.ServiceConnection; import android.graphics.Color; import android.os.Bundle; import android.os.IBinder; import android.os.Parcel; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.LinearLayout; import android.widget.TextView; public class ac01 extends Activity implements OnClickListener { private final int WC = LinearLayout.LayoutParams.WRAP_CONTENT; private final int FP = LinearLayout.LayoutParams.FILL_PARENT; private Button btn, btn2, btn3, btn4; public TextView tv; private IBinder ib = null; public void onCreate(Bundle icicle) { super.onCreate(icicle); LinearLayout layout = new LinearLayout(this); layout.setOrientation(LinearLayout.VERTICAL); btn = new Button(this); btn.setId(101); btn.setText("start service"); btn.setBackgroundResource(R.drawable.heart); btn.setOnClickListener(this); LinearLayout.LayoutParams param = new LinearLayout.LayoutParams(120, 50); param.topMargin = 10; layout.addView(btn, param); btn2 = new Button(this);
//ac01.java (myActivity)
package com.misoo.kx02b; import android.app.Activity; import android.content.Intent; import android.graphics.Color; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.LinearLayout; import android.widget.TextView; public class ac01 extends Activity implements OnClickListener { //private final int WC = LinearLayout.LayoutParams.WRAP_CONTENT; private TextView tx; private Button btn; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); LinearLayout layout = new LinearLayout(this); layout.setOrientation(LinearLayout.VERTICAL); LinearLayout.LayoutParams param = new LinearLayout.LayoutParams(150, 40); param.topMargin = 5; tx = new TextView(this); tx.setTextSize(16); tx.setTextColor(Color.BLUE); tx.setBackgroundResource(R.drawable.x_yellow); layout.addView(tx, param); btn = new Button(this); btn.setText("Exit"); btn.setBackgroundResource(R.drawable.earth); btn.setOnClickListener(this); layout.addView(btn, param); this.setContentView(layout); //-------------------------------------------------String tna = Thread.currentThread().getName(); Thread.currentThread().setName(tna + "-myActivity"); //-------------------------------------------------myService.setUpdateListener(new UpdateUIListener()); Intent svc = new Intent(this, myService.class); startService(svc); } @Override protected void onDestroy() { super.onDestroy(); { Intent svc = new Intent(this, myService.class); stopService(svc);
} } class UpdateUIListener implements IListener { public void update(String s) { tx.setText(s); } } public void onClick(View v) { this.finish(); } } IListener.java package com.misoo.kx02b; public interface IListener { public void update(String s); } //myService.java package com.misoo.kx02b; import java.util.Timer; import java.util.TimerTask; import android.app.Service; import android.content.Intent; import android.os.Handler; import android.os.IBinder; import android.os.Message; public class myService extends Service { private static IListener plis; private static int k = 0; private Timer timer = new Timer(); private String tna; public Handler handler = new Handler(){ public void handleMessage(Message msg) { plis.update(tna); } }; @Override public void onCreate() { super.onCreate(); tna = Thread.currentThread().getName(); TimerTask task = new TimerTask() { @Override public void run() { handler.sendEmptyMessage(0); } }; timer.schedule(task,1000, 4000); } @Override public IBinder onBind(Intent intent) { return null; } public static void setUpdateListener(IListener listener) {
本文就 Local SDK-Service 与 Remote SDK-Service 两种来说明之。 一、 如果属于 Local Service。
---- 则 myActivity 与 myService 两者都是由 main thread 所执行。亦即,两者 是同一线程所执行。此情形下,两个类别里的函数都不宜太费时(例如不宜超过 5 秒钟);但必要时可诞生子线程去执行费时的函数。
Android 底层结构:SDK Service 线程知多少

总是由进程(Process)的主线程(Main thread)执行 SDK-Service(如下图的 myService)对象。

Android 底层 Binder System 在 binding-time 会从该进程的 Thread pool 里启动一 个线程来执行 SDK-Service 的 Binder 接口对象(如 myBinder)。 执行 myActivity 对象的线程与 myBinder 对象的线程会同步(Synchronize),让 myActivity 开发者觉得 IPC 远程呼叫、跨进程的两个线程,就如同单一线程一般。
<intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" />
</intent-filter> </activity> <activity android:name=".myActivity" android:process=":remote"> </activity> <service android:name=".myService" android:process=":remote"> <intent-filter> <action android:name="com.misoo.kx02c.REMOTE_SERVICE" /> </intent-filter> </service> </application> </manifest>
plis = listener; } } 此程序输出:
此结果说明了,myActivity 与 myService 两者都是由 main thread 所执行。
二. 就一个 SDK Service 而言, 都是由 main thread 执行 myService 的 onCreate()、 onBind()、 onStart()等函数。 然而,是由独立的线程来执行 myBinder::onTransact()函数。因为 Binder System 会从该进程的 Threaபைடு நூலகம் pool 里启动一个线程来执行 SDK-Service 的 Binder 接口对象(如 myBinder)。
相关文档
最新文档