0

The app is getting an input number and has a button that makes the call. I want to save the input for later use and have tried used Shared preference.

public class MainActivity extends AppCompatActivity {

    Button myButton = null;
    EditText numberCall = null;
    int PERMISSION_ALL = 1;
    String[] PERMISSIONS = {Manifest.permission.READ_CONTACTS, Manifest.permission.CALL_PHONE};

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

The button for making the call:

    myButton = (Button) findViewById(R.id.button);

I want to save numberCall!

    numberCall = (EditText) findViewById(R.id.editText);

asking for permission

    if(!hasPermissions(this, PERMISSIONS)){
        ActivityCompat.requestPermissions(this, PERMISSIONS, PERMISSION_ALL);
    }

    myButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {


            OnCallBtnClicked();
        }
    });
}

making the call

private void OnCallBtnClicked() {

    Integer num = Integer.valueOf(numberCall.getText().toString());
    Intent call = new Intent(Intent.ACTION_CALL);
    call.setData(Uri.parse("tel:" +"0"+num));
    startActivity(call);

    }

public static boolean hasPermissions(Context context, String... permissions) {
    if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && context != null && permissions != null) {
        for (String permission : permissions) {
            if (ActivityCompat.checkSelfPermission(context, permission) != PackageManager.PERMISSION_GRANTED) {
                return false;
            }
        }
    }
    return true;
}
Shaido
  • 27,497
  • 23
  • 70
  • 73
Tsur Yohananov
  • 533
  • 1
  • 7
  • 17
  • What didn't work with SharedPreferences? Because what I understand is that you want to save a number and it will definitively be the way to go. – Nicolas May 09 '17 at 01:48
  • I want to save it so next time the user uses the app it will be already set. Could you help me? Maybe an example or how it should look? I tried like I said and nothing so far. (I am new in Android developing) – Tsur Yohananov May 09 '17 at 01:50
  • So when a call is made, save the number. When restarting app the number should already be in the EditText is that what you want? You can read about SharedPreferences [here](https://developer.android.com/training/basics/data-storage/shared-preferences.html), it will work for that purpose. – Nicolas May 09 '17 at 02:00

2 Answers2

0

Shared preference would be the easiest solution .change your OnCallBtnClicked method to this :

 private void OnCallBtnClicked() {
        Integer num = Integer.valueOf(numberCall.getText().toString());
        Intent call = new Intent(Intent.ACTION_CALL);
        call.setData(Uri.parse("tel:" +"0"+num));
        SharedPreferences.Editor editor = getSharedPreferences(MY_PREFS_NAME,MODE_PRIVATE).edit();
     editor.putString("Phone", "0"+num);

     editor.commit();

      startActivity(call);

        }

to get the value you save , try this :

SharedPreferences prefs = getSharedPreferences(MY_PREFS_NAME, MODE_PRIVATE); 
String restoredText = prefs.getString("Phone", null);
if (restoredText != null) {
  String phone= prefs.getString("Phone", "No value defined");//"No valuedefined" is the default value.

}

Source

Community
  • 1
  • 1
0

@TsurYohananov If you want to add database then it is easy to get value which was stored by user

Firebase database has the capability to provide this functionality. There is much more video on youtube to get a reference.

keyur patel
  • 99
  • 1
  • 7