0

i have created a code where when user shakes phone then phone automatically sends message to the specified number, but i want to pick up a number from contacts that user has. i started activity for result but i dont know to to put the contact picked by user into the sms manager.here is my whole code please help me getting phone number from user and put it in sms managers phone field

@BindView(R.id.textViewSensor)
TextView txtSensor;

@BindView(R.id.buttonActivate)
Button btnActivate;

@BindView(R.id.textViewLocation)
TextView txtLocation;

@BindView(R.id.buttonContacts)
Button btnContacts;

SensorManager sensorManager;
Sensor sensor;
LocationManager locationManager;

static final int PICK_CONTACT=1;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    ButterKnife.bind(this);

    btnActivate.setOnClickListener(this);
    btnContacts.setOnClickListener(this);

    sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
    sensor = sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
    locationManager= (LocationManager) getSystemService(LOCATION_SERVICE);
}

@Override
public void onClick(View v) {
    int id = v.getId();
    if (id == R.id.buttonContacts){
        Intent contacts = new Intent(Intent.ACTION_PICK);
        contacts.setType(ContactsContract.Contacts.CONTENT_TYPE);
        startActivityForResult(contacts,PICK_CONTACT);
    }

    sensorManager.registerListener(this, sensor, SensorManager.SENSOR_DELAY_NORMAL);


    if (id == R.id.buttonActivate) {
    }
}

@Override
public void  onSensorChanged(SensorEvent event){

    float[] values = event.values;

    float x = values[0];
    float y = values[1];
    float z = values[2];

    float cal = ((x * x) + (y * y) + (z * z)) / (SensorManager.GRAVITY_EARTH * SensorManager.GRAVITY_EARTH);

    if (cal > 30) {

        txtSensor.setText("Device Shaken at Coordinates =  " + x + " : " + y + " : " + z);
        sensorManager.unregisterListener(this);


        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,"Please Grant Permissions to access Location in Settings",Toast.LENGTH_LONG).show();
        }else {
            locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 10, 10, this);
        }

    } else {
        txtSensor.setText("Coordinates: " + x + " : " + y + " : " + z);
        txtLocation.setText("Location will be displayed when device is shaken");
    }
}

@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
}

@Override
protected void onDestroy() {
    super.onDestroy();
    sensorManager.unregisterListener(this);
    locationManager.removeUpdates(this);
}

@Override
public void onLocationChanged(Location location) {
    double latitude = location.getLatitude();
    double longitude = location.getLongitude();

    txtLocation.setText("Location is: " + latitude + " , " + longitude);

    try {
        Geocoder geocoder = new Geocoder(this);
        List<Address> addresses = geocoder.getFromLocation(latitude, longitude, 5);

        StringBuffer completeAddress = new StringBuffer();

        if (addresses != null && addresses.size() > 0) {
            Address address = addresses.get(0);
            for (int i = 0; i <= address.getMaxAddressLineIndex(); i++) {
                completeAddress.append(address.getAddressLine(i) + "\n");
            }

            txtLocation.setText("Location is: " + latitude + " , " + longitude + "\n" + completeAddress.toString());
        }


        Toast.makeText(this, "Message has been sent", Toast.LENGTH_LONG).show();
        SmsManager smsManager = SmsManager.getDefault();
        String message = "THIS IS AN EMERGENCY! \nLOCATION IS:http://maps.google.com/maps?saddr=" + latitude + "," + longitude;
        String phone = "7986816504";
        smsManager.sendTextMessage(phone, null, message, null, null);
        locationManager.removeUpdates(this);

    }catch (Exception e){
        e.printStackTrace();
    }
}

@Override
public void onStatusChanged(String s, int i, Bundle bundle) {
}

@Override
public void onProviderEnabled(String s) {
}

@Override
public void onProviderDisabled(String s) {
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == PICK_CONTACT) {

        if (resultCode == RESULT_OK) {

            Uri contactUri = data.getData();

            String[] projection = {ContactsContract.CommonDataKinds.Phone.NUMBER};

            Cursor cursor = getContentResolver()
                    .query(contactUri, projection, null, null, null);
            cursor.moveToFirst();


            int column = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);
            String number = cursor.getString(column);

        }
    }
}
WuzerHaun
  • 35
  • 5

0 Answers0