gpt4 book ai didi

Android Service为IntentService扩展ResultReceiver,CREATOR如何实现?

转载 作者:IT老高 更新时间:2023-10-28 23:30:52 25 4
gpt4 key购买 nike

我的应用依赖于在后台与外部硬件保持同步的 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 是一个内部类,所以我必须把它放到一个自己的类中。我无法将其设为 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 {

关于Android Service为IntentService扩展ResultReceiver,CREATOR如何实现?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33889229/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com