简体   繁体   English

如何在不使用 bindService 的情况下等待服务在 Android 上启动

[英]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.

相关问题 bindservice不会启动服务,onserviceconnect不会调用 - bindservice wont start the service and onserviceconnect wont call 使用AIDL时,Android bindService()返回false? - Android bindService() returns false when using AIDL? Android-调用bindservice但无法访问该服务中的任何方法 - Android - calling bindservice but not being able to access any methods in that service Android BroadcastReceiver等待服务启动还是将WakeLock传输到Service? - Android BroadcastReceiver wait for Service to start or transmit WakeLock to Service? 如何使用 android 中的 MVP 模式从我的交互器启动服务? - How do I start a service from my Interactor using the MVP pattern in android? android bindService()NullPointerException - android bindService() NullPointerException 如何在Android中的onCreate()中启动多个线程而不使用for循环启动线程(IllegalThreadStateException)? - How Do I start multiple threads in Android in onCreate() without getting Thread Already started (IllegalThreadStateException) using for loop? 如何等待线程在Android中启动另一个线程? - How to wait for a thread to start another one in Android? 如何在不锁定 UI 线程的情况下在 Android 中等待? - How to wait in Android without locking the UI thread? 当另一个服务依赖它时,我该如何等待服务 - how do i wait for service when another service depends on it
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM