Add Settings to enter server URL
This commit is contained in:
parent
1dc5fc78f9
commit
82f38e0db9
@ -25,6 +25,7 @@
|
||||
<category android:name="android.intent.category.LAUNCHER" />
|
||||
</intent-filter>
|
||||
</activity>
|
||||
<activity android:name=".SettingsActivity"></activity>
|
||||
</application>
|
||||
|
||||
</manifest>
|
||||
|
@ -16,13 +16,16 @@ import android.app.Activity;
|
||||
import android.content.ActivityNotFoundException;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.content.SharedPreferences;
|
||||
import android.net.ConnectivityManager;
|
||||
import android.net.NetworkInfo;
|
||||
import android.os.AsyncTask;
|
||||
import android.os.Bundle;
|
||||
import android.preference.PreferenceManager;
|
||||
import android.speech.RecognizerIntent;
|
||||
import android.util.Log;
|
||||
import android.view.Menu;
|
||||
import android.view.MenuItem;
|
||||
import android.view.View;
|
||||
import android.widget.ImageButton;
|
||||
import android.widget.TextView;
|
||||
@ -33,9 +36,10 @@ public class MainActivity extends Activity {
|
||||
private TextView txtSpeechInput;
|
||||
private ImageButton btnSpeak;
|
||||
private final int REQ_CODE_SPEECH_INPUT = 100;
|
||||
private final int REQ_PREF = 200;
|
||||
public static final String TAG = MainActivity.class.getSimpleName();
|
||||
public static final String Url = "http://192.168.10.109:1234";
|
||||
public static final int MAX_ANSWER_LEN = 500;
|
||||
public static String Url;
|
||||
|
||||
@Override
|
||||
protected void onCreate(Bundle savedInstanceState) {
|
||||
@ -46,7 +50,7 @@ public class MainActivity extends Activity {
|
||||
btnSpeak = (ImageButton) findViewById(R.id.btnSpeak);
|
||||
|
||||
// hide the action bar
|
||||
getActionBar().hide();
|
||||
//getActionBar().hide();
|
||||
|
||||
btnSpeak.setOnClickListener(new View.OnClickListener() {
|
||||
|
||||
@ -56,7 +60,9 @@ public class MainActivity extends Activity {
|
||||
}
|
||||
});
|
||||
new SilentTalkToMeTask().execute("goodbye");
|
||||
|
||||
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
|
||||
Url = preferences.getString(SettingsActivity.KEY_PREF_SERVER_URL, "");
|
||||
Log.d(TAG, "Server url is " + Url);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -78,9 +84,6 @@ public class MainActivity extends Activity {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Receiving speech input
|
||||
* */
|
||||
@Override
|
||||
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
|
||||
super.onActivityResult(requestCode, resultCode, data);
|
||||
@ -104,6 +107,11 @@ public class MainActivity extends Activity {
|
||||
}
|
||||
break;
|
||||
}
|
||||
case REQ_PREF:{
|
||||
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
|
||||
Url = preferences.getString(SettingsActivity.KEY_PREF_SERVER_URL, "");
|
||||
}
|
||||
break;
|
||||
|
||||
}
|
||||
}
|
||||
@ -115,6 +123,16 @@ public class MainActivity extends Activity {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onOptionsItemSelected(MenuItem item) {
|
||||
switch (item.getItemId()) {
|
||||
case R.id.action_settings:
|
||||
startActivityForResult(new Intent(getBaseContext(), SettingsActivity.class), REQ_PREF);
|
||||
return true;
|
||||
}
|
||||
return super.onOptionsItemSelected(item);
|
||||
}
|
||||
|
||||
private class TalkToMeTask extends AsyncTask<String,Void, String>{
|
||||
|
||||
@Override
|
||||
@ -122,7 +140,7 @@ public class MainActivity extends Activity {
|
||||
try {
|
||||
return talkToServer(question[0]);
|
||||
} catch (IOException e){
|
||||
Log.d(TAG, "Coincoin "+e);
|
||||
Log.d(TAG, e.toString());
|
||||
return "Cannot connect to server";
|
||||
}
|
||||
}
|
||||
@ -140,7 +158,7 @@ public class MainActivity extends Activity {
|
||||
try {
|
||||
return talkToServer(question[0]);
|
||||
} catch (IOException e){
|
||||
Log.d(TAG, "Coincoin "+e);
|
||||
Log.d(TAG, e.toString());
|
||||
return "Cannot connect to server";
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,22 @@
|
||||
package info.androidhive.speechtotext;
|
||||
|
||||
import android.app.Activity;
|
||||
import android.os.Bundle;
|
||||
import android.preference.PreferenceFragment;
|
||||
|
||||
public class SettingsActivity extends Activity {
|
||||
final static String KEY_PREF_SERVER_URL = "pref_serverURL";
|
||||
@Override
|
||||
protected void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
|
||||
// Display the fragment as the main content.
|
||||
getFragmentManager().beginTransaction()
|
||||
.replace(android.R.id.content, new SettingsFragment())
|
||||
.commit();
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
@ -0,0 +1,13 @@
|
||||
package info.androidhive.speechtotext;
|
||||
|
||||
import android.os.Bundle;
|
||||
import android.preference.PreferenceFragment;
|
||||
|
||||
public class SettingsFragment extends PreferenceFragment {
|
||||
|
||||
@Override
|
||||
public void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
addPreferencesFromResource(R.xml.preference);
|
||||
}
|
||||
}
|
@ -7,5 +7,8 @@
|
||||
<string name="speech_prompt">Say something…</string>
|
||||
<string name="speech_not_supported">Sorry! Your device doesn\'t support speech input</string>
|
||||
<string name="tap_on_mic">Tap on mic to speak</string>
|
||||
<string name="server_url">Server URL</string>
|
||||
<string name="pref_localhostserver">http://127.0.0.1:1234</string>
|
||||
|
||||
|
||||
</resources>
|
||||
|
8
app/src/main/res/xml/preference.xml
Normal file
8
app/src/main/res/xml/preference.xml
Normal file
@ -0,0 +1,8 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
|
||||
<EditTextPreference
|
||||
android:key="pref_serverURL"
|
||||
android:title="@string/server_url"
|
||||
android:dialogTitle="@string/server_url"
|
||||
android:defaultValue="@string/pref_localhostserver"/>
|
||||
</PreferenceScreen>
|
Loading…
Reference in New Issue
Block a user