I am trying to respond to a result from an activity using startActivityForResult. I am getting the expected result but am unsure how to respond to the result. What I'd like to do is call a different ArrayList (which is a resource) depending on the result.
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK) {
Bundle d = data.getExtras();
String oil = d.getString("oil");
Resources res = getResources();
String[] oil_info = res.getStringArray(R.array.OIL);
...
}
}
I'd like R.array.OIL to change in response to the result. i.e. if oil = 'BASIL' change R.array.OIL to R.array.BASIL. I thought about making a hashmap but couldn't figure out how to put R.array.OIL into a hashmap as you cannot put primitives in a hashmap. I am pretty new to android and java so I'm sure there is a better way to do this. Any help would be much appreciated. Thanks.
Use getIdentifier() for your requirement as below.
e.g. in array type 2 items are there as OIL and BASIL with different values.
you can access like R.array.OIL and R.array.BASIL.
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK) {
Bundle d = data.getExtras();
String oil = d.getString("oil");
Resources res = getResources();
int id = res.getIdentifier(oil, "array", getPackageName());
String[] oil_info = res.getStringArray(id);
...
}
}
In above code if string oil is "OIL" then id will become R.array.OIL and oil is "BASIL" then id will become R.array.BASIL
what you are trying is actually an attempt of permanent storage . this is not allowed for "Res" folder or "R.smthing.smthing" data's .
use shared preferences for primitive storage .
see whether you can save array through sharedPreferences else use Serialization or SQLite database .
Related
I'm trying to set the Image to the ImageView through calling a Bitmap but suddenly the line below shows Cannot resolve putExtra. I think there is updated syntax of this line. Is somebody knows the problem about this? I search many times but this line must be required
#Override
protected void onActivityResult(int requestCode, int resultCode, #Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 100){
Bitmap bitmap = (Bitmap) data.putExtra().get("BitmapImage"); //Error
PsId.setImageBitmap(bitmap);
}
}
you want to GET data, not PUT... just use getExtras() instead of putExtra()...
Bitmap bitmap = (Bitmap) data.getExtras().get("BitmapImage");
btw. avoid passing Bitmaps in Intent/Bundle, these are too heavy and may result in some rare crashes related to memory handling by OS... it would be better to store this bitmap in memory or cache and pass as result only reference to it (e.g. filename or some key under bitmap was stored)
i want to store my balance in sqlite database i am successfully dial the USSD
String balance_check="*444";
String encodedHash = Uri.encode("#");
String ussd = balance_check + encodedHash;
startActivityForResult(new Intent("android.intent.action.CALL",Uri.parse("tel:" + ussd)), 1);
Here is onActivityResult function
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 1) {
if (resultCode == RESULT_OK) {
String dd = data.toString();
Log.d("myussdmessage", data.toString());}}
but I cannot get the string exactly how to do it.I want to store the balance form the dialogue to my database.Can any one suggest me how to do it.
You need to build an AccessibilityService in order to do that you need to read the documentation of AccessibilityService in below link
https://developer.android.com/reference/android/accessibilityservice/AccessibilityService.html
and also this question helped me
Prevent USSD dialog and read USSD response?
#Override
public void onActivityResult(int reqCode, int resultCode, **Intent data**){
super.onActivityResult(reqCode, resultCode, **data**);
switch (reqCode) {
case (PICK_CONTACT) :
if (resultCode == Activity.RESULT_OK) {
Uri contactData = **data**.getData();
Cursor c = getContentResolver().query(**contactData**, null, null, null, null);
if (c.moveToFirst()) {
String name = c.getString(c.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
// TODO Whatever you want to do with the selected contact name.
//ImageView imageView = (ImageView)findViewById(R.id.imageView);
//Picasso.with(this).load("https://cms-assets.tutsplus.com/uploads/users/21/posts/19431/featured_image/CodeFeature.jpg").into(imageView);
FragmentManager FM = getFragmentManager();
FragmentTransaction FT = FM.beginTransaction();
FragmentActivity F1 = new FragmentActivity();
FT.add(R.id.frame_layout, F1);
FT.commit();
}
}
break;
}
}
Can someone explain how the data variable in the onActivityResult argument is used to make this code work?
I see that the variable is used to call getData() but I'm confused as to how this variable is connected to the Intent outside of this method.
Furthermore, what exactly does calling data.getData() do ?
Basically I'm trying to understand this snippet of the code
public void onActivityResult(int reqCode, int resultCode, Intent data){
super.onActivityResult(reqCode, resultCode, data);
switch (reqCode) {
case (PICK_CONTACT) :
if (resultCode == Activity.RESULT_OK) {
Uri contactData = data.getData();
Cursor c = getContentResolver().query(contactData, null, null, null, null);
Could anyone help me make sense of it?
The data variable is the Intent that you have specified in the closing Activity. When you want to finish the Activity you called with startActivityForResult(), you should call setResult().
As first parameter, you should set the result code (RESULT_OK, RESULT_CANCELED, ...). You can also add a second one which should be an Intent and which will contain the information you want to receive in onActivityResult(). And that is the data variable in onActivityResult().
If you want to know more about getData(), you can check this : http://developer.android.com/reference/android/content/Intent.html#getData()
Take a look here : Click me and than the first answer
At the link you can see that you can give the Intent data a bundle or extras, which you get with your getData() method.
Let's assume that the called Activity (started by startActivityForResult()) sets the result before finishing as follows:
Intent data = new Intent();
data.putExtra ("aValue", 42);
getActivity().setResult(Activity.RESULT_OK, data);
finish();
In order to get the value in the calling activity, use
public void onActivityResult(int requestCode, int resultCode, Intent data){
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case (PICK_CONTACT) :
if (resultCode == Activity.RESULT_OK) {
int aValue = data.getIntExtra("aValue", -1);
....
}
}
This example is based on an int, however, there are a lot of other value types can be passed. See Androids Intent documentation.
p.s.: let me add that it works in the same way with void Intent.setData(Uri) and Uri Intent.getData() instead of putExtra(...) and getExtra(...)
I have a search button in MainActivity which launches SearchActivity. In SearchActivity, the user can either choose from one of the predefined categories listed, or can enter in a search query. The SearchActivity will return a different extra depending on which way the user searches. The code that runs in MainActivity will depend on which extra is returned.
I'm debating which way is better or more "correct". I've coded it both ways, and it works either way.
The first way I coded it was to check for the existence of the intent extra:
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == GET_SEARCH_REQUEST_CODE) {
if (resultCode == RESULT_OK) {
if (data.getExtras().containsKey("extra1")) {
extra1 = data.getStringExtra("extra1");
}
if (data.getExtras().containsKey("extra2")) {
extra2 = data.getStringExtra("extra2");
}
}
// plus rest of code for checking for RESULT_CANCELED
}
}
The other way I coded it is by using custom result codes. The result codes RESULT_OK_CATEGORY and RESULT_OK_SEARCH are public static variables in MainActivity so that they can be accessed from SearchActivity, and sent back as a result code through setResult(MainActivity.RESULT_OK_CATEGORY, intent) or setResult(MainActivity.RESULT_OK_SEARCH, intent) respectively.
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == GET_SEARCH_REQUEST_CODE) {
if (resultCode == RESULT_OK_CATEGORY) {
extra1 = data.getStringExtra("extra1");
} else if (resultCode == RESULT_OK_SEARCH) {
extra2 = data.getStringExtra("extra2");
}
// plus rest of code for checking for RESULT_CANCELED
}
}
Which way is better, and why? Checking for the existence of the extra, or checking for a custom result code?
You should use resultCode because it's completely under your control. You may lose values set in Intent's extras, as revealed in this question (referenced by Ajay in the comments).
Note, if you use many custom result codes, it has been recommended to use a switch statement for code clarity.
I need the results from recognizer to be a string.. when i use this code i get eclipse error 'cannot convert Array as string..
code: final ArrayList<String> results2 = data.getStringArrayListExtra (RecognizerIntent.EXTRA_RESULTS);
also tried, with no luck:
final String results2 = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
still says error..
Is this enough info?? Thank you in advance.
NEW: I added this code, but i want a .get that will store this word/text in acceptable manner for a cursor usage:
String[] matches = data.getStringArrayExtra(SpeechRecognizer.RESULTS_RECOGNITION);
Eclipse does not error , but my result is coming out as 'null' so i just need a .Get that works in my scenario?? Than kyou all for helping also!
you code should work fine. See this snippet:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Intent i = getIntent();
final ArrayList<String> stringArrayListExtra = i.getStringArrayListExtra("hello");
}
What you can look is:
check for your java version. Do you have java 1.5 (or higher but I am not very confortable with 1.7 yet) configured. The reason this that generics are added in 1.5.
Try cleaning and rebuilding your project by choosing Project > Clean.
I can't see any other
Edit: Since there is not enough code to look into, you can yourself look into speech sample here
If look there:
/**
* Handle the results from the recognition activity.
*/
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == VOICE_RECOGNITION_REQUEST_CODE && resultCode == RESULT_OK) {
// Fill the list view with the strings the recognizer thought it could have heard
ArrayList<String> matches = data.getStringArrayListExtra(
RecognizerIntent.EXTRA_RESULTS);
mList.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,
matches));
}
super.onActivityResult(requestCode, resultCode, data);
}
They are using RecognizerIntent.EXTRA_RESULTS. Try this sample if it resolves your issue. As now your issue is different then what i look initially.
This should work for you:
#Override
public void onResults(Bundle results) {
ArrayList<String> matches = results.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION);
[...]
/edit:
Sorry, like I said, I mixed that up. Have you checked the request code? Normally this should work:
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
if (requestCode == REQUEST_CODE && resultCode == RESULT_OK)
{
ArrayList<String> matches = data.getStringArrayListExtra(
RecognizerIntent.EXTRA_RESULTS);
}
super.onActivityResult(requestCode, resultCode, data);
}
However, this is actually almost exactly the code you are using - try it nethertheless, as it has super.onActivityResult() in the end.