Hi How can I use Android Speech to text API to get a value out of onActivityResult and use it in other activities/methods?
Heres the example code
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
if(requestCode == check && resultCode == RESULT_OK){
String results1 = data.getStringExtra(RecognizerIntent.EXTRA_RESULTS);
EditText test = (EditText) findViewById(R.id.editText1);
test.setText(results1);
ArrayList<String> results = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
ListView lv = (ListView) findViewById(R.id.listView);
lv.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, results));
whatYouSaid = results.get(0); }
So yeah.... How can i get this string value of whatYouSaid from this method to be able to use it in ohter methods/classes?
Store it as a global static variable in your app.
Save this in Shared preferences.
If its just few more activities you can pass it through Intent;
SharedPreference:
starting from this line
.......
whatYouSaid = results.get(0);
Sharedpreferences sp = getSharedPreferences("UR_UNIQ_PREF_ID", Context.MODE_PRIVATE);
Editor editor = sp.edit();
editor.putString("WHATYOUSAID", whatyousaid);
editor.commit();
//Then start your another activity
//Then in your next activity
oncreate(){
......
Sharedpreferences sp = getSharedPreferences("UR_UNIQ_PREF_ID", Context.MODE_PRIVATE);
String whatyousaid = sp.getString("UR_UNIQ_PREF_ID","");
}
}
Related
I'm trying to save an Intent object to SharedPreferences and the result code from onActivityResult() so I can use it later. I have already tired to store it as a json using Gson, but there is data loss. I have also tried to save it as uri, but the Intent.toURi() method returns empty string or a string that is not complete. Is there any other way to save it than as a string ? The intent has extras and I need to save all of its data, not just some parts of it.
This is what I've tried for storing it as uri:
SharedPreferences settings = getSharedPreferences(PREFERENCES, 0);
SharedPreferences.Editor editor = settings.edit();
String uriString = data.toUri(0);
editor.putString("SavedIntentData", uriString);
editor.putInt("SavedResultCode", resultCode);
editor.apply();
and for restoring it:
SharedPreferences settings = getSharedPreferences(PREFERENCES, 0);
String intentData = settings.getString("SavedIntentData", null);
int resultCode = settings.getInt("SavedResultCode", 0);
if(resultCode !=0 && intentData != null) {
data = Intent.parseUri(intentData, 0);
}
And for json:
String intentData = new Gson().toJson(data);
to restore it:
data= new Gson().fromJson(intentData, Intent.class);
In my app urls are getting from the firebase database . I want to send these url to the another activity. how can i pass the url (Uri webpage) to the
viewer class.Please tell me how to do it.
Use intent in first activity like this:
Intent intent = new Intent(CurrentActivity.this, NextActivity.class);
intent.putExtra("key", value);
CurrentActivity.this.startActivity(intent);
And retrieve it on the second activity:
#Override
protected void onCreate(Bundle savedInstanceState) {
Intent intent = getIntent();
String value = intent.getStringExtra("key");
}
you can use this SharedPreferences
SharedPreferences sharedPref = getSharedPreferences("key",Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPref.edit();
editor.putString("key1","your uri");
editor.apply();
if you want get value in new ativity
SharedPreferences sharedPref = getSharedPreferences("key",Context.MODE_PRIVATE);
String uri=sharedPref.getString("key1","default value if value is null");
keys values must be same
You can also send String Array via intent from one activity to another.
In sender's activity, you can add URL string array into intent.
Intent intent = new Intent(this, MainActivity.class);
String[] urls = new String[] {
"https://google.com",
"https://stackoverflow.com"
};
intent.putExtra("urls", urls);
startActivity(intent);
In MainActivity (Receiver), you can get String array from intent.
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String[] urls = getIntent().getStringArrayExtra("urls");
}
}
Hope it will be helpful.
I wrote an android application which loads a form in webview and it allows user to scan a barcode and copy the result of the barcode in the clipboard. I want to paste that from clipboard to the webview using android's paste function. When I try to search for paste it just shows me to get the data from clipboard and put it in an EditText field, But that's not what I want. I want to use the stock paste function (similar to longpress the field -> paste) in my code. Thanks
public void clickScan(View view){
final Activity activity = this;
IntentIntegrator integrator = new IntentIntegrator(activity);
integrator.setDesiredBarcodeFormats(IntentIntegrator.ALL_CODE_TYPES);
integrator.setPrompt("Scan the serial number");
integrator.setCameraId(0);
integrator.setBeepEnabled(false);
integrator.setBarcodeImageEnabled(false);
integrator.initiateScan();
}
protected void onActivityResult(int requestCode, int resultCode, Intent data){
IntentResult result = IntentIntegrator.parseActivityResult(requestCode, resultCode, data);
if(result != null){
if(result.getContents() ==null){
Toast.makeText(getApplicationContext(), "Error Scanning the code. Please try again!", Toast.LENGTH_LONG).show();
}
else {
String input = result.getContents().toString();
((ClipboardManager)getSystemService(getApplicationContext().CLIPBOARD_SERVICE)).setText(input);
Toast.makeText(getApplicationContext(), input,Toast.LENGTH_LONG).show();
}
}
else{
super.onActivityResult(requestCode, resultCode, data);
}
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String str = "test";
WebView webView = (WebView) findViewById(R.id.webView);
webView.getSettings().setJavaScriptEnabled(true);
webView.setWebViewClient(new WebViewClient());
webView.loadUrl("https://docs.google.com/forms/d/e/1FAIpQLSesTx1IBlPZlN5RXzzauJWYxStHOqt7wH_z4lFe0JHQmKm91w/viewform?usp=sf_link");
}
}
Found an answer guys.
I used:-
webView.dispatchKeyEvent(new KeyEvent(KeyEvent.ACTION_DOWN,KeyEvent.KEYCODE_PASTE));
Thanks.
I want to pass info as shown in this picture screen description
I know that my code is working, becuase with one Intent it works perfectley. However, when I try to put two or more Intents it seems to get messed up. I also checked here to find a sutibale solution, but I don't think I can use it the same way. Thanks in advance
--update--
still dosen't work
i did this on my saving side
Intent saveIntent = new Intent(this, MainActivity.class);
SharedPreferences.Editor editor = getSharedPreferences(MY_PREFS_NAME, MODE_PRIVATE).edit();
editor.putString("time", displayTime.getText().toString());
editor.commit();
startActivity(saveIntent);
and this on my reciving side
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
String time = preferences.getString("time",null);
if (time != null)
getTime.setText(time);
Main Activity:
private Button createNewEvent;
private Button showMyEvents;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
createNewEvent = (Button) findViewById(R.id.bCreateNewEvent);
showMyEvents = (Button) findViewById(R.id.bShowMyEvents);
}
public void buttonOnClick(View v) {
switch (v.getId()) {
case R.id.bCreateNewEvent:
Intent createNewEventIntent = new Intent(this, CreateNewEventActivity.class);
startActivity(createNewEventIntent);
break;
case R.id.bShowMyEvents:
Intent myEventsIntent = new Intent(this,MyEventsActivity.class);
startActivity(myEventsIntent);
break;
}
}
}
screen 1 save button:
case R.id.bSaveNewEvent:
Intent putIntent = new Intent(getApplicationContext(),MyEventsActivity.class);
// String text = displayTime.getText().toString();
putIntent.putExtra("time",displayTime.getText().toString());
Intent saveIntent = new Intent(this, MainActivity.class);
startActivity(saveIntent);
startActivity(putIntent);
screen 2 getExtras
public class MyEventsActivity extends AppCompatActivity {
TextView getTime;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my_events);
getTime = (TextView) findViewById(R.id.tvGetTime);
Intent in = getIntent();
String name = in.getStringExtra("time");
getTime.setText(name);
/* Bundle extras = getIntent().getExtras();
if (extras != null){
getTime.setText(extras.getString("time"));
}*/
If you are trying to save data that will be need to be used in multiple other activities, SharedPreferences will be the easiest solution.
https://developer.android.com/training/basics/data-storage/shared-preferences.html
When you want to save a value, you would use:
SharedPreferences.Editor editor = getSharedPreferences(MY_PREFS_NAME, MODE_PRIVATE).edit();
editor.putString("time", displayTime.getText().toString());
editor.commit();
Then when you want to get the preference later on you would use:
SharedPreferences preferences = getSharedPreferences(MY_PREFS_NAME, MODE_PRIVATE);
String time = preferences.getString("time", "");
I've created a widget which displays a simple textview, which is editable as an Edittext in a configuration activity. I save the inputted text with shared preferences, so the user can tap the widget to edit the text, and the already inputted text appears in the edittextfield.
My problem is this. I would like the user to be able to add multiple widgets, but when a seccond widget is added, the same text as in the other widget is loaded from shared preferences. And, when on widget is edited so is the other one. Hope i'm being clear. I kinda have an idea it has something to do with appWidgetIds but i cannot figure it out.
Here's my code, a bit simplified.
public class WidgetConfig extends Activity implements OnClickListener, OnItemSelectedListener {
AppWidgetManager awm;
int awID;
Context c;
EditText info;
Button b;
String note;
int styleStart = -1, cursorLoc = 0;
SharedPreferences sp;
Spinner spinner;
String[] paths = { "10", "20", "30" };
File path = null;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.widgetconfig);
c = WidgetConfig.this;
info = (EditText)findViewById(R.id.etwidgetconfig);
...
b = (Button)findViewById(R.id.bwidgetconfig);
loadPrefs();
b.setOnClickListener(this);
//Getting Info about the widget that launched this activity
Intent i = getIntent();
Bundle extras = i.getExtras();
if (extras != null){
awID = extras.getInt(AppWidgetManager.EXTRA_APPWIDGET_ID,
AppWidgetManager.INVALID_APPWIDGET_ID );
}
awm = AppWidgetManager.getInstance(c);
}
...
private void loadPrefs(){
sp = PreferenceManager.getDefaultSharedPreferences(this);
note = sp.getString("NOTE", "DEFAULT");
info.setText(note);
}
private void savePrefs(String key, String value){
sp = PreferenceManager.getDefaultSharedPreferences(this);
SharedPreferences.Editor editor = sp.edit();
editor.putString(key, value); // value to store
editor.commit();
}
public void onClick(View v) {
// TODO Auto-generated method stub
savePrefs("NOTE", info.getText().toString());
RemoteViews views = new RemoteViews(c.getPackageName(), R.layout.widget);
views.setTextViewText(R.id.tvConfigInput, info.getText());
ComponentName thisWidget = new ComponentName(this, Widget.class);
AppWidgetManager manager = AppWidgetManager.getInstance(this);
manager.updateAppWidget(thisWidget, views);
Intent in = new Intent(c, WidgetConfig.class);
PendingIntent pi = PendingIntent.getActivity(c, 0, in, PendingIntent.FLAG_UPDATE_CURRENT);
views.setOnClickPendingIntent(R.id.B_EditAgain, pi);
awm.updateAppWidget(awID, views);
Intent result = new Intent();
result.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, awID);
setResult(RESULT_OK, result);
finish();
}
}
UPDATE:
So i tried this, but nothings different, still doesn't work.
private void loadPrefs(){
sp = context.getSharedPreferences("widget" + String.valueOf(appWidgetId)
, Context.MODE_PRIVATE);
note = sp.getString("Note", "");
info.setText(note);
}
private void savePrefs(String key, String value){
sp = context.getSharedPreferences("widget" + String.valueOf(appWidgetId)
, Context.MODE_PRIVATE);
Editor editor = sp.edit();
editor.clear();
editor.putString("Note", info.getText().toString());
editor.putString(key, value); // value to store
editor.commit();
}
You probably don't want to use getDefaultSharedPreferences here. All widgets share the same default shared preferences, so they will be constantly overwriting each other.
I had the same situation in my own app, so I used a custom preference file for each widget. You can name the preference file with the widgetID, and then each widget will always get it's own unique set of preferences.
In the configuration PreferenceActivity:
this.getPreferenceManager().setSharedPreferencesName(
"widget" + String.valueOf(mAppWidgetId)
);
This will store all of the PreferenceActivity settings into a preference file named after the widget id.
Then in the widget itself, just retrieve the file corresponding to its id:
preferences = context.getSharedPreferences(
"widget" + String.valueOf(appWidgetId)
, Context.MODE_PRIVATE);
And retrieve preferences like normal.