-1

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();
        }
    }
}
James Z
  • 12,209
  • 10
  • 24
  • 44
  • `PackageManager.PERMISSION_GRANTED` type is `int`. You can't obviously add an `int` to a `String[]`. Convert it – Imaguest Jan 24 '20 at 10:06
  • @Imaguest i did as it suggested to do so, dut then it marks whole line as error – SANJAY KASHYAP Jan 24 '20 at 10:09
  • I don't understand what you've wrote. If you have another error after adding something to convert your `int` into a `String`, then fix it or tell us what is the error message. – Imaguest Jan 24 '20 at 10:18
  • https://github.com/Sanjay9kashyap/location/blob/master/locationn – SANJAY KASHYAP Jan 24 '20 at 10:29
  • Wooo my friend! Take a deep breath. If you want to add some informations about your errors, edit your post instead of spaming the comments section. Did you try to do the changes suggested by Bruno as an answer? – Imaguest Jan 24 '20 at 10:33
  • Ok i've looked for `ActivityCompat.requestPermissions` javadoc. It looks like it takes three parameters. So I see where you missed. `ActivityCompat.requestPermissions(this, new String [] {Manifest.permission.SEND_SMS}, PackageManager.PERMISSION_GRANTED);` `ActivityCompat.requestPermissions(this, new String [] {Manifest.permission.INTERNET}, PackageManager.PERMISSION_GRANTED);` -- replace the two lines with errors with this. You've included the `int` in the `String[]` but it has to be the third param. But I'm not an android expert, you should read the Bruno answer. – Imaguest Jan 24 '20 at 10:41
  • @Imaguest bro, now it shows following error: Cannot fit requested classes in a single dex file (# methods: 76697 > 65536) – SANJAY KASHYAP Jan 24 '20 at 10:52
  • Try to look [here](https://stackoverflow.com/questions/48249633/errorcannot-fit-requested-classes-in-a-single-dex-file-try-supplying-a-main-dex). But please as your initial problem has been resolved, you need to create a new post to explain new troubles. – Imaguest Jan 24 '20 at 10:57

2 Answers2

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);
        }
    }
}
Bruno
  • 3,872
  • 4
  • 20
  • 37
0

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