【问题标题】:Disable Reoplening Activitiy Mulitple Times on Map Long Clicks在地图长按上多次禁用重新打开活动
【发布时间】:2017-11-28 20:18:13
【问题描述】:

我正在制作一个应用程序,使用户能够长按地图,并打开一个新活动,允许他们添加新标签和有关它的信息。当用户长按一次时,如果他的速度足够快,他可以长按两次地图,第二个活动会打开两次。我正在尝试找到一种方法来禁用此行为。我已经尝试了一些示例,并尝试添加标志,但没有效果。

我想禁止用户长按两次。我还想添加一个加载器。

简而言之,我要做的是:如果用户已经长按打开新活动,则禁用长按,并在新活动关闭时再次启用

我的地图片段如下所示:

 //Add marker on long click
 mMap.setOnMapLongClickListener(new GoogleMap.OnMapLongClickListener() {

     @Override
     public void onMapLongClick(final LatLng arg0) {

         RequestQueue queue = Volley.newRequestQueue(getActivity());
                    String url = "https://maps.googleapis.com/maps/api/geocode/json?latlng=" + String.valueOf(arg0.latitude) + "," + String.valueOf(arg0.longitude) + "&key=myKey";

         // Request a string response from the provided URL.
         StringRequest stringRequest = new StringRequest(Request.Method.GET, url, new Response.Listener<String>() {
             @Override
             public void onResponse(String response) {
                 try {
                     JSONArray jObj = new JSONObject(response).getJSONArray("results").getJSONObject(0).getJSONArray("address_components");

                     Intent intent = new Intent(getActivity(), AddRestaurantActivity.class);

                      for (int i = 0; i < jObj.length(); i++) {
                          String componentName = new JSONObject(jObj.getString(i)).getJSONArray("types").getString(0);
                          if (componentName.equals("postal_code") || componentName.equals("locality") || componentName.equals("street_number") || componentName.equals("route")
                                                    || componentName.equals("neighborhood") || componentName.equals("sublocality") || componentName.equals("administrative_area_level_2")
                                                    || componentName.equals("administrative_area_level_1") || componentName.equals("country")) {
                                                intent.putExtra(componentName, new JSONObject(jObj.getString(i)).getString("short_name"));
                           }
                      }

                       intent.putExtra("latitude", arg0.latitude);
                       intent.putExtra("longitude", arg0.longitude);

                       startActivity(intent);

                   } catch (JSONException e) {
              e.printStackTrace();
              }
          }
    }, new Response.ErrorListener() {

    @Override
    public void onErrorResponse(VolleyError error) {
        int x = 1;
    }
 });
 // Add the request to the RequestQueue.
 queue.add(stringRequest);

    }
});

这是它打开的活动,thisthis 的答案尝试(以及其他)添加一个标志:

private void setRestaurant(final String userId, final String message, final String pickDate, final String pickTime, final String location, final String lat, final String lon, final String sendTo, final boolean enableComments) {
    // Tag used to cancel the request
    String tag_string_req = "req_add_restaurant";

    final String commentsEnabled = (enableComments) ? "0" : "1";

    pDialog.setMessage(getString(R.string.setting_a_restaurant));
    showDialog();

    ApiInterface apiService =
            ApiClient.getClient().create(ApiInterface.class);

    Call<DefaultResponse> call = apiService.addrestaurant(userId, message, lat, lon, pickDate, pickTime, sendTo, commentsEnabled);
    call.enqueue(new Callback<DefaultResponse>() {
        @Override
        public void onResponse(Call<DefaultResponse> call, retrofit2.Response<DefaultResponse> response) {

            // Launch main activity
            Intent intent = new Intent(SetRestaurantActivity.this,
                    MainActivity.class);
            // I TRIED TO BLOCK IT HERE
            intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
            // I ALSO TRIED: 
            // intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
            startActivity(intent);
            finish();

            Toast.makeText(getApplicationContext(), R.string.sucessfully_created_restaurant, Toast.LENGTH_LONG).show();
        }
    });
}

【问题讨论】:

    标签: java android android-intent google-maps-api-3 long-click


    【解决方案1】:

    简单的添加标志变量。在这种情况下,我为此使用 isRequestProcess 变量。

    Boolean isRequestProcess = false;
    mMap.setOnMapLongClickListener(new GoogleMap.OnMapLongClickListener() {
    
     @Override
     public void onMapLongClick(final LatLng arg0) {
         if(isRequestProcess){
             return;
         }
        isRequestProcess = true;
         RequestQueue queue = Volley.newRequestQueue(getActivity());
                    String url = "https://maps.googleapis.com/maps/api/geocode/json?latlng=" + String.valueOf(arg0.latitude) + "," + String.valueOf(arg0.longitude) + "&key=myKey";
    
         // Request a string response from the provided URL.
         StringRequest stringRequest = new StringRequest(Request.Method.GET, url, new Response.Listener<String>() {
             @Override
             public void onResponse(String response) {
                 try {
                     JSONArray jObj = new JSONObject(response).getJSONArray("results").getJSONObject(0).getJSONArray("address_components");
    
                     Intent intent = new Intent(getActivity(), AddRestaurantActivity.class);
    
                      for (int i = 0; i < jObj.length(); i++) {
                          String componentName = new JSONObject(jObj.getString(i)).getJSONArray("types").getString(0);
                          if (componentName.equals("postal_code") || componentName.equals("locality") || componentName.equals("street_number") || componentName.equals("route")
                                                    || componentName.equals("neighborhood") || componentName.equals("sublocality") || componentName.equals("administrative_area_level_2")
                                                    || componentName.equals("administrative_area_level_1") || componentName.equals("country")) {
                                                intent.putExtra(componentName, new JSONObject(jObj.getString(i)).getString("short_name"));
                           }
                      }
    
                       intent.putExtra("latitude", arg0.latitude);
                       intent.putExtra("longitude", arg0.longitude);
    
                       startActivity(intent);
                       isRequestProcess = false;
    
                   } catch (JSONException e) {
                      e.printStackTrace();
                  }
            }, new Response.ErrorListener() {
    
                @Override
                public void onErrorResponse(VolleyError error) {
                    int x = 1;
                }
    
            }
        }
    }
    

    【讨论】:

    • 感谢您的回答萨赫布。这在一定程度上起作用。它禁用了同时打开的两个活动,但是当我长按两次时-它会打开新活动一次,但是当我关闭它时,它会再次打开...
    • 我使用 ProgressDialog 而不是 isRequestProcess,它现在可以工作了:ProgressDialog pd = ProgressDialog.show(this,"","Loading. Please wait...",true); //download file pd.cancel();
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-04
    • 2018-05-12
    • 2019-03-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多