-3

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();
    }
}
KishuDroid
  • 5,411
  • 4
  • 30
  • 47
bfmv991
  • 109
  • 1
  • 12
  • Can you please let me know why are you using ContactPicked method? – KishuDroid Oct 28 '15 at 08:52
  • http://stackoverflow.com/questions/33254064/string-to-listview-android-phonecontacts – bfmv991 Oct 28 '15 at 08:58
  • Yeah but what purpose. As i saw you are getting name and number through preferences – KishuDroid Oct 28 '15 at 09:00
  • I thought that im just saving the two TextViews with the SharedPreference. I never used SharedPreference before. – bfmv991 Oct 28 '15 at 09:04
  • I think you need to first understand your own code that what you are actually doing and what you exactly want to do. – KishuDroid Oct 28 '15 at 09:06
  • Or you could just give me an advice and thats how i can learn it :) and btw thats not my Question becuase it works anyway, when i'm done i can optimize stuff and reduce code. – bfmv991 Oct 28 '15 at 09:07

1 Answers1

0

In your MainActivity, you are already sending an SMS in your onClick methdo via SMSManager. Below that you can add the code given in this link to make a phone call. How to make a phone call in android and come back to my activity when the call is done?

    EndCallListener callListener = new EndCallListener();
    TelephonyManager mTM = (TelephonyManager)this.getSystemService(Context.TELEPHONY_SERVICE);
    mTM.listen(callListener, PhoneStateListener.LISTEN_CALL_STATE);

    private class EndCallListener extends PhoneStateListener {
    @Override
    public void onCallStateChanged(int state, String incomingNumber) {
    if(TelephonyManager.CALL_STATE_RINGING == state) {
        Log.i(LOG_TAG, "RINGING, number: " + incomingNumber);
    }
    if(TelephonyManager.CALL_STATE_OFFHOOK == state) {
        //wait for phone to go offhook (probably set a boolean flag) so you know your app initiated the call.
        Log.i(LOG_TAG, "OFFHOOK");
    }
    if(TelephonyManager.CALL_STATE_IDLE == state) {
        //when this state occurs, and your flag is set, restart your app
        Log.i(LOG_TAG, "IDLE");
    }
}
}

And in your manifest.xml add the following permission.

<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
Community
  • 1
  • 1
Hadi
  • 520
  • 9
  • 21
  • i don't want to call anyone just send the text from textview in ContactView when i press the button in my MainActivity – bfmv991 Oct 28 '15 at 09:30
  • You said in your question you wanted to call the contact. That being said, easiest way is to save your text from textview in contact picker view in a shared preference. In OnClick method of your MainActivity you can obtain that shared preference and send the text in your sms. – Hadi Oct 28 '15 at 09:32