从通知启动主屏幕时出现 ANR

问题描述

我对 android 开发世界还很陌生,也很天真。我正在解决我们的 android 应用程序中的一个现有问题,到目前为止我还没有解决它。我很抱歉问了一个冗长的问题。

在当前用于跟踪店主是否开店或关店的应用中,我们显示一个通知用户可以点击该通知(当应用未在前台打开时)并从“值班”到“值班”。如果用户关闭了定位服务并且用户尝试点击从“Duty Off”到“Duty On”的通知,系统会尝试获取用户的纬度和经度,发出 POST 标注并打开应用程序主屏幕。当应用程序主屏幕打开时,用户看到应用程序无响应错误。这只发生在用户关闭他们的位置的情况下。

下面是我的代码片段,它是发生此操作时调用的类,它主要负责 2 个方法 createNotification 和 fun_DutyONOFF。自己在谷歌上搜索了很多,似乎如果 broadcastReceiver 在 10 秒内没有完成,Android 会抛出 Application Not Responding (ANR)。但我不确定这里的替代方案是什么,如果关闭定位服务,为什么会出现 ANR?任何帮助都可以挽救生命,因为我花了 3 天时间却没有运气。

public class SwitchButtonListenerON extends broadcastReceiver {
//Class variable declaration that I have omitted
@Override
    public void onReceive(Context context,Intent intent) {
        if ("dutyON".equalsIgnoreCase(intent.getAction())) {
            SharedPrefrence_Login.getDataLogin(context);
            location_update = new Location_Update(context);
            createNotification(context);
//This is probably the method "fun_DutyONOFF" that takes more time but I am not sure how to replace the work done by this method
            fun_DutyONOFF("1",SharedPrefrence_Login.getMhawker_code(),context);
        }
    }
private void createNotification(final Context context) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        mChannel = new NotificationChannel(CHANNEL_ID,"Hawker",notificationmanager.IMPORTANCE_HIGH);
    }
    Intent notificationIntent = new Intent(context,Home.class);
    PendingIntent pendingIntent = PendingIntent.getActivity(context,notificationIntent,0);

    Intent dutyIntent = new Intent("action.cancel.notification");
    PendingIntent pendingDutyIntent = PendingIntent.getbroadcast(context,dutyIntent,PendingIntent.FLAG_UPDATE_CURRENT);
    RemoteViews notificationView = new RemoteViews(context.getPackageName(),R.layout.notification_layout);
    //the intent that is started when the notification is clicked (works)
    Notification notification = new NotificationCompat.Builder(context,CHANNEL_ID)
            .setContentTitle("Location Service")
            .setContentText("")
            .setSmallIcon(R.drawable.ic_business)
            .setContentIntent(pendingIntent)
            .setongoing(true)
            .build();
    notification.contentView = notificationView;
    notification.contentIntent = pendingDutyIntent;
    notification.flags |= Notification.FLAG_NO_CLEAR;
    notificationView.setonClickPendingIntent(R.id.closeDuty,pendingDutyIntent);
    notificationmanager mnotificationmanager = (notificationmanager)context.getSystemService(Context.NOTIFICATION_SERVICE);
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        mnotificationmanager.createNotificationChannel(mChannel);
        mnotificationmanager.notify(1,notification);
    }else {
        mnotificationmanager.notify(1,notification);
    }
}

private void fun_DutyONOFF(final String sstatus,final String hawker_code,final  Context context) {
    requestQueue = VolleySingleton.getInstance(context).getRequestQueue();
    StringRequest stringRequest = new StringRequest(Request.Method.POST,Urls.URL_DUTY_ON_OFF_SELLER,new Response.Listener<String>() {
                @Override
                public void onResponse(String response) {
                    try {
                        // _progressDialog.dismiss();
                        // converting response to json object
                        JSONObject obj = new JSONObject(response);
                        String str = obj.getString("data");
                        JSONObject jsoObject = new JSONObject(str);
                        strStatus  = jsoObject.getString("status");
                        strActive_status = jsoObject.getString("active_status");
                        strActive_msg = jsoObject.getString("active_message");
                        if(strStatus.equals("1")){
                           // System.exit(0);
                            Intent intent = new Intent(context,Home.class);
                            intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK );
                            context.startActivity(intent);
                            createNotification(context);
                        }

                    } catch (JSONException e) {
                        e.printstacktrace();
                    }
                }
            },new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    //      _progressDialog.dismiss();
                    if (error.getClass().equals(TimeoutError.class)) {
                        CallbackSnakebarModel.getInstance().SnakebarMessage(context,"It took longer than expected to get the response from Server.",MessageConstant.toast_warning);
                    }else {
                        CallbackSnakebarModel.getInstance().SnakebarMessage(context,"Server Respond Error! Try Again Later",MessageConstant.toast_warning);
                    }      }
            }) {
        @Override
        protected Map<String,String> getParams() throws AuthFailureError {
            Map<String,String> params = new HashMap<>();
            params.put("hawker_code",hawker_code);
            params.put("longitude",location_update.LONGITUDE);
            params.put("latitude",location_update.LATTITUDE);
            params.put("duty_status",sstatus);
            params.put("notification_id",SharedPrefrence_Login.getPnotification_id());
            return params;
        }
    };
    stringRequest.setRetryPolicy(new DefaultRetryPolicy(20000,DefaultRetryPolicy.DEFAULT_MAX_RETRIES,DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
    VolleySingleton.getInstance(context).addToRequestQue(stringRequest);

}

}

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)