Service的启动方式
1)startService启动。启动的Service就一直(相对来说)在后台允许,与启动该service的其他组件的生命周期没有关系。
2)bindService启动。如果是Activity启动的,那么当这个Activity销毁的时候Service也销毁。
假设我们有这样的需求:我们想让一个Service帮我们干一个比较耗时的工作(比如下载大文件),当Service干完活后自动销毁(目的是为节省内存)。
则可以采用IntentService进行处理
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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">
<Button
android:onClick="startIntentService"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="启动IntentService" />
</RelativeLayout>
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.test.intentservicedemo">
<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/Theme.IntentServiceDemo">
<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=".MyIntentService"/>
</application>
</manifest>
MainActivity.java
package com.test.intentservicedemo;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void startIntentService(View view) {
Intent intent = new Intent(this, MyIntentService.class);
intent.putExtra("url", "https://www.baidu.com/****");
startService(intent);
}
}
MyIntentService.java
package com.test.intentservicedemo;
import android.app.IntentService;
import android.content.Intent;
import android.os.SystemClock;
import android.util.Log;
import androidx.annotation.Nullable;
public class MyIntentService extends IntentService {
//必须要添加,不然启动不了
public MyIntentService() {
this("MyThreadName");
}
/**
* Creates an IntentService. Invoked by your subclass's constructor.
*
* @param name Used to name the worker thread, important only for debugging.
* name:用于处理onHandleIntent方法的线程的名称
*/
public MyIntentService(String name) {
super(name);
}
//用于处理Intent的请求,该方法是在子线程中执行。给这个子线程起个名称
@Override
protected void onHandleIntent(@Nullable Intent intent) {
Log.d("abc", "ThreadName = " + Thread.currentThread().getName());
String url = intent.getStringExtra("url");
Log.d("abc", "url:" + url);
// 模拟耗时操作
SystemClock.sleep(5000);
Log.d("abc", "执行完任务了");
}
@Override
public void onDestroy() {
super.onDestroy();
Log.d("abc", "onDestroy");
}
}
运行结果:
2021-05-27 09:16:48.675 18833-18882/com.test.intentservicedemo D/abc: ThreadName = IntentService[MyThreadName]
2021-05-27 09:16:48.675 18833-18882/com.test.intentservicedemo D/abc: url:https://www.baidu.com/****
2021-05-27 09:16:53.676 18833-18882/com.test.intentservicedemo D/abc: 执行完任务了
2021-05-27 09:16:53.679 18833-18833/com.test.intentservicedemo D/abc: onDestroy