后台任务
后台任务
Service
- 组件必须注册
- 服务并不是运行在一个独立的进程中,而是依赖于创建服务时所在的应用程序进程中。应用程序被杀死时,所有依赖它的服务都会停止
- 要在服务的内部创建子线程执行具体任务,否则默认运行在主线程,有可能出现主线程被阻塞的情况
- activity_my2.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:layout_height="match_parent"
android:layout_width="match_parent"
android:orientation="vertical"
xmlns:android="http://schemas.android.com/apk/res/android">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="startService"
android:onClick="startService"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="stopService"
android:onClick="stopService"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="bindService"
android:onClick="bindService"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="unbindService"
android:onClick="unbindService"/>
</LinearLayout>
</LinearLayout>
- MyService.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package com.example.myactivity1;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;
import androidx.annotation.Nullable;
public class MyService extends Service {
private static final String TAG = "xxx";
@Override
public void onCreate() {
super.onCreate();
Log.d(TAG, "onCreate: ");
}
@Override
public void onStart(Intent intent, int startId) {
super.onStart(intent, startId);
Log.d(TAG, "onStart: ");
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.d(TAG, "onStartCommand: ");
return super.onStartCommand(intent, flags, startId);
}
@Override
public void onDestroy() {
super.onDestroy();
Log.d(TAG, "onDestroy: ");
}
@Nullable
@Override
public IBinder onBind(Intent intent) {
Log.d(TAG, "onBind: ");
return null;
}
@Override
public boolean onUnbind(Intent intent) {
Log.d(TAG, "onUnbind: ");
return super.onUnbind(intent);
}
}
- MyActivity2.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
package com.example.myactivity1;
import androidx.appcompat.app.AppCompatActivity;
import android.app.Service;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.view.View;
public class MyActivity2 extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my2);
}
// 关闭activity后会在后台运行
public void startService(View view) {
/**
* onCreate() -> onStartCommand() -> onStart()
*/
startService(new Intent(this, MyService.class));
}
public void stopService(View view) {
/**
* onDestroy()
*/
stopService(new Intent(this, MyService.class));
}
// 与activity共存亡
public void bindService(View view) {
/**
*onCreate() -> onBind()
*/
bindService(new Intent(this, MyService.class), connection, BIND_AUTO_CREATE);
}
public void unbindService(View view) {
/**
*onUnbind() -> onDestroy
*/
unbindService(connection);
}
private ServiceConnection connection = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
}
@Override
public void onServiceDisconnected(ComponentName componentName) {
}
};
// 一般写法,当activity销毁时,自动解绑服务
@Override
protected void onDestroy() {
super.onDestroy();
unbindService(connection);
}
}
Handler
- MainActivity.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
package com.example.mythread;
import androidx.appcompat.app.AppCompatActivity;
import android.annotation.SuppressLint;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity implements View.OnClickListener{
public static final int UPDATE_TEXT = 1;
private TextView textView;
private Button button;
@SuppressLint("HandlerLeak")
private Handler handler = new Handler(){
public void handleMessage(Message msg){
switch (msg.what){
case UPDATE_TEXT:
Toast.makeText(MainActivity.this, "handleMessage", Toast.LENGTH_SHORT).show();
textView.setText("xixi");
break;
default:
}
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView = findViewById(R.id.tv);
button = findViewById(R.id.btn);
button.setOnClickListener(this);
}
@Override
public void onClick(View view) {
switch (view.getId()){
case R.id.btn:
Toast.makeText(MainActivity.this, "onClick", Toast.LENGTH_SHORT).show();
new Thread(()->{
Message message = new Message();
message.what = UPDATE_TEXT;
handler.sendMessage(message);
}).start();
break;
default:
}
}
}
AsyncTask
本文由作者按照 CC BY 4.0 进行授权