So this is my situation...
I have a Spinner with Parking Zones... Zone 1, Zone 2 etc.
What I want to do: when user clicks a Button "Send SMS Message", the Button should load a Parking Zone phone number into an SMS message. The Parking Zone number is set depending on the Parking Zone selection from the Spinner.
I completely edited my initial question... So I have...
Spinner with Parking Zones:
final Spinner spinner_zona = (Spinner) findViewById(R.id.spinnerZona);
ArrayAdapter<CharSequence> adapter2 = ArrayAdapter.createFromResource(
this, R.array.zone, android.R.layout.simple_spinner_item);
adapter2.setDropDownViewResource(
android.R.layout.simple_spinner_dropdown_item);
spinner_zona.setAdapter(adapter2);
spinnerPrefs with Parking zones phone numbers:
<resources>
<string-array name="spinnerZone_postavke">
<item>700101</item>
<item>700102</item>
<item>700103</item>
<item>700105</item>
<item>700104</item>
</string-array>
Storing and retrieving phone numbers in SharedPreferences:
public static boolean setPreferences(Context c, String key, String value) {
SharedPreferences settings = c.getSharedPreferences("com.example.igor.parkingzagreb.Preferences", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = settings.edit();
editor.putString(key, value);
return editor.commit();
}
public static String getPreference(Context c, String key) {
SharedPreferences settings = c.getSharedPreferences("com.example.igor.parkingzagreb.Preferences", Context.MODE_PRIVATE);
String value = settings.getString(key, "");
return value;
Am I on the right track? What is the next step in order to push those Phone numbers to an SMS message via a Button click?
Many thanks.
In your Button
s onClick
method, you can implement the SmsManager
, like below.
You can either use the SmsManager API:
SmsManager smsManager = SmsManager.getDefault();
smsManager.sendTextMessage("phoneNumber", null, "message", null, null);
Or, you could use the built in SMS application:
Intent sendIntent = new Intent(Intent.ACTION_VIEW);
sendIntent.putExtra("sms_body", "default content");
sendIntent.setType("vnd.android-dir/mms-sms");
startActivity(sendIntent);
Using the SmsManager API approache requires a permission:
<uses-permission android:name="android.permission.SEND_SMS" />
Example using SmsManager
in your Button
s onClick
method:
@Override
public void onClick(View v) {
//Fetch your selected zone from the spinner
String zone = spinner_zona.getSelectedItem().toString();
//Set the receipt phone number here, e.g. from SharedPreferences
String phoneNo = "yourPhoneNumber"
try {
SmsManager smsManager = SmsManager.getDefault();
smsManager.sendTextMessage(phoneNo, null, zone, null, null);
Toast.makeText(getApplicationContext(), "SMS Sent!",
Toast.LENGTH_LONG).show();
} catch (Exception e) {
Toast.makeText(getApplicationContext(),
"SMS faild, please try again later!",
Toast.LENGTH_LONG).show();
e.printStackTrace();
}
}
Find more details here.