最近公司有需求 service要以内部类方式使用特此记录
代码:
public class OutService extends Service {
public OutService() {
Log.d("OutService","OutService()");
}
@Override
public IBinder onBind(Intent intent) {
// TODO: Return the communication channel to the service.
Log.d("OutService","onBind(Intent intent)");
throw new UnsupportedOperationException("Not yet implemented");
}
@Override
public void onCreate() {
super.onCreate();
Log.d("OutService","onCreate()");
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.d("OutService","onStartCommand(Intent intent, int flags, int startId)");
startService(new Intent(this,InnerService.class));
return super.onStartCommand(intent, flags, startId);
}
/**
* 内部 Service
*/
public static class InnerService extends Service {
public InnerService() {
Log.d("InnerService","InnerService()");
}
@Override
public IBinder onBind(Intent intent) {
// TODO: Return the communication channel to the service.
Log.d("InnerService","onBind(Intent intent)");
throw new UnsupportedOperationException("Not yet implemented");
}
@Override
public void onCreate() {
super.onCreate();
Log.d("InnerService","onCreate()");
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.d("InnerService","onStartCommand(Intent intent, int flags, int startId)");
return super.onStartCommand(intent, flags, startId);
}
}
}
清单文件:
<service
android:name=".service.OutService"
android:enabled="true"
android:exported="true"/>
<service android:name=".service.OutService$InnerService" />