1

I made an application that saves a number with shared preferences, and a button that executes a call with that number.

I would like to save multiple inputs (numbers) and names, I know I need to do that with ArrayList.

how can I save the inputs, the name, and the number and then show to the user some spinner with the names, so when he picks the name the app will retrieve the number and will cal this number?

thank you!

public class MainActivity extends AppCompatActivity {

Button openButton = null;
ImageButton editButton = null;
int PERMISSION_ALL = 1;
String[] PERMISSIONS = {Manifest.permission.READ_CONTACTS, Manifest.permission.CALL_PHONE};
AlertDialog dialog;

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

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


    openButton = (Button) findViewById(R.id.button);
    editButton = (ImageButton) findViewById(R.id.imageButton);

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

            AlertDialog.Builder mBuilder = new AlertDialog.Builder(MainActivity.this);
            View mView = getLayoutInflater().inflate(R.layout.activity_save, null);
            final EditText number = (EditText) mView.findViewById(R.id.editText);
            Button saveButton = (Button) mView.findViewById(R.id.button2);

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

                   // Integer num = Integer.valueOf(number.getText().toString());
                    String num = number.getText().toString();
                    SharedPreferences.Editor editor = getSharedPreferences("userInfo",MODE_PRIVATE).edit();
                    editor.putString("Phone", String.valueOf(num));
                    editor.commit();

                    Toast.makeText(MainActivity.this, "Saved!", Toast.LENGTH_LONG).show();
                    dialog.dismiss();

                }
            });

            mBuilder.setView(mView);
            dialog = mBuilder.create();
            dialog.show();
        }
    });

    openButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            makeCall();
        }
    });


}
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;
}

public void makeCall(){
    SharedPreferences prefs = getSharedPreferences("userInfo", MODE_PRIVATE);
    String restoredText = prefs.getString("Phone", null);
    if (restoredText != null) {
        String name = prefs.getString("Phone", "");

        Intent call = new Intent(Intent.ACTION_CALL);
        call.setData(Uri.parse("tel:" + name));
        startActivity(call);

    }
    this.finishAffinity();
    System.exit(0);

}
}
Athul Antony NP
  • 43
  • 2
  • 12
Tsur Yohananov
  • 533
  • 1
  • 7
  • 17

1 Answers1

0

For saving multiple contacts, you can use the Gson library or a sqlite DB. I think Gson will do the same.

First, you must create a contact class including the fields name, number etc. whatever you want. Make the ArrayList of the object and save it to a Gson and make it a string, so you can store the string into SharedPreferences; and using the same Gson, you can retrieve the class object.

Refer to this link:
store and retrieve a class object in shared preference

Community
  • 1
  • 1
Athul Antony NP
  • 43
  • 2
  • 12