我正在尝试从广播接收器中发出 API 请求,有人可以提及正确的做法吗?

huangapple 未分类评论46阅读模式
英文:

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(&quot;result&quot;) == &quot;ok&quot;) {
          
            JsonObject jsonObject = new JsonObject();
            jsonObject.addProperty(Web.Register.User_Id, sharedPrefHelper.getUserId());
            jsonObject.addProperty(Web.BookingRequest.ACTIVITY, &quot;ok&quot;);
            new BasePresenter&lt;&gt;().createApiRequest(BaseApplication.getRetrofit().create(ApisHelper.class)
                    .okResponse(jsonObject), new BaseCallBack&lt;BasicApiModel&gt;() {
                @Override
                public void onCallBack(BasicApiModel output) {
                    if (Log.isLoggable(&quot;qwert&quot;, Log.DEBUG)) {
                        Log.d(&quot;qwert&quot;, &quot;Receiver&#39;s onCallBack: &quot; + output.getMessage());
                    }
                }
            });
        } else {
            
             Log.d(&quot;qwert&quot;, &quot;onReceive: testing_cancelled&quot;);
        }
    }
}

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.

huangapple
  • 本文由 发表于 2020年4月10日 15:55:51
  • 转载请务必保留本文链接:https://java.coder-hub.com/61136160.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定