1/*
2 * Copyright (C) 2016 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package androidx.lifecycle.service;
18
19import android.content.Context;
20import android.content.Intent;
21import android.os.Binder;
22import android.os.IBinder;
23
24import androidx.annotation.Nullable;
25import androidx.lifecycle.Lifecycle;
26import androidx.lifecycle.LifecycleObserver;
27import androidx.lifecycle.LifecycleOwner;
28import androidx.lifecycle.LifecycleService;
29import androidx.lifecycle.OnLifecycleEvent;
30import androidx.localbroadcastmanager.content.LocalBroadcastManager;
31
32public class TestService extends LifecycleService {
33
34    public static final String ACTION_LOG_EVENT = "ACTION_LOG_EVENT";
35    public static final String EXTRA_KEY_EVENT = "EXTRA_KEY_EVENT";
36
37    private final IBinder mBinder = new Binder();
38
39    public TestService() {
40        getLifecycle().addObserver(new LifecycleObserver() {
41            @OnLifecycleEvent(Lifecycle.Event.ON_ANY)
42            public void anyEvent(LifecycleOwner owner, Lifecycle.Event event) {
43                Context context = (TestService) owner;
44                Intent intent = new Intent(ACTION_LOG_EVENT);
45                intent.putExtra(EXTRA_KEY_EVENT, event);
46                LocalBroadcastManager.getInstance(context).sendBroadcast(intent);
47            }
48        });
49    }
50
51    @Nullable
52    @Override
53    public IBinder onBind(Intent intent) {
54        super.onBind(intent);
55        return mBinder;
56    }
57}
58