0

I have an EditView where the user is supposed to input a phone number. I then want to take that phone number and call/text it in another activity. How could I do that?

This is part of my Main Activity:

    EditText editText = (EditText) findViewById(R.id.editText1);
    editText.setOnClickListener(this);

  }
public void onClick(View src) {
        Intent serviceIntent = new Intent(this, MyService.class);
     switch (src.getId()) {
            case R.id.button1:
              Log.d(TAG, "onClick: starting service");
              EditText editText = (EditText) findViewById(R.id.editText1);
              if (editText.getText().toString()==""){
                  Toast.makeText(getApplicationContext(), "Please enter a  phone number", Toast.LENGTH_SHORT).show();
              } else {
                  pnumber = editText.getText().toString();
                  startService(serviceIntent);  
                  serviceIntent.putExtra(number, pnumber);

              break;
              }

            }
          }

        }

And then in my other Activity:

    Bundle extras = this.getIntent().getExtras(); {
    if(extras !=null)
{
    String newnumber = extras.getString(number,pnumber);
    Uri number = Uri.parse(newnumber);
    Intent callIntent = new Intent(Intent.ACTION_CALL, number);
    Intent textIntent = new Intent(Intent.ACTION_SENDTO, number);
}
}

public void onClick(View view) {

    switch (view.getId()) {
    case R.id.button2:
      //Log.d(TAG, "onClick: starting call");
      startActivity(callIntent);
      break;
    case R.id.button3:
        //Log.d(TAG, "onClick: start text message");
        startActivity(textIntent);
        break;
        }       
        }
     }

The Uri keeps parsing "newnumber" and giving a phone number made from the numbers that have those letters (For example, I would get 639686237 since n is on the 6 key, e is on the 3 key etc.) It also doesn't recognize the variables number and pnumber.

user2502058
  • 45
  • 1
  • 1
  • 4

3 Answers3

1

You can send your data in two activities like that:

Intent intent = new Intent(CurrentActivity.this, MyService.class);
intent.putExtra("number", pnumber);
startActivity(intent);

And you can get this value in MyService.class:

Intent intent = getIntent();
String yourNumber = intent.getStringExtra("number");

Or your can put your values in bundle:

In your current activity:

Bundle bundle = new Bundle();
bundle.putString("number", pnumber);
bundle.putStirng("name", "yourNameValue");
bundle.putInt("age", "ageValue");
Intent intent = new Intent(CurrentActivity.this, MyService.class);
intent.putExtras(bundle);
startActivity(intent);

And in your MyService activity:

Bundle bundle = getIntent().getExtras();
String number = bundle.getString("number");
String name = bundle.getString("name");
int age = bundle.getInt("age");

I hope that helps you.

Hugo Mallet
  • 200
  • 7
0

Due to intent.putExtra(String name, Bundle extra) statement in first activity, to get the pnumber in second activity we requires the string name. There is no scope of number (name in your case) and pnumber variables in the second class.

So instantiate a variable having the value of number in the second class ValueOfNumber, this will serve as the key to get pnumber from the intent.

Use

pnumber=extras.getString(ValueOfNumber); //ValueOfNumber is the value of number variable in first activity.

Also call startService after Intent.putExtras.

Refer to This answer

Community
  • 1
  • 1
Lohit
  • 126
  • 8
-1

Used SharedPreferences. There'a nice thread here for it. With it, you can save the number in a Preference file (visible only for the current application and not in the file system). Like this one:

private final String PREFS_NAME  = "savefile";
private final String KEY_NUMBER    = "num";
String strWithTheNumber = "00 123 456 789"
String strSavedValue = "";
SharedPreferences sharedPreferences = ctx.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE);

//To put the phone in the file

SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString(KEY_NUMBER, strWithTheNumber);
editor.commit();

//To retrieve the number

strSavedValue = sharedPreferences.getString("num", null);
Community
  • 1
  • 1
g00dy
  • 6,752
  • 2
  • 30
  • 43