1 package jp
.gr
.java_conf
.ofnhwx
.olib
.base
;
3 import java
.lang
.reflect
.Method
;
4 import java
.util
.HashMap
;
7 import android
.app
.Notification
;
8 import android
.app
.Service
;
14 public abstract class BaseService
extends Service
{
16 private static final Class
<?
>[] setForegroundSignature
= new Class
[] { boolean.class };
17 private static final Class
<?
>[] startForegroundSignature
= new Class
[] { int.class, Notification
.class };
18 private static final Class
<?
>[] stopForegroundSignature
= new Class
[] { boolean.class };
20 private Method setForeground
;
21 private Method startForeground
;
22 private Method stopForeground
;
24 private Object
[] setForegroundArgs
= new Object
[1];
25 private Object
[] startForegroundArgs
= new Object
[2];
26 private Object
[] stopForegroundArgs
= new Object
[1];
28 private static Map
<String
, BaseService
> services
= new HashMap
<String
, BaseService
>();
29 private boolean isForeground
;
32 public void onCreate() {
35 startForeground
= getClass().getMethod("startForeground", startForegroundSignature
);
36 stopForeground
= getClass().getMethod("stopForeground" , stopForegroundSignature
);
37 } catch (Exception e
) {
38 startForeground
= null;
39 stopForeground
= null;
42 setForeground
= getClass().getMethod("setForeground", setForegroundSignature
);
43 } catch (Exception e
) {
46 services
.put(getClass().getName(), this);
50 public void onDestroy() {
52 services
.put(getClass().getName(), this);
56 * {@link #isForeground(Class)}を参照.
58 public boolean isForeground() {
59 return isForeground(getClass());
63 * {@link #startForegroundCompat(Class, int, Notification)}を参照.
65 public void startForegroundCompat(int id
, Notification notification
) {
66 startForegroundCompat(getClass(), id
, notification
);
70 * {@link #stopForegroundCompat(Class)}を参照.
72 public void stopForegroundCompat() {
73 stopForegroundCompat(getClass());
78 * @param clazz {@link OService#getClass()}
79 * @return true:サビースはフォアグラウンドに設定されている
81 public static final boolean isForeground(Class
<?
extends BaseService
> clazz
) {
82 BaseService service
= services
.get(clazz
.getName());
83 if (service
== null) {
86 return service
.isForeground
;
92 * @param clazz {@link OService#getClass()}
94 * @param notification 通知オブジェクト
96 public static final void startForegroundCompat(Class
<?
extends BaseService
> clazz
, int id
, Notification notification
) {
97 BaseService service
= services
.get(clazz
.getName());
98 if (service
== null || service
.isForeground
) {
102 if (service
.startForeground
!= null) {
103 service
.startForegroundArgs
[0] = Integer
.valueOf(id
);
104 service
.startForegroundArgs
[1] = notification
;
105 service
.startForeground
.invoke(service
, service
.startForegroundArgs
);
108 service
.setForegroundArgs
[0] = Boolean
.TRUE
;
109 service
.setForeground
.invoke(service
, service
.setForegroundArgs
);
110 } catch (Exception e
) {
113 service
.isForeground
= true;
118 * サービスのフォアグラウンド設定を解除する.
119 * @param clazz {@link OService#getClass()}
121 public static final void stopForegroundCompat(Class
<?
extends BaseService
> clazz
) {
122 BaseService service
= services
.get(clazz
.getName());
123 if (service
== null || !service
.isForeground
) {
127 if (service
.stopForeground
!= null) {
128 service
.stopForegroundArgs
[0] = Boolean
.TRUE
;
129 service
.stopForeground
.invoke(service
, service
.stopForegroundArgs
);
132 service
.setForegroundArgs
[0] = Boolean
.FALSE
;
133 service
.setForeground
.invoke(service
, service
.setForegroundArgs
);
134 } catch (Exception e
) {
137 service
.isForeground
= false;