Read out Bundle content - android

I use intents to communicate from a service to the activity. I use intent.putExtra(Tag, message); if I have a error or something.
In the Activity I can get these Extras like following
String message_error_1 = intent.getStringExtra("Message_Error_1");
String message_error_2 = intent.getStringExtra("Message_Error_2");
String message_error_3 = intent.getStringExtra("Message_Error_3");
but just one has data. I can now make an If statement for every entry but I think there is a way to figere out which entry has data. Is there a way?

If there's only going to be one error, I'd consider passing an ERROR_TYPE int as one extra and the ERROR_MESSAGE string as another extra, so you don't have to write so many if statements. For example:
int type = intent.getIntExtra("error_type"); // 1, 2, or 3
String message = intent.getStringExtra("error_message");
To answer your question, you could have the service put in an extra that indicates which error message key to use. For example:
// In Service
intent.putExtra("error_key", "Message_Error_1"); // error 1 has the message!
// In Activity
String key = intent.getStringExtra("error_key");
String actualErrorMessage = intent.getStringExtra(key);

Related

How to pass dynamic data in intent from a qr code?

I am done a Qr code which have data like
url=www.google.com;type=T;location=xyz;company=aaa
or
url=www.google.com;
type=T;
location=5Q01;
company=SYS
or url=www.google.com|type=T|location=5Q01|company=SYS
and I have done reading QR with this tutorial in android.
so here I want to pass this data separately via intent
like url=www.google.com in one intent and type=T;location=xyz;company=aaa in other intent
but here this entire content I am getting in one rewtext format...
can any one help me how to resolve..
Well the response is in String format, you can simply split the string with ';' char and get a separate array which will serve your purpose.
String s = "url=www.google.com;type=T;location=xyz;company=aaa";
String sArray [] = s.split(";");
then -
sArray[0] = url=www.google.com
sArray[1] = type=T
sArray[2] = location=xyz
sArray[3] = company=aaa

how to send a string to listView in another activity ? in android

Good evening guys,
I'm making an app and I want to know how to send a string to "List-View" in another activity ?
You can send data using the following code -
Intent intent = new Intent(this,newActivity);
intent.putExtra(name, value)
name = The name of the extra data
value = The String data value.
startActivity(intent);
In the new activity, you receive string via following (in onCreate)
Intent intent = getIntent();
String str = intent.getString(name)
name = The name of the extra data
Now search the web on how to add a string to list view. You will find it easily

How can i get few characters from String?

I want to retrieve few characters from string i.e., String data on the basis of first colon (:) used in string . The String data possibilities are,
String data = "smsto:....."
String data = "MECARD:....."
String data = "geo:....."
String data = "tel:....."
String data = "MATMSG:....."
I want to make a generic String lets say,
String type = "characters up to first colon"
So i do not have to create String type for every possibility and i can call intents according to the type
It looks like you want the scheme of a uri. You can use Uri.parse(data).getScheme(). This will return smsto, MECARD, geo, tel etc...
Check out the Developers site: http://developer.android.com/reference/android/net/Uri.html#getScheme()
Note: #Alessandro's method is probably more efficient. I just got that one off the top of my head.
You can use this to get characters up to first ':':
String[] parts = data.split(":");
String beforeColon = parts[0];
// do whatever with beforeColon
But I don't see what your purpose is, which would help giving you a better solution.
You should use the method indexOf - with that you can get the index of a certain char. Then you retrieve the substring starting from that index. For example:
int index = string.indexOf(':');
String substring = string.substring(index + 1);

I want to know how to send 5 strings in Android to Arduino to store in variable?

I've used Amarino receive 5 String on Android from 5 edittexts then send to Arduino.
//Code to send a String. One of five Strings.
Amarino.sendDataToArduino(MainActivity.this,"HC-05",'a',name);
In Arduino I've created functions for each String for save in variables. But I can't pass compiling. There is an error messages "void value not ignored as it ought to be" .
//Arduino code. I take part of custom function code that relate Meetandroid.
void getUsername(byte flag, byte numOfValues)
{
// first we need to know how long the string was in order to prepare an array
// big enough to hold it.
// you should know that: (length == 'length of string sent from Android' + 1)
// due to the '\0' null char added in Arduino
int length = meetAndroid.stringLength();
// define an array with the appropriate size which will store the string
char data[length];
// tell MeetAndroid to put the string into your prepared array
//Can't compile Here
username = meetAndroid.getString(data);
}
I've also tried to send array of String. There is a function in Amarino but I can't find the receive function. I dont know what I may miss. How to solve this problem?
//data is array of String in Andriod
Amarino.sendDataToArduino(MainActivity.this,"HC-05",'a',data);
meetAndroid.getString(data) has no return value. Thus, you can't assign it to username.
char data[length];
username = meetAndroid.getString(data);
Change that to:
char data [] = new char[length];
meetAndroid.getString(data);
String username = data.toString();
return username; // and change function return type to String
You are sure byte data [] = new byte[length]; isn't more appropriate?

get the value from Intent of android

Thanks in advance.
When I print Log.d("me",getIntent().toString());
I am getting:
Intent { act=android.intent.action.CALL_PRIVILEGED dat=tel:888 flg=0x13800000 cmp=com.ninetology.freecall.two/.CallFinalActivity }
I am trying to fetch the value which is associated with "dat" but I am getting NullPointer exception.
//the code I am using is
getIntent().getStringExtra("dat"); // no use
//i tried
getIntent().getExtras("dat").toString(); // NullPointer exception
I tried with "tel" as key in above code still no use.
it seems you're doing this wrong.
The getExtras() function returns a bundle that you can extract data from and not a function that returns a specific String.
dat is NOT a String value as you can see from the data that was printed. it's a Uri,
try parsing it as you should and I'm sure you'll be able to get the data.
public void onCreate(Bundle b) { //mistyped
super.onCreate(b);
Uri data = getIntent().getData();
// OR USE THIS
String data = getIntent().getDataString();
// DO STUFF
}
try getIntent().getExtras().get("dat");
First of all, Its not necessary the string from Intent your are getting in log have a object with values..
So its better to just check its not a null, like,
Bundle bundle = getIntent().getExtras();
if(bundle ! = null)
{
// Now check you bundle object which has a values or not
}
else
{
// 1. get data in form of Uri
Uri data = getIntent().getData();
// 2. OR get string of Uri
String dataString = getIntent().getDataString();
// 3. Or split the data string
// The logic from this part may be different on your requirement.. I only suggests you to get data from string.. (Actual logic may different on your case)
String data = getIntent().toString();
data = data.subString(data.indexOf(":"), data.indexOf("flg")-1);
Log.e("tel:", data);
}
When you want to pass the data with the intent just add the below code before starting activity
intent.putExtra("dat", value); //value=the value you want to send
And when you want to fetch the same value in another activity just do:
Bundle bundle=getIntent().getExtras();
if(bundle!=null){
String string=bundle.getString("dat");
}
By doing this, you wont get the null pointer exception and will help you.

Categories

Resources