在下面的代码中,它显示了
activitycompat.requestpermissions(this,new string[]{manifest.permission.send\u sms,packagemanager.permission\u granted});
以及
activitycompat.requestpermissions(this,new string[]{manifest.permission.internet,packagemanager.permission\u granted});
package com.example.myapplication;
import androidx.core.app.ActivityCompat;
import androidx.fragment.app.FragmentActivity;
import android.Manifest;
import android.content.pm.PackageManager;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.telephony.SmsManager;
import java.lang.String;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;`
enter code here`
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;
public class MyLocationApp extends FragmentActivity implements OnMapReadyCallback {
private GoogleMap mMap;
private LocationManager locationManager;
private LocationListener locationListener;
private final long MIN_TIME = 1000;
private final long MIN_DIST = 1;
private LatLng latLng;@
Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my_location_app);
// 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);
ActivityCompat.requestPermissions(this, new String[] {
Manifest.permission.SEND_SMS, PackageManager.PERMISSION_GRANTED
});
ActivityCompat.requestPermissions(this, new String[] {
Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION
}, PackageManager.PERMISSION_GRANTED);
ActivityCompat.requestPermissions(this, new String[] {
Manifest.permission.INTERNET, PackageManager.PERMISSION_GRANTED
});
}
/**
* 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 onMapReady(GoogleMap googleMap) {
mMap = googleMap;
// Add a marker in Sydney and move the camera
LatLng sydney = new LatLng(-34, 151);
mMap.addMarker(new MarkerOptions()
.position(sydney)
.title("Marker in Sydney"));
mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));
locationListener = new LocationListener() {@
Override
public void onLocationChanged(Location location) {
try {
latLng = new LatLng(location.getLatitude(), location.getLongitude());
mMap.addMarker(new MarkerOptions()
.position(latLng)
.title("My Position"));
mMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
String phoneNumber = "55555";
String myLatitude = String.valueOf(location.getLatitude());
String myLongitude = String.valueOf(location.getLongitude());
String message = "HELP" + "Latitude =" + myLatitude + "Longitude =" + myLongitude;
SmsManager smsManager = SmsManager.getDefault();
smsManager.sendTextMessage(phoneNumber, null, message, null, null);
} catch (Exception e) {
e.printStackTrace();
}
}@
Override
public void onStatusChanged(String provider, int status, Bundle extras) {}@
Override
public void onProviderEnabled(String provider) {}@
Override
public void onProviderDisabled(String provider) {}
};
locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
try {
locationManager.requestLocationUpdates(locationManager.NETWORK_PROVIDER, MIN_TIME, MIN_DIST, locationListener);
locationManager.requestLocationUpdates(locationManager.GPS_PROVIDER, MIN_TIME, MIN_DIST, locationListener);
} catch (SecurityException e) {
e.printStackTrace();
}
}
}
2条答案
按热度按时间kjthegm61#
您正在混合权限id和权限权限
以下是您必须做的:
然后检查权限:
mnowg1ta2#
当此行出现错误时,请替换代码,然后重试:
activitycompat.requestpermissions(this,new string[]{manifest.permission.send\u sms},packagemanager.permission\u grated);
activitycompat.requestpermissions(this,new string[]{manifest.permission.access\u fine\u location,manifest.permission.access\u rough\u location},packagemanager.permission\u granted);
activitycompat.requestpermissions(this,new string[]{manifest.permission.internet},packagemanager.permission\u grated);