Android Service 入門

說明

Service 工做在主進程上。生命週期圖ide

Android Service lifecycle

 

兩種狀態測試

Startedthis

好比Activity經過調用startService 方法。一旦被啓動(Started),服務就永久在後臺運行,即便建立他的Activity被銷燬。spa

Bound3d

當一個Component經過調用bindService方法來綁定該服務。服務提供接口讓組件和服務交互。code

 

回調方法說明component

onStartCommand 其餘組件經過調用startService時,被調用,若是實現該方法,須要調用stopSelf或者stopService來結束服務。xml

onBind 其餘組件經過調用bindService時,被調用。須要實現接口,返回IBinder對象,讓Client和服務交互。若是不綁定服務,返回null對象

onUnbind 全部已經綁定的組件斷開blog

onCreate建立服務只有一次。

onDestroy服務被銷燬時調用,這裏應該清理相應資源。

 

例子1:

 主界面兩個按鈕

 

 

Activity對應的兩個方法

    public void startService(View view)
    {

        startService(new Intent(getBaseContext(), MyService.class));
    }
    public void stopService(View view)
    {
        stopService(new Intent(getBaseContext(), MyService.class));
    }

 

 服務類的內容

public class MyService extends Service {
    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Toast.makeText(this, "Servcie Started", Toast.LENGTH_LONG).show();
        return START_STICKY;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        Toast.makeText(this, "Service Destroyed", Toast.LENGTH_SHORT).show();
    }
}

而後在AndroidManifest.xml中添加

 

 運行效果

點擊啓動服務

 

點擊中止服務

 

例子2:使用綁定服務,而且在Activity中調用Service的內部類的方法

在例子1的基礎上

public class MyService extends Service {
    public static final String SERVICE_LOG = "service_LOG";
    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        Log.d(SERVICE_LOG, "MyServic Bound");
        return new Mybind();
    }

    @Override
    public boolean onUnbind(Intent intent) {
        Log.d(SERVICE_LOG, "MyService Unbound");
        return super.onUnbind(intent);
    }

    @Override
    public void onCreate() {
        super.onCreate();
        Log.d(SERVICE_LOG, "Myservice created");
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Log.d(SERVICE_LOG, "Myservice Started");
        return START_STICKY;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        Log.d(SERVICE_LOG, "MyService destroyed");
    }

    public class Mybind extends Binder
    {
        public  void getString() {
            Log.d(SERVICE_LOG, "============> get a string");
        }
    }

}

主要變化,新增內部類Mybind,而且在onBind返回Mybind對象。

 

Activity中新增

 

.

新增代碼

    private MyService.Mybind mybind;
    private ServiceConnection connection=new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
            mybind = (MyService.Mybind) iBinder;
            mybind.getString();
        }

        @Override
        public void onServiceDisconnected(ComponentName componentName) {

        }
    };
    public void BindService(View view) {
        Intent bindIntent = new Intent(LoginActivity.this, MyService.class);
        bindService(bindIntent, connection, BIND_AUTO_CREATE);
    }

    public void UnbindService(View view) {
        unbindService(connection);
    }

所有代碼

public class LoginActivity extends AppCompatActivity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.test_login);
    }
    public void startService(View view)
    {startService(new Intent(getBaseContext(), MyService.class));
    }
    public void stopService(View view)
    {
        stopService(new Intent(getBaseContext(), MyService.class));
    }
    private MyService.Mybind mybind;
    private ServiceConnection connection=new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
            mybind = (MyService.Mybind) iBinder;
            mybind.getString();
        }

        @Override
        public void onServiceDisconnected(ComponentName componentName) {

        }
    };
    public void BindService(View view) {
        Intent bindIntent = new Intent(LoginActivity.this, MyService.class);
        bindService(bindIntent, connection, BIND_AUTO_CREATE);
    }

    public void UnbindService(View view) {
        unbindService(connection);
    }

}
View Code

 

運行效果

A測試

點擊綁定服務,此時建立服務,而後綁定服務,

Myservice created

MyService Bound

============> get a string

點擊解除綁定(解綁以後,再點擊解綁,程序會崩潰)

MyService Unbound

MyService destroyed

若是Activity銷燬,服務一樣自動解綁,銷燬。

B測試

點擊啓動服務

Myservice created

Myservice Started

點擊綁定服務

MyServic Bound

============> get a string

點擊解除綁定

MyService Unbound

由於是經過startService建立的後臺Service,不會銷燬。

 

 

若是有client綁定,點擊「中止服務」,service 不會中止,一旦全部client解除綁定,由於已經點擊過「中止服務」,此時服務中止。

相關文章
相關標籤/搜索