英文:
I'm trying to hit API request from a broadcast receiver, can anyone mention correct way of doing it?
问题
public class GetReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
SharedPrefHelper sharedPrefHelper = new SharedPrefHelper(context);
NotificationManager manager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
manager.cancel(1);
if (intent != null) {
if ("ok".equals(intent.getStringExtra("result"))) {
JsonObject jsonObject = new JsonObject();
jsonObject.addProperty(Web.Register.User_Id, sharedPrefHelper.getUserId());
jsonObject.addProperty(Web.BookingRequest.ACTIVITY, "ok");
new BasePresenter<>().createApiRequest(BaseApplication.getRetrofit().create(ApisHelper.class)
.okResponse(jsonObject), new BaseCallBack<BasicApiModel>() {
@Override
public void onCallBack(BasicApiModel output) {
if (Log.isLoggable("qwert", Log.DEBUG)) {
Log.d("qwert", "Receiver's onCallBack: " + output.getMessage());
}
}
});
} else {
Log.d("qwert", "onReceive: testing_cancelled");
}
}
}
}
(Note: I've corrected the translation of the code comments and strings within the code. However, please keep in mind that some variables like "Web.Register.User_Id", "Web.BookingRequest.ACTIVITY", "Web.BookingRequest.ACTIVITY", "BasePresenter<>" might be placeholders or references to other parts of your codebase that are not included here.)
英文:
public class GetReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
SharedPrefHelper sharedPrefHelper = new SharedPrefHelper(context);
NotificationManager manager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
manager.cancel(1);
if (intent != null) {
if (intent.getStringExtra("result") == "ok") {
JsonObject jsonObject = new JsonObject();
jsonObject.addProperty(Web.Register.User_Id, sharedPrefHelper.getUserId());
jsonObject.addProperty(Web.BookingRequest.ACTIVITY, "ok");
new BasePresenter<>().createApiRequest(BaseApplication.getRetrofit().create(ApisHelper.class)
.okResponse(jsonObject), new BaseCallBack<BasicApiModel>() {
@Override
public void onCallBack(BasicApiModel output) {
if (Log.isLoggable("qwert", Log.DEBUG)) {
Log.d("qwert", "Receiver's onCallBack: " + output.getMessage());
}
}
});
} else {
Log.d("qwert", "onReceive: testing_cancelled");
}
}
}
what is the correct way of starting api request from receiver ?
答案1
得分: 0
不要在广播接收器中启动任何长时间运行的任务,否则如果onReceive方法运行时间超过10秒,您的应用可能会崩溃。最好从onReceive方法中触发一个服务,并将您的网络操作放在服务内部。此外,请记住根据您的需求使用意图服务或任何其他服务,配合工作线程来运行您的网络操作,以避免阻塞您的UI线程。
英文:
Don't start any long running task in your broadcast receiver, otherwise your app may crash if onReceive runs more than 10 sec.
It's better to trigger a service from onReceive method and put your network operation inside the service.
Also keep in mind to use intent service / any other service depending on your requirement, with a worker thread to run your network operation so that your UI thread doesn't get blocked.
专注分享java语言的经验与见解,让所有开发者获益!
评论