error: incompatible types: int cannot be converted to String

余生长醉 提交于 2020-02-08 10:39:48

问题


In the below code it shows error in

ActivityCompat.requestPermissions(this, new String [] {Manifest.permission.SEND_SMS, PackageManager.PERMISSION_GRANTED});

and

ActivityCompat.requestPermissions(this, new String [] {Manifest.permission.INTERNET, PackageManager.PERMISSION_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();
        }
    }
}

回答1:


You are mixing Permissions ID and Permissions rights

Here is what you have to do instead :

public static final int MULTIPLE_PERMISSIONS = 100; // any value you want
requestPermissions(this, new String [] {Manifest.permission.SEND_SMS, Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION, Manifest.permission.INTERNET}, MULTIPLE_PERMISSIONS);

Then to check permissions :

@Override
public void onRequestPermissionsResult(int requestCode,
    String[] permissions, int[] grantResults) {
    if (requestCode == MULTIPLE_PERMISSIONS) {
        if (grantResults.length > 0) {
            // check here if all permissions have been granted
        } else {
            requestPermissions(this, new String[] {
                Manifest.permission.SEND_SMS, Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION, Manifest.permission.INTERNET
            }, MULTIPLE_PERMISSIONS);
        }
    }
}



回答2:


please replace your code when error occurred with this line and try again:

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);



来源:https://stackoverflow.com/questions/59894076/error-incompatible-types-int-cannot-be-converted-to-string

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!