Android Service extends ResultReceiver for IntentService,如何实现CREATOR?
Android Service extends ResultReceiver for IntentService, how to implement CREATOR?
我的应用依赖于 Service
,它在后台与外部硬件保持同步。因为该服务在 main 线程上运行,所以它使用 IntentService 异步执行任何繁重的工作。这是解释控制流的代码的最小化示例:
public class MyService extends Service {
private final Handler handler = new Handler();
void someMethodDoesCallBarAsynchronously(){
Intent i = new Intent(this, MyIntentService.class);
i.putAction(ACTION_FOO);
i.putExtra(EXTRA_RECEIVER, new MyResultReceiver(handler));
startService(toGetScannerStatus);
}
void receivedFooResult(String bar){
//...
}
public class MyResultReceiver extends ResultReceiver{
public MyResultReceiver(Handler handler){
super(handler);
}
@Override
protected void onReceiveResult(int resultCode, Bundle resultData) {
super.onReceiveResult(resultCode, resultData);
switch(resultCode){
case RESULT_CODE_FOO:
receivedFooResult(resultData.getString(FOO_RESULT));
break;
}
}
}
}
public class MyIntentService extends IntentService {
public MyIntentService(){super("MyIntentService");}
@Override
protected void onHandleIntent(Intent intent) {
switch (intent.getAction()) {
case ACTION_FOO:
ResultReceiver receiver = intent.getParcelableExtra(EXTRA_RECEIVER);
Bundle bundle = new Bundle()
bundle.putString(FOO_RESULT, foo());
receiver.send(RESULT_CODE_FOO, b);
break;
}
}
现在,经过半年和一些更新,Android Studio 抱怨 MyResultReceiver
实现了 Parcelable 但不提供 CREATOR 字段。问题一,MyResultReceiver是一个内层的class,所以我得把它放到一个自己的class中。我不能使它 static
因为它必须包含对 MyService 的引用。那还是很简单的:
public class MyService extends Service {
private final Handler handler = new Handler();
void someMethodDoesCallBarAsynchronously(){
Intent i = new Intent(this, MyIntentService.class);
i.putAction(ACTION_FOO);
i.putExtra(EXTRA_RECEIVER, new MyResultReceiver(this, handler));
startService(toGetScannerStatus);
}
void receivedFooResult(String bar){
//...
}
}
public class MyResultReceiver extends ResultReceiver{
private final MyService myService;
public MyResultReceiver(MyService s, Handler handler){
super(handler);
this.myService = myService;
}
@Override
protected void onReceiveResult(int resultCode, Bundle resultData) {
super.onReceiveResult(resultCode, resultData);
switch(resultCode){
case RESULT_CODE_FOO:
myService.receivedFooResult(resultData.getString(FOO_RESULT));
break;
}
}
}
现在我可以添加一个 public 静态字段。但是如何正确实现CREATOR呢?
public class MyResultReceiver extends ResultReceiver {
public static final Parcelable.Creator<MyResultReceiver> CREATOR
= new Parcelable.Creator<MyResultReceiver>() {
public MyResultReceiver[] newArray(int size) {
return new MyResultReceiver[size];
}
public MyResultReceiver createFromParcel(Parcel in) {
// ???
}
};
如果您想:为什么这是必要的?它会用来做什么?
恕我直言,您当前的架构太复杂了:您可以轻松地在一个 Service
中做到这一点,但是当涉及到您的问题时,这取决于您:您不需要创建任何 CREATOR
因为你得到的是一个 lint 警告,只需像这样添加 SuppressLint 注释:
@SuppressLint("ParcelCreator")
public class MyResultReceiver extends ResultReceiver {
我的应用依赖于 Service
,它在后台与外部硬件保持同步。因为该服务在 main 线程上运行,所以它使用 IntentService 异步执行任何繁重的工作。这是解释控制流的代码的最小化示例:
public class MyService extends Service {
private final Handler handler = new Handler();
void someMethodDoesCallBarAsynchronously(){
Intent i = new Intent(this, MyIntentService.class);
i.putAction(ACTION_FOO);
i.putExtra(EXTRA_RECEIVER, new MyResultReceiver(handler));
startService(toGetScannerStatus);
}
void receivedFooResult(String bar){
//...
}
public class MyResultReceiver extends ResultReceiver{
public MyResultReceiver(Handler handler){
super(handler);
}
@Override
protected void onReceiveResult(int resultCode, Bundle resultData) {
super.onReceiveResult(resultCode, resultData);
switch(resultCode){
case RESULT_CODE_FOO:
receivedFooResult(resultData.getString(FOO_RESULT));
break;
}
}
}
}
public class MyIntentService extends IntentService {
public MyIntentService(){super("MyIntentService");}
@Override
protected void onHandleIntent(Intent intent) {
switch (intent.getAction()) {
case ACTION_FOO:
ResultReceiver receiver = intent.getParcelableExtra(EXTRA_RECEIVER);
Bundle bundle = new Bundle()
bundle.putString(FOO_RESULT, foo());
receiver.send(RESULT_CODE_FOO, b);
break;
}
}
现在,经过半年和一些更新,Android Studio 抱怨 MyResultReceiver
实现了 Parcelable 但不提供 CREATOR 字段。问题一,MyResultReceiver是一个内层的class,所以我得把它放到一个自己的class中。我不能使它 static
因为它必须包含对 MyService 的引用。那还是很简单的:
public class MyService extends Service {
private final Handler handler = new Handler();
void someMethodDoesCallBarAsynchronously(){
Intent i = new Intent(this, MyIntentService.class);
i.putAction(ACTION_FOO);
i.putExtra(EXTRA_RECEIVER, new MyResultReceiver(this, handler));
startService(toGetScannerStatus);
}
void receivedFooResult(String bar){
//...
}
}
public class MyResultReceiver extends ResultReceiver{
private final MyService myService;
public MyResultReceiver(MyService s, Handler handler){
super(handler);
this.myService = myService;
}
@Override
protected void onReceiveResult(int resultCode, Bundle resultData) {
super.onReceiveResult(resultCode, resultData);
switch(resultCode){
case RESULT_CODE_FOO:
myService.receivedFooResult(resultData.getString(FOO_RESULT));
break;
}
}
}
现在我可以添加一个 public 静态字段。但是如何正确实现CREATOR呢?
public class MyResultReceiver extends ResultReceiver {
public static final Parcelable.Creator<MyResultReceiver> CREATOR
= new Parcelable.Creator<MyResultReceiver>() {
public MyResultReceiver[] newArray(int size) {
return new MyResultReceiver[size];
}
public MyResultReceiver createFromParcel(Parcel in) {
// ???
}
};
如果您想:为什么这是必要的?它会用来做什么?
恕我直言,您当前的架构太复杂了:您可以轻松地在一个 Service
中做到这一点,但是当涉及到您的问题时,这取决于您:您不需要创建任何 CREATOR
因为你得到的是一个 lint 警告,只需像这样添加 SuppressLint 注释:
@SuppressLint("ParcelCreator")
public class MyResultReceiver extends ResultReceiver {