Android 之路55---四大元件之Service
阿新 • • 發佈:2019-01-03
導讀
1.簡介
2.兩種服務的使用
簡介
兩種服務的使用
說明:接下來的案例運用了兩種服務,其中1~100迴圈執行緒是與Activity無關的,而與列印執行緒進度是需要繫結的
配置檔案 AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.hala.srevicedemo">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service
android:name=".MyService"
android:enabled="true"
android:exported="true"></service>
</application>
</manifest>
佈局檔案 activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.hala.srevicedemo.MainActivity">
<Button
android:id="@+id/start"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="啟動服務"
android:onClick="onClick"/>
<Button
android:id="@+id/stop"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="停止服務"
android:onClick="onClick"/>
<Button
android:id="@+id/bind"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="繫結服務"
android:onClick="onClick"/>
<Button
android:id="@+id/unbind"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="解鎖服務"
android:onClick="onClick"/>
</LinearLayout>
MainActivity.java
package com.hala.srevicedemo;
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;
public class MainActivity extends AppCompatActivity {
private ServiceConnection conn=new ServiceConnection() {
//當客戶端正常連線著Service時,執行服務的繫結操作會被呼叫
//如果沒有ServiceConnection,會出現繫結後解綁,無法再次繫結的情況
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
Log.d("TAG","hello,bind again");
//強制轉換
MyService.MyBinder mb=(MyService.MyBinder)service;
int step=mb.getProcess();
Log.d("TAG","當前進度為:"+step);
}
//當客戶端和服務的連線丟失了
@Override
public void onServiceDisconnected(ComponentName name) {
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void onClick(View view){
//start 與 stop為一組
//bind 與 unbind為一組
switch (view.getId()){
case R.id.start:
//如果服務已經建立,重複啟動不會重新建立,操作的是同一個服務,除非先銷燬
Intent it=new Intent(this,MyService.class);
startService(it);
break;
case R.id.stop:
//這裡雖然是兩個intent,但確實針對同一個物件
Intent it1=new Intent(this,MyService.class);
stopService(it1);
break;
case R.id.bind:
//繫結服務:最大作用是對Service執行的任務進行監控,如下載到哪了,音樂播放到哪了
Intent it2=new Intent(this,MyService.class);
bindService(it2,conn,BIND_AUTO_CREATE);
break;
case R.id.unbind:
//要指明解綁哪個連線,所以conn要設定為全域性
unbindService(conn);
break;
}
}
}
MyService.java
package com.hala.srevicedemo;
import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
import android.util.Log;
//實現進度監控(兩個要素):
//ServicerConnection:用與繫結客戶端和Service->見MainActivity.java有說明
//IBinder:在android中用於遠端操作物件的一個基本介面
//因為IBinder強制要求我們實現一些方法,而Binder類是給所有強制方法實現了一個空方法
//在開發中我們會自己寫一個內部類,繼承Binder類,在裡邊寫自己的方法
public class MyService extends Service {
public static final String TAG = "TAG";
private int i;
public MyService() {
}
/**
* 建立
*/
@Override
public void onCreate() {
super.onCreate();
Log.e(TAG,"服務建立了");
//開啟一個執行緒,模擬耗時任務
new Thread(){
@Override
public void run() {
super.run();
try {
for(i = 0; i <100; i++){
sleep(1000);
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}.start();
}
/**
* 啟動
* @param intent
* @param flags
* @param startId
* @return
*/
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.e(TAG,"服務啟動了");
return super.onStartCommand(intent, flags, startId);
}
/**
* 繫結
* @param intent
* @return
*/
@Override
public IBinder onBind(Intent intent) {
// TODO: Return the communication channel to the service.
Log.e(TAG,"服務綁定了");
return new MyBinder();
}
class MyBinder extends Binder{
public int getProcess(){
return i;
}
}
/**
* 解綁
* @param intent
* @return
*/
@Override
public boolean onUnbind(Intent intent) {
Log.e(TAG,"服務解綁了");
return super.onUnbind(intent);
}
/**
* 銷燬
*/
@Override
public void onDestroy() {
super.onDestroy();
Log.e(TAG,"服務銷燬了");
}
}
顯示結果