關於Service的線程問題
關於綁定Service
package com.example.javy.bindservicedemo;
import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
import android.util.Log;
public class MyService extends Service {
@Override
public IBinder onBind(Intent intent) {
Log.e("bind","onBind");
return new MyBind();
}
public class MyBind extends Binder{
public void logMessage(String msg){
Log.e("testlog",msg);
}
}
@Override
public void onDestroy() {
super.onDestroy();
Log.e("serviceDes","onDestroy");
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:orientation="vertical"
android:gravity="center"
tools:context=".MainActivity">
<Button
android:text="綁定"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/mybtnbind"
/>
<Button
android:text="解綁"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/unbind"
/>
<Button
android:text="Log"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/log"
/>
</LinearLayout>
然後看java代碼:
package com.example.javy.bindservicedemo;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.IBinder;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity implements View.OnClickListener{
private Button btnBind;
private Button btnUnbind;
private MyConn conn;
private MyService.MyBind mybind;
private Button btnLog;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
}
private void initView(){
btnBind=(Button)findViewById(R.id.mybtnbind);
btnUnbind= (Button) findViewById(R.id.unbind);
btnLog= (Button) findViewById(R.id.log);
btnBind.setOnClickListener(this);
btnUnbind.setOnClickListener(this);
btnLog.setOnClickListener(this);
}
@Override
public void onClick(View v) {
if(conn==null){
conn=new MyConn();
}
if(v.equals(btnBind)){
Intent intent =new Intent(this,MyService.class);
bindService(intent, conn, BIND_AUTO_CREATE);
}
if(v.equals(btnUnbind)){
unbindService(conn);
}
if(btnLog.equals(v)){
mybind.logMessage("挖槽~還真的可以啊");
}
}
private class MyConn implements ServiceConnection{
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
Log.e("bindLog","bind");
mybind= (MyService.MyBind) service;
}
@Override
public void onServiceDisconnected(ComponentName name) {
Log.e("bindLog","unbind");
}
}
}
我們來看看綁定的操作:
if(v.equals(btnBind)){
Intent intent =new Intent(this,MyService.class);
bindService(intent, conn, BIND_AUTO_CREATE);
}
跟普通的啓動service一樣,也是新new一個intent出來,不同的是所調用的方法不是startService而是bindService。