首页 > 解决方案 > 将摇动应用程序作为项目的短信

问题描述

我创建了一个代码,当用户摇动手机时,手机会自动将消息发送到指定的号码,但我想从用户拥有的联系人中获取一个号码。我开始活动以获得结果,但我不知道将用户选择的联系人放入短信管理器。这是我的整个代码,请帮助我从用户那里获取电话号码并将其放入短信管理器电话字段

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

        }
    }
}

标签: javaandroidlocationsms

解决方案


推荐阅读