10.Service
约 669 字大约 2 分钟
Started Service(启动服务)
启动服务一旦启动,就会一直运行,直到调用stopSelf()方法或其他组件调用stopService()方法来停止它。
第一步:
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;
import android.widget.Toast;
public class MyService2 extends Service {
private static final String TAG = "MyService2";
/**
* 调用时机:当 Service 被第一次创建时调用
* 作用:在服务启动时执行一次性初始化操作,比如设置资源、初始化变量等。此时服务尚未开始处理任务。
*/
@Override
public void onCreate() {
super.onCreate();
Toast.makeText(this, "dddddddddddddddddd", Toast.LENGTH_LONG).show();
}
/**
* 调用时机:每次通过 startService() 启动服务时,都会调用此方法。
* 作用:此方法用于执行服务的实际任务。你可以在这里启动一个长时间运行的操作,或者处理传递给服务的 Intent
*/
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Toast.makeText(this, "sssssssssssssssssssssssss", Toast.LENGTH_LONG).show();
return super.onStartCommand(intent, flags, startId);
}
/**
* 调用时机:当组件(如 Activity)调用 bindService() 时,调用此方法。
* 作用:此方法在服务被绑定时调用。通过此方法返回一个 IBinder 对象,允许客户端与服务进行通信。若服务不支持绑定,可以返回 null。
*/
@Override
public IBinder onBind(Intent intent) {
return null;
}
}
第二步
AndroidManifest.xml
<service
android:name=".service.MyService2"
android:enabled="true"
android:exported="true" />
第三步
Intent intent = new Intent(this, MyService2.class);
startService(intent);
Bound Service(绑定服务)
第一步:
import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
import android.widget.Toast;
public class MyService extends Service {
private static final String TAG = "MyService";
@Override
public IBinder onBind(Intent intent) {
return new LocalBinder();
}
public void performTask() {
Toast.makeText(this, "sssssseeeeeeeeee", Toast.LENGTH_LONG).show();
}
public class LocalBinder extends Binder {
public MyService getService() {
return MyService.this;
}
}
}
第二步
绑定服务是通过调用bindService()方法启动的服务。绑定服务允许应用组件绑定到服务,并与其进行交互。绑定服务会在所有客户端都解绑后自动停止
<service
android:name=".service.MyService"
android:enabled="true"
android:exported="true" />
第三步:
import androidx.appcompat.app.AppCompatActivity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
private MyService myService;
private ServiceConnection connection = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName className, IBinder service) {
MyService.LocalBinder binder = (MyService.LocalBinder) service;
myService = binder.getService();
}
@Override
public void onServiceDisconnected(ComponentName arg0) {
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button test01Btn = findViewById(R.id.test01Btn);
test01Btn.setOnClickListener((View v)->{
myService.performTask();
});
}
@Override
protected void onStart() {
super.onStart();
// Bind to MyService
Intent intent = new Intent(this, MyService.class);
bindService(intent, connection, Context.BIND_AUTO_CREATE);
}
@Override
protected void onStop() {
super.onStop();
if (myService != null) {
unbindService(connection);
}
}
}
Intent Service(意图服务)
IntentService是Service的子类,专门用于处理异步请求。它通过工作线程来处理所有的启动请求,并在所有请求处理完毕后自动停止。IntentService是一个简单的服务实现,它消除了处理线程的复杂性
第一步:
public class MyIntentService extends IntentService {
public MyIntentService() {
super("MyIntentService");
}
@Override
protected void onHandleIntent(Intent intent) {
// Task logic here
}
}