我为分配编写了这个应用程序,但出于某种原因,其中两个问题只会运行一次,并且只有在安装了应用程序之后才会运行。
e.g。如果我安装应用程序并运行问题1a,那么它将完美地工作。如果我在此之后尝试运行问题2,或者再次运行问题1a,除非我卸载并重新安装apk,否则它将无效。它也可以反过来,所以如果我先运行问题2,那么问题1a将无效,如果我再试一次,问题2就无法运行。
我没有任何错误或崩溃,它只是没有做任何事情。这很奇怪,因为问题1b一直都很完美,除了使用Google Map之外,它与问题1a和问题2非常相似。
编辑:我刚刚意识到问题1b中的地图不再起作用(谷歌地图API授权失败)但它确实在TextView中正确更新了地址,现在只要我能弄明白为什么TextView在问题中更新1b而不是其他两个。
非常感谢任何帮助!
代码如下:
问题1a:
public class Question1a extends FragmentActivity implements
GoogleApiClient.ConnectionCallbacks,
GoogleApiClient.OnConnectionFailedListener,
LocationListener
{
private TextView txtAddress;
private GoogleApiClient client;
private LocationRequest locationRequest;
public static final int REQUEST_MULTIPLE_PERMISSIONS_CODE = 99;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_question1a);
txtAddress = (TextView) findViewById(R.id.addressEdit);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)
{
checkPermissions();
}
}
//creates a location request object and sets the location request intervals
@Override
public void onConnected(@Nullable Bundle bundle)
{
locationRequest = new LocationRequest();
locationRequest.setInterval(1000);
locationRequest.setFastestInterval(1000);
locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED)
{
LocationServices.FusedLocationApi.requestLocationUpdates(client, locationRequest, this);
}
LocationServices.FusedLocationApi.requestLocationUpdates(client, locationRequest, this);
}
@Override
public void onConnectionSuspended(int i)
{
}
@Override
public void onConnectionFailed(@NonNull ConnectionResult connectionResult)
{
}
//Gets address and displays it in a TextView
@Override
public void onLocationChanged(Location location)
{
Geocoder geocoder = new Geocoder(this, Locale.getDefault());
List<Address> addresses = null;
try
{
addresses = geocoder.getFromLocation(location.getLatitude(), location.getLongitude(), 1);
}
catch (IOException e)
{
e.printStackTrace();
}
if(addresses == null)
{
Toast.makeText(this, "No address found", Toast.LENGTH_SHORT).show();
}
else
{
Address address = addresses.get(0);
String finalAddress = "Address: ";
for (int i = 0; i <= address.getMaxAddressLineIndex(); i++)
{
finalAddress = finalAddress + address.getAddressLine(i);
}
txtAddress.setText(finalAddress);
}
}
//build the API client
protected synchronized void buildGoogleApiClient()
{
client = new GoogleApiClient.Builder(this).addConnectionCallbacks(this).addOnConnectionFailedListener(this).addApi(LocationServices.API).build();
client.connect();
}
//Checks if the app has the required permissions to run (Location permissions)
public boolean checkPermissions()
{
if(ContextCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ContextCompat.checkSelfPermission(this, Manifest.permission.SEND_SMS) != PackageManager.PERMISSION_GRANTED)
{
if(ActivityCompat.shouldShowRequestPermissionRationale(this, android.Manifest.permission.ACCESS_FINE_LOCATION))
{
ActivityCompat.requestPermissions(this, new String[]{android.Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.SEND_SMS}, REQUEST_MULTIPLE_PERMISSIONS_CODE);
}
else
{
ActivityCompat.requestPermissions(this, new String[]{android.Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.SEND_SMS}, REQUEST_MULTIPLE_PERMISSIONS_CODE);
}
return false;
}
else
{
return true;
}
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults)
{
switch (requestCode)
{
case REQUEST_MULTIPLE_PERMISSIONS_CODE:
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED)
{
//permission has been granted
if (ContextCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED && ContextCompat.checkSelfPermission(this, Manifest.permission.SEND_SMS) == PackageManager.PERMISSION_GRANTED)
{
//if client is null(there is no Google API Client) this calls the method to build the Google API Client
if (client == null)
{
buildGoogleApiClient();
}
}
}
else
{
Toast.makeText(this, "Permissions were denied", Toast.LENGTH_SHORT).show();
}
}
}
}
问题1b:
public class Question1b extends FragmentActivity implements OnMapReadyCallback, GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener, LocationListener
{
private TextView txtAddress;
private GoogleMap mMap;
private GoogleApiClient client;
private LocationRequest locationRequest;
private Location lastLocation;
private Marker currentLocationMarker;
public static final int REQUEST_MULTIPLE_PERMISSIONS_CODE = 99;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_question1b);
txtAddress = (TextView) findViewById(R.id.locationAddressEdit);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)
{
checkPermissions();
}
// Obtain the SupportMapFragment and get notified when the map is ready to be used.
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults)
{
switch (requestCode)
{
case REQUEST_MULTIPLE_PERMISSIONS_CODE:
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED)
{
//permission has been granted
if (ContextCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED && ContextCompat.checkSelfPermission(this, Manifest.permission.SEND_SMS) == PackageManager.PERMISSION_GRANTED)
{
//if client is null(there is no Google API Client) this calls the method to build the Google API Client
if (client == null)
{
buildGoogleApiClient();
}
}
}
else
{
Toast.makeText(this, "Permissions were denied", Toast.LENGTH_SHORT).show();
}
}
}
//if the app has the right permissions then it will build the API client and enable location services when the map is ready
@Override
public void onMapReady(GoogleMap googleMap)
{
mMap = googleMap;
mMap.setMapType(GoogleMap.MAP_TYPE_SATELLITE);
if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED)
{
buildGoogleApiClient();
mMap.setMyLocationEnabled(true);
}
}
//creates a location request object and sets the location request intervals
@Override
public void onConnected(@Nullable Bundle bundle)
{
locationRequest = new LocationRequest();
locationRequest.setInterval(1000);
locationRequest.setFastestInterval(1000);
locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED)
{
LocationServices.FusedLocationApi.requestLocationUpdates(client, locationRequest, this);
}
LocationServices.FusedLocationApi.requestLocationUpdates(client, locationRequest, this);
}
@Override
public void onConnectionSuspended(int i)
{
}
@Override
public void onConnectionFailed(@NonNull ConnectionResult connectionResult)
{
}
//Moves the marker when location changes
@Override
public void onLocationChanged(Location location)
{
lastLocation = location;
if (currentLocationMarker != null)
{
currentLocationMarker.remove();
}
LatLng latLng = new LatLng(location.getLatitude(), location.getLongitude());
MarkerOptions markerOptions = new MarkerOptions();
markerOptions.position(latLng);
markerOptions.title("Current Location");
markerOptions.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_GREEN));
currentLocationMarker = mMap.addMarker(markerOptions);
mMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
Geocoder geocoder = new Geocoder(this, Locale.getDefault());
List<Address> addresses = null;
try
{
addresses = geocoder.getFromLocation(location.getLatitude(), location.getLongitude(), 1);
}
catch (IOException e)
{
e.printStackTrace();
}
if(addresses == null)
{
Toast.makeText(this, "No address found", Toast.LENGTH_SHORT).show();
}
else
{
Address address = addresses.get(0);
String finalAddress = "Address: ";
for (int i = 0; i <= address.getMaxAddressLineIndex(); i++)
{
finalAddress = finalAddress + address.getAddressLine(i);
}
txtAddress.setText(finalAddress);
}
}
//Checks if the app has the required permissions to run (Location permissions & SMS permissions)
public boolean checkPermissions()
{
if(ContextCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ContextCompat.checkSelfPermission(this, Manifest.permission.SEND_SMS) != PackageManager.PERMISSION_GRANTED)
{
if(ActivityCompat.shouldShowRequestPermissionRationale(this, android.Manifest.permission.ACCESS_FINE_LOCATION))
{
ActivityCompat.requestPermissions(this, new String[]{android.Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.SEND_SMS}, REQUEST_MULTIPLE_PERMISSIONS_CODE);
}
else
{
ActivityCompat.requestPermissions(this, new String[]{android.Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.SEND_SMS}, REQUEST_MULTIPLE_PERMISSIONS_CODE);
}
return false;
}
else
{
return true;
}
}
//build the API client
protected synchronized void buildGoogleApiClient()
{
client = new GoogleApiClient.Builder(this).addConnectionCallbacks(this).addOnConnectionFailedListener(this).addApi(LocationServices.API).build();
client.connect();
}
}
问题2:
public class Question2 extends FragmentActivity implements GoogleApiClient.ConnectionCallbacks,
GoogleApiClient.OnConnectionFailedListener,
LocationListener
{
private TextView txtAddress;
private GoogleApiClient client;
private LocationRequest locationRequest;
public static final int REQUEST_MULTIPLE_PERMISSIONS_CODE = 99;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_question2);
txtAddress = (TextView) findViewById(R.id.addressEdit);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)
{
checkPermissions();
}
}
//creates a location request object and sets the location request intervals
@Override
public void onConnected(@Nullable Bundle bundle)
{
locationRequest = new LocationRequest();
locationRequest.setInterval(1000);
locationRequest.setFastestInterval(1000);
locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED)
{
LocationServices.FusedLocationApi.requestLocationUpdates(client, locationRequest, this);
}
LocationServices.FusedLocationApi.requestLocationUpdates(client, locationRequest, this);
}
@Override
public void onConnectionSuspended(int i)
{
}
@Override
public void onConnectionFailed(@NonNull ConnectionResult connectionResult)
{
}
//Gets address using geocoder and displays it in a TextView
@Override
public void onLocationChanged(Location location)
{
Geocoder geocoder = new Geocoder(this, Locale.getDefault());
List<Address> addresses = null;
try
{
addresses = geocoder.getFromLocation(location.getLatitude(), location.getLongitude(), 1);
}
catch (IOException e)
{
e.printStackTrace();
}
if(addresses == null)
{
Toast.makeText(this, "No address found", Toast.LENGTH_SHORT).show();
}
else
{
Address address = addresses.get(0);
String finalAddress = "Address: ";
for (int i = 0; i <= address.getMaxAddressLineIndex(); i++)
{
finalAddress = finalAddress + address.getAddressLine(i);
}
txtAddress.setText(finalAddress);
}
}
//build the API client
protected synchronized void buildGoogleApiClient()
{
client = new GoogleApiClient.Builder(this).addConnectionCallbacks(this).addOnConnectionFailedListener(this).addApi(LocationServices.API).build();
client.connect();
}
//Checks if the app has the required permissions to run (Location permissions & SMS permissions)
public boolean checkPermissions()
{
if(ContextCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ContextCompat.checkSelfPermission(this, Manifest.permission.SEND_SMS) != PackageManager.PERMISSION_GRANTED)
{
if(ActivityCompat.shouldShowRequestPermissionRationale(this, android.Manifest.permission.ACCESS_FINE_LOCATION))
{
ActivityCompat.requestPermissions(this, new String[]{android.Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.SEND_SMS}, REQUEST_MULTIPLE_PERMISSIONS_CODE);
}
else
{
ActivityCompat.requestPermissions(this, new String[]{android.Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.SEND_SMS}, REQUEST_MULTIPLE_PERMISSIONS_CODE);
}
return false;
}
else
{
return true;
}
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults)
{
switch (requestCode)
{
case REQUEST_MULTIPLE_PERMISSIONS_CODE:
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED)
{
//permission has been granted
if (ContextCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED && ContextCompat.checkSelfPermission(this, Manifest.permission.SEND_SMS) == PackageManager.PERMISSION_GRANTED)
{
//if client is null(there is no Google API Client) this calls the method to build the Google API Client
if (client == null)
{
buildGoogleApiClient();
}
}
}
else
{
Toast.makeText(this, "Permissions were denied", Toast.LENGTH_SHORT).show();
}
}
}
public void sendMyLocation(View v)
{
SmsManager smsManager = SmsManager.getDefault();
smsManager.sendTextMessage("+27718831284", null, "Your location is: " + txtAddress.getText(), null, null);
}
}