我遇到问题,但我不知道为什么多次调用 switch case 。为此我正在上传完整档案。
在logcat中,此行已多次显示
03-01 17:23:31.861 10958-11008 / com.example.akki.assignment2 E / Surface:getSlotFromBufferLocked:unknown buffer:0xb40d3810
package com.example.akki.assignment2;
import android.Manifest;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.PackageManager;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.net.Uri;
import android.os.Bundle;
import android.provider.Settings;
import android.support.v4.app.ActivityCompat;
import android.support.v4.app.FragmentActivity;
import android.util.Log;
import android.widget.Toast;
import com.google.android.gms.appindexing.Action;
import com.google.android.gms.appindexing.AppIndex;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.location.LocationRequest;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;
import java.util.Timer;
import java.util.TimerTask;
public class MapsActivity extends FragmentActivity implements LocationListener, OnMapReadyCallback {
private static final String TAG = null;
LatLng myPosition;
int Request_Code_Ask_Location = 123;
Location location;
LocationManager locationManager;
double latitude = 0, longitude = 0;
AlertDialog.Builder dialog;
public static final String PREFS_NAME = "MyPrefsFile";
boolean isGPSEnabled;
boolean isNetworkEnabled;
/**
* ATTENTION: This was auto-generated to implement the App Indexing API.
* See https://g.co/AppIndexing/AndroidStudio for more information.
*/
private GoogleApiClient client;
/**
* ATTENTION: This was auto-generated to implement the App Indexing API.
* See https://g.co/AppIndexing/AndroidStudio for more information.
*/
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
setFinishOnTouchOutside(false);
//setSilent(silent);
// ATTENTION: This was auto-generated to implement the App Indexing API.
// See https://g.co/AppIndexing/AndroidStudio for more information.
client = new GoogleApiClient.Builder(this).addApi(AppIndex.API).build();
}
private void dialogbox() {
dialog = new AlertDialog.Builder(this);
dialog.setCancelable(false);
dialog.setTitle("My Address Information")
.setMessage("Would you like to see your address information?")
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
//mMap.clear();
Intent intent = new Intent(getApplicationContext(), LocationDetails.class);
Bundle bundle = new Bundle();
bundle.putDouble("latitude", latitude);
bundle.putDouble("longitude", longitude);
intent.putExtras(bundle);
// mMap.clear();
startActivity(intent);
}
})
.setNegativeButton("No", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
})
.show();
setFinishOnTouchOutside(false);
}
/**
* Manipulates the map once available.
* This callback is triggered when the map is ready to be used.
* This is where we can add markers or lines, add listeners or move the camera. In this case,
* we just add a marker near Sydney, Australia.
* If Google Play services is not installed on the device, the user will be prompted to install
* it inside the SupportMapFragment. This method will only be triggered once the user has
* installed Google Play services and returned to the app.
*/
@Override
public void onPause() {
super.onPause();
}
@Override
public void onResume() {
super.onResume();
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
}
@Override
public void onLocationChanged(Location location) {
LocationRequest mLocationRequest = new LocationRequest();
mLocationRequest.setInterval(10000);
mLocationRequest.setFastestInterval(10000);
mLocationRequest.setPriority(LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY);
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
@Override
public void onProviderEnabled(String provider) {
}
@Override
public void onProviderDisabled(String provider) {
}
@Override
public void onBackPressed() {
super.onBackPressed();
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
finish();
System.exit(0);
}
@Override
public void onRequestPermissionsResult(int requestcode, String[] permissions, int[] grantResults) {
Toast.makeText(this,"REquest: " + requestcode,Toast.LENGTH_SHORT).show();
switch (requestcode) {
case 0:
{
if (grantResults.length == 2 && grantResults[0] == PackageManager.PERMISSION_GRANTED && grantResults[1] == PackageManager.PERMISSION_GRANTED) {
// onResume();
} else if (grantResults.length > 0) {
//Toast.makeText(this, "In else", Toast.LENGTH_LONG).show();
// boolean shouldshow1 = ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.ACCESS_FINE_LOCATION);
boolean shouldshow = ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.ACCESS_COARSE_LOCATION);
if (shouldshow == false) {
RequestPermission();
/*Toast.makeText(this,"Go to App setting to enable permission or Press Device Back Button to Exit",Toast.LENGTH_LONG).show();*/
// onBackPressed();
} else {
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
finish();
System.exit(0);
}
// System.exit(0);
}
}
default:
Toast.makeText(this,"default case",Toast.LENGTH_SHORT).show();
}
// super.onRequestPermissionsResult(requestcode, permissions, grantResults);
}
private void RequestPermission() {
AlertDialog.Builder dialog2;
dialog2 = new AlertDialog.Builder(this);
dialog2.setCancelable(false);
Log.e("Error", "In rp");
Toast.makeText(this, "In reqestpermission", Toast.LENGTH_LONG).show();
dialog2.setTitle("Permission")
.setMessage("You need to allow acess to your location")
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
//mMap.clear();
Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS,
Uri.fromParts("package", getPackageName(), null));
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
}
})
.setNegativeButton("No", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
finish();
System.exit(0);
}
})
.show();
setFinishOnTouchOutside(false);
}
// return;
@Override
public void onMapReady(GoogleMap mMap) {
if (mMap == null)
Toast.makeText(this, "no map", Toast.LENGTH_LONG).show();
// mMap.clear();
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
Toast.makeText(this, "In checkself", Toast.LENGTH_LONG);
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_COARSE_LOCATION, Manifest.permission.ACCESS_FINE_LOCATION}, Request_Code_Ask_Location); //to ask permission 6.0
// TODO: Consider calling
// ActivityCompat#requestPermissions
// here to request the missing permissions, and then overriding
// public void onRequestPermissionsResult(int requestCode, String[] permissions,
// int[] grantResults)
// to handle the case where the user grants the permission. See the documentation
// for ActivityCompat#requestPermissions for more details.
return;
}
// mMap.setMyLocationEnabled(true);
locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
Criteria criteria = new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_FINE);
// getting GPS status
isGPSEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
// getting network status
isNetworkEnabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
if (!isGPSEnabled && !isNetworkEnabled) {
AlertDialog.Builder alertDialog = new AlertDialog.Builder(this);
alertDialog.setCancelable(false);
alertDialog.setTitle("GPS Is Off"); //To enable GPS
alertDialog.setMessage("Please Enable GPS for better Location");
alertDialog.setPositiveButton("YES", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Intent i = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
startActivity(i);
}
});
alertDialog.setNegativeButton("NO", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
});
alertDialog.show();
}
{
if (isNetworkEnabled) {
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 5000, 25f, this);
if (locationManager != null) {
location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
if (location != null) {
latitude = location.getLatitude();
longitude = location.getLongitude();
}
}
}
// if GPS Enabled get lat/long using GPS Services
if (isGPSEnabled) {
if (location == null) {
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 5000, 25f, this);
if (locationManager != null) {
location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if (location != null) {
latitude = location.getLatitude();
longitude = location.getLongitude();
}
}
}
}
}
/* String provider = locationManager.getBestProvider(criteria, true).toString();
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000L, 100.0f, this);
// locationManager.requestLocationUpdates(provider, 1000, 10f, this);
location = locationManager.getLastKnownLocation(provider);*/
//Toast.makeText(getApplicationContext(),provider,Toast.LENGTH_LONG).show();
if (location != null) {
onLocationChanged(location);
}
// latitude = location.getLatitude();
// longitude = location.getLongitude();
Timer t = new Timer(false); //To show dialogbox after required time
if (isGPSEnabled || isNetworkEnabled) {
t.schedule(new TimerTask() {
@Override
public void run() {
runOnUiThread(new Runnable() {
public void run() {
dialogbox();
}
});
}
}, 5000);
}
if (latitude != 0 && longitude != 0) {
myPosition = new LatLng(latitude, longitude);
mMap.addMarker(new MarkerOptions().position(myPosition));
//mMap.clear();
mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(latitude, longitude), 16));
}
}
答案 0 :(得分:2)
您需要在每个案例后添加break
@Override
public void onRequestPermissionsResult(int requestcode, String[] permissions, int[] grantResults) {
Toast.makeText(this,"REquest: " + requestcode,Toast.LENGTH_SHORT).show();
switch (requestcode) {
case 0: {
if (grantResults.length == 2 && grantResults[0] == PackageManager.PERMISSION_GRANTED && grantResults[1] == PackageManager.PERMISSION_GRANTED) {
// onResume();
} else if (grantResults.length > 0) {
//Toast.makeText(this, "In else", Toast.LENGTH_LONG).show();
// boolean shouldshow1 = ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.ACCESS_FINE_LOCATION);
boolean shouldshow = ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.ACCESS_COARSE_LOCATION);
if (shouldshow == false) {
RequestPermission();
/*Toast.makeText(this,"Go to App setting to enable permission or Press Device Back Button to Exit",Toast.LENGTH_LONG).show();*/
// onBackPressed();
} else {
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
finish();
System.exit(0);
}
// System.exit(0);
}
break;
}
default: {
Toast.makeText(this,"default case",Toast.LENGTH_SHORT).show();
break;
}
// super.onRequestPermissionsResult(requestcode, permissions, grantResults);
}
}