[英]How do wait for a service to start on Android without using bindService
I know that I can wait for a service start by doing我知道我可以等待服务开始
bindService(intent, serviceConnection, Context.BIND_AUTO_CREATE);
where serviceConnection
implements a callback that is called when the service starts.其中
serviceConnection
实现了服务启动时调用的回调。 However, I need a service that is less killeable, that is, a service hat uses startForeground
.但是,我需要一个不易被杀死的服务,即使用
startForeground
的服务帽。 But this can only work if I start my service with startService
, which does not provide a way to pass a serviceConnection
.但这只有在我使用
startService
启动服务时才有效,它不提供传递serviceConnection
的方法。
How can I wait for an android service to start?如何等待 android 服务启动?
Why I need it to start?为什么我需要它来启动? Well, because I need to call things inside it by doing
service.method1();
好吧,因为我需要通过
service.method1();
来调用它里面的东西。 etc.等等
You can try to use both methods, first start the service with startForeground() and then use bindService() to wait for the service connection.可以尝试使用这两种方法,先用 startForeground() 启动服务,然后用 bindService() 等待服务连接。
The service starts in the foreground and you can use the service methods.服务在前台启动,您可以使用服务方法。
Starting Service:启动服务:
listenServiceIntent = new Intent(getApplicationContext(), ListenService.class);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
//Start in Foreground
ContextCompat.startForegroundService(this, listenServiceIntent);
if (connection != null) {
//Then bind service
bindService(listenServiceIntent, connection, Context.BIND_AUTO_CREATE);
}
}
else {
startService(listenServiceIntent);
if (connection != null) {
bindService(listenServiceIntent, connection, Context.BIND_AUTO_CREATE);
}
}
Waiting Connection:等待连接:
private ServiceConnection connection = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
Log.i(TAG, "onServiceConnected");
ListenService.LocalBinder binder = (ListenService.LocalBinder) service;
listenService = binder.getService();
//HERE you can use service methods
}
@Override
public void onServiceDisconnected(ComponentName name) {
Log.i(TAG, "onServiceDisconnected");
}
};
Create notificationChannel:创建通知频道:
public static void createNotificationChannel(Context context) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel serviceChannel = new NotificationChannel(
NOTIFICATION_CHANNEL,
NOTIFICATION_NAME,
NotificationManager.IMPORTANCE_HIGH
);
NotificationManager manager = context.getSystemService(NotificationManager.class);
if (manager != null) {
manager.createNotificationChannel(serviceChannel);
}
}
}
Push notification:推送通知:
public static Notification pushNotification(Context context, String notificationText, Class classNotification) {
Intent notificationIntent = new Intent(context, classNotification);
PendingIntent pendingIntent = PendingIntent.getActivity(context,
0, notificationIntent, 0);
return new NotificationCompat.Builder(context, NOTIFICATION_CHANNEL)
.setContentTitle(context.getString(R.string.notification_title))
.setContentText(notificationText)
.setContentIntent(pendingIntent)
.setSmallIcon(R.drawable.logosinfondo_mini)
.setColor(Color.MAGENTA)
.setAutoCancel(true)
.build();
}
In onCreate() inside your Service:在您的服务内的 onCreate() 中:
@Override
public void onCreate() {
super.onCreate();
Lib.createNotificationChannel(this);
startForeground(1, Lib.pushNotification(this, "Started", ListenActivity.class));
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.