知乎專欄 | 多維度架構 | | | 微信號 netkiller-ebook | | | QQ群:128659835 請註明“讀者” |
目錄
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="cn.netkiller.service"> <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>
這段代碼不是手工加入的,只需在 Android Studio 中選擇 File - New - Service - Service 創建 Service 會自動加入下面代碼
<service android:name=".MyService" android:enabled="true" android:exported="true"></service>
在 Android Studio 中選擇 File - New - Service - Service 創建 Service
MyService繼承自Service,並重寫父類的onCreate()、onStartCommand()和onDestroy()方法
package cn.netkiller.service; import android.app.Service; import android.content.Intent; import android.os.IBinder; import android.util.Log; public class MyService extends Service { public MyService() { } @Override public void onCreate() { super.onCreate(); Log.d("Service", "onCreate() executed"); Log.d("Service", "MyService thread id is " + Thread.currentThread().getId()); } @Override public int onStartCommand(Intent intent, int flags, int startId) { Log.d("Service", "onStartCommand() executed"); return super.onStartCommand(intent, flags, startId); } @Override public void onDestroy() { super.onDestroy(); Log.d("Service", "onDestroy() executed"); } @Override public IBinder onBind(Intent intent) { // TODO: Return the communication channel to the service. throw new UnsupportedOperationException("Not yet implemented"); } }
onCreate() Service 創建的時候執行,已經創建的Service不會再執行
onStartCommand() 任何時候,只要執行 startService(intent); 便會執行
onDestroy() 停止的時候執行
在佈局檔案中加入了兩個按鈕,一個用於啟動Service,一個用於停止Service
<?xml version="1.0" encoding="utf-8"?> <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" 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=".MainActivity"> <LinearLayout android:layout_width="368dp" android:layout_height="229dp" android:layout_marginStart="8dp" android:layout_marginTop="8dp" android:layout_marginEnd="8dp" android:layout_marginBottom="8dp" android:orientation="vertical" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintHorizontal_bias="0.0" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" app:layout_constraintVertical_bias="0.0"> <Button android:id="@+id/startService" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Start service" /> <Button android:id="@+id/stopService" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Stop service" /> </LinearLayout> </android.support.constraint.ConstraintLayout>
package cn.netkiller.service; import android.content.Intent; 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 startService; private Button stopService; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); startService = (Button) findViewById(R.id.startService); stopService = (Button) findViewById(R.id.stopService); startService.setOnClickListener(this); stopService.setOnClickListener(this); Log.d("Service", "MainActivity thread id is " + Thread.currentThread().getId()); } @Override public void onClick(View v) { Intent intent; switch (v.getId()) { case R.id.startService: intent = new Intent(this, MyService.class); startService(intent); break; case R.id.stopService: intent = new Intent(this, MyService.class); stopService(intent); break; default: break; } } }