How can I use the data of an activity in another activity?

38 Views Asked by At

I want to send an SMS with location to the contact that the user has been saved. But I don't know how to do it. I am totally new to Android Studio, so I don´t know how to write my code that it will work the way I want it. Please, can you help me to do that?

This is the code of sending SMS:

Button sendBtn;

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

    sendBtn = (Button) findViewById(R.id.btnSendSMS);
    sendBtn.setOnClickListener(new View.OnClickListener() {
        public void onClick(View view) {
            sendSMSMessage();
        }
    });

}

protected void sendSMSMessage() {
    if (ContextCompat.checkSelfPermission(this,
            Manifest.permission.SEND_SMS)
            != PackageManager.PERMISSION_GRANTED) {
        if (ActivityCompat.shouldShowRequestPermissionRationale(this,
                Manifest.permission.SEND_SMS)) {
            // fixme: show explanation
            // before requesting the permission again
            ActivityCompat.requestPermissions(this,
                    new String[]{Manifest.permission.SEND_SMS},
                    MY_PERMISSIONS_REQUEST_SEND_SMS);
        } else {
            ActivityCompat.requestPermissions(this,
                    new String[]{Manifest.permission.SEND_SMS},
                    MY_PERMISSIONS_REQUEST_SEND_SMS);
        }
    } else {
        sendSmsImpl();
    }
}
@Override
public void onRequestPermissionsResult(int requestCode,String permissions[], int[] grantResults) {
    switch (requestCode) {
        case MY_PERMISSIONS_REQUEST_SEND_SMS: {
            if (grantResults.length > 0
                    && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                sendSmsImpl();
            } else {
                // fixme: explain that it can't send SMS without the permission
                Toast.makeText(getApplicationContext(),
                        "SMS faild, please try again.", Toast.LENGTH_LONG).show();
                return;
            }
            // !!! NOTE: you still need break inside switch/case
            // even with curly braces
            break;
        }
    }
}

private void sendSmsImpl() {
    SmsManager smsManager = SmsManager.getDefault();
    smsManager.sendTextMessage("+212xxx", null, "Je suis en danger, voici ma localisation : https://www.google.com/maps/search/?api=1&query=<lat>,<lng>", null, null);
    //todo: use sentIntent argument of sendTextMessage to detect success/error
    Toast.makeText(getApplicationContext(),
            "SMS sent.", Toast.LENGTH_LONG).show();
}

And this the code of saving contact:

private int id;

private String phoneNo;

private String name;

// constructor
public ContactModel(int id, String name, String phoneNo) {
    this.id = id;
    this.phoneNo = validate(phoneNo);
    this.name = name;
}

// validate the phone number, and reformat is necessary
private String validate(String phone) {

    // creating StringBuilder for both the cases
    StringBuilder case1 = new StringBuilder("+212");
    StringBuilder case2 = new StringBuilder("");

    // check if the string already has a "+"
    if (phone.charAt(0) != '+') {
        for (int i = 0; i < phone.length(); i++) {
            // remove any spaces or "-"
            if (phone.charAt(i) != '-' && phone.charAt(i) != ' ') {
                case1.append(phone.charAt(i));
            }
        }
        return case1.toString();
    } else {
        for (int i = 0; i < phone.length(); i++) {
            // remove any spaces or "-"
            if (phone.charAt(i) != '-' || phone.charAt(i) != ' ') {
                case2.append(phone.charAt(i));
            }
        }
        return case2.toString();
    }

}

public String getPhoneNo() {
    return phoneNo;
}

public int getId() {
    return id;
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

}

0

There are 0 best solutions below