I have a MainActivity that display a Map with CurrentLocation and a Button that should call for Help.
Then I have a ContactView where I can load a contact and a message in TextView.
What I would like to do is this: When I press the help button in my MainActivity I would like to call the contact and the message from ContactView and send sms on this click.
Now I dont know how and where to implement the onClick because in my ContactView I am already using an onClick for importing the Contact.
this is my MainActivity:
public class MainActivity extends AppCompatActivity {
private TextView message;
Button sendsms;
// GoogleMap class
private GoogleMap googleMap;
@Override
public boolean onCreateOptionsMenu(Menu menu)
{
MenuInflater inflater= getMenuInflater();
inflater.inflate(R.menu.contact_menu, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item){
int id = item.getItemId();
if(id == R.id.action_contactView)
{
Intent ContactIntent = new Intent(this, ContactView.class);
startActivity(ContactIntent);
}
return true;
}
//zooms to currentLocation when the Position is obtained.
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getSupportActionBar();
message = (TextView) findViewById(R.id.message);
String msg=message.getText().toString();
sendsms = (Button)findViewById(R.id.helpButton);
googleMap = ((MapFragment)getFragmentManager().findFragmentById(R.id.map)).getMap();
googleMap.setMyLocationEnabled(true);
GoogleMap.OnMyLocationChangeListener myLocationChangeListener = new GoogleMap.OnMyLocationChangeListener() {
@Override
public void onMyLocationChange (Location location) {
LatLng loc = new LatLng (location.getLatitude(), location.getLongitude());
googleMap.animateCamera(CameraUpdateFactory.newLatLngZoom(loc, 16.0f));
}
};
googleMap.setOnMyLocationChangeListener(myLocationChangeListener);
// verify we can interact with the Google Map{
try {
if (googleMap == null) {
googleMap = ((MapFragment) getFragmentManager().
findFragmentById(R.id.map)).getMap();
}
// Show a satellite map with roads
/* MAP_TYPE_NORMAL: Basic map with roads.
MAP_TYPE_SATELLITE: Satellite view with roads.
MAP_TYPE_TERRAIN: Terrain view without roads.
*/
googleMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
// Enables indoor maps
googleMap.setIndoorEnabled(true);
// Show Zoom buttons
googleMap.getUiSettings().setZoomControlsEnabled(true);
} catch (Exception e) {
e.printStackTrace();
}
}
public void onClick(View v) {
SmsManager sms = SmsManager.getDefault();
sms.sendTextMessage(phoneNo,null,msg,null,null);
}
I already started to create and onClick in my MainActivity for sending the SMS. But the phoneNo and msg from ContactView can't be used in there.
This is my ContactView:
public class ContactView extends AppCompatActivity {
private static final int RESULT_PICK_CONTACT = 85;
private TextView textView1;
private TextView textView2;
private TextView message;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_contact_view);
textView1 = (TextView) findViewById(R.id.TxtName);
textView2 = (TextView) findViewById(R.id.TxtNumber);
message = (TextView) findViewById(R.id.message);
//speicher den aktuellen status der activity
SharedPreferences settings = getSharedPreferences("SelectedContact", MODE_PRIVATE);
String name = settings.getString("contactName", "");
//the second parameter set a default data if “contactName” is empty
if (!name.isEmpty()){
textView1.setText(name);
}
String phoneNo = settings.getString("contactPhone", "");//the second parameter set a default data if “contactName” is empty
if (!phoneNo.isEmpty()){
textView2.setText(phoneNo);
}
}
public void onClick(View v) {
Intent contactPickerIntent = new Intent(Intent.ACTION_PICK,
ContactsContract.CommonDataKinds.Phone.CONTENT_URI);
startActivityForResult(contactPickerIntent, RESULT_PICK_CONTACT);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// check whether the result is ok
if (resultCode == RESULT_OK) {
// Check for the request code, we might be usign multiple startActivityForReslut
switch (requestCode) {
case RESULT_PICK_CONTACT:
contactPicked(data);
break;
}
} else {
Log.e("ContactView", "Failed to pick contact");
}
}
/**
* Query the Uri and read contact details. Handle the picked contact data.
*
* @param data
*/
private void contactPicked(Intent data) {
Cursor cursor = null;
try {
String phoneNo = null;
String name = null;
String msg=message.getText().toString();
//String ResqMessage = null;
// getData() method will have the Content Uri of the selected contact
Uri uri = data.getData();
//Query the content uri
cursor = getContentResolver().query(uri, null, null, null, null);
cursor.moveToFirst();
// column index of the phone number
int phoneIndex = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);
// column index of the contact name
int nameIndex = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME);
phoneNo = cursor.getString(phoneIndex);
name = cursor.getString(nameIndex);
// Set the value to the textviews
textView1.setText(name);
textView2.setText(phoneNo);
SharedPreferences settings = getSharedPreferences("SelectedContact", MODE_PRIVATE);
SharedPreferences.Editor editor = settings.edit();
editor.putString("contactName", name);
editor.putString("contactPhone", phoneNo);
editor.commit();
} catch (Exception e) {
e.printStackTrace();
}
}