在地图上长按禁用多次重新打开活动
Disable Reoplening Activitiy Mulitple Times on Map Long Clicks
我正在制作一个应用程序,使用户可以长按地图,然后打开一个新的 activity,这样他们就可以添加新的标签和相关信息。当用户长按一次时,如果速度够快可以长按两次地图,第二个activity会打开两次。我正在尝试找到一种方法来禁用此行为。我已经尝试了一些示例,并尝试添加标志,但没有效果。
我想禁止用户长按两次。我还想添加一个装载机。
简而言之,我想做的是:如果用户已经长按打开新activity,则禁用长按,并在新activity时再次启用已关闭.
我的地图片段如下所示:
//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);
}
});
这是它打开的 activity,this and this 答案尝试(除其他外)添加一个标志:
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();
}
});
}
简单的添加标志变量。在这种情况下,我为此使用 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;
}
}
}
}
我正在制作一个应用程序,使用户可以长按地图,然后打开一个新的 activity,这样他们就可以添加新的标签和相关信息。当用户长按一次时,如果速度够快可以长按两次地图,第二个activity会打开两次。我正在尝试找到一种方法来禁用此行为。我已经尝试了一些示例,并尝试添加标志,但没有效果。
我想禁止用户长按两次。我还想添加一个装载机。
简而言之,我想做的是:如果用户已经长按打开新activity,则禁用长按,并在新activity时再次启用已关闭.
我的地图片段如下所示:
//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);
}
});
这是它打开的 activity,this and this 答案尝试(除其他外)添加一个标志:
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();
}
});
}
简单的添加标志变量。在这种情况下,我为此使用 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;
}
}
}
}