• Introducing XDA Computing: Discussion zones for Hardware, Software, and more!    Check it out!
  • Fill out your device list and let everyone know which phones you have!    Edit Your Device Inventory

How can I run a command from user-selected choice (array)

Search This thread

nex7er

Senior Member
Feb 3, 2008
449
163
Bergen
A user can select an item from my view to get a dialog showing different strings. Below that is a button. I would like the button, when pressed, to run a shell command. I have no idea how I would go about doing this.

Basically, how can I make the button have a command set to it, which when a user selects something in the dialog popup, runs the command + the string in the chosen view?

The class:

Code:
public static class DataSyncPreferenceFragment extends PreferenceFragment {
	  [user=439709]@override[/user]
		public void onCreate(Bundle savedInstanceState) {
			super.onCreate(savedInstanceState);
			addPreferencesFromResource(R.xml.pref_data_sync);

			// Bind the summaries of EditText/List/Dialog/Ringtone preferences
			// to their values. When their values change, their summaries are
			// updated to reflect the new value, per the Android Design
			// guidelines.
			bindPreferenceSummaryToValue(findPreference("sync_frequency"));

		}

	}

The button
Code:
public void button1(View v) {
		switch (v.getId()) {
		case R.id.button1:
			try {
				Runtime.getRuntime().exec(new String[] { "su", "-c","HOW_DO_I_ADD_THINGS_HERE" });
			} catch (IOException e) {
				e.printStackTrace();
			}
			break;
		}
	}


My arrays look like this:

Code:
<string-array name="pref_sync_frequency_titles">
        <item>Normal</item>
        <item>Smaller</item>
        <item>Smallest</item>
    </string-array>
    <string-array name="pref_sync_frequency_values">
        <item>320</item>
        <item>280</item>
        <item>220</item>
    </string-array>

Any help would be appreciated. I'm too new to know these things, sadly. :crying:
 

alobo

Senior Member
Mar 20, 2012
163
217
Waterloo
www.oadigital.ca
Here's a possible solution to what you're trying to achieve. I've had to implement something like this in my apps before, and I've made use of this helpful StackOverflow question.


Code:
new AlertDialog.Builder(this.getActivity())
		//Add your titles array to the dialog (in single choice mode)
        .setSingleChoiceItems(R.array.pref_sync_frequency_titles, 0, null)
        .setPositiveButton(R.string.ok_button_label, new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int whichButton) {
                dialog.dismiss();
                int selectedPosition = ((AlertDialog)dialog).getListView().getCheckedItemPosition();
				
				//Get the corresponding values Array
				String[] strValues = getActivity().getResources().getStringArray(R.array.pref_sync_frequency_values);
				//Get the corresponding value that was clicked
				String yourValue = strValues[selectedPosition];
				
				try {
					Runtime.getRuntime().exec(new String[] { "su", "-c","HOW_DO_I_ADD_THINGS_HERE" + yourValue });
				} catch (IOException e) {
					e.printStackTrace();
				}
				
            }
        })
        .show();

Let me know how that works!