I want to sent value to another activity. I'm using VS 2012 C# for developing apps. I have done lot of search from google. I have tried almost every methods. in my application getIntent(); giving error. that is, getIntent() does not exist in the current context.
I'm also not getting these below functions in my application.
Intent sender=getIntent();
getApplicationContext();
I have added all references. Help me where Am I wrong?
I got answer, I have to use below line of for getting value:
string extraData = Intent.GetStringExtra("KeyName");
Related
I'm getting an weird error so I'm trying to eliminate the possibilities.
Does the context passed to PreferenceManager.getDefaultSharedPreferences() changes the result?
I mean, when i'm writting setting to my app i never pay attention which context i pass to this method since it is a valid context...
Sometimes i put the Activity, sometimes the Appliaction whatever context i've on hands
Is it wrong? I've noticed that i'm getting wrong preferences values at some point, and i dont know if there is a bug in my code or if this be
It doesn't matter whether you provide an Application or an Activity as the Context parameter for PreferenceManager.getDefaultSharedPreferences().
If you look at the source for getDefaultSharedPreferences():
return context.getSharedPreferences(getDefaultSharedPreferencesName(context),
getDefaultSharedPreferencesMode());
Looking further, into getDefaultSharedPreferencesName(context):
return context.getPackageName() + "_preferences";
This means that for any Context of your application, you'll get the same SharedPreferences back, as your application ID does not change based on Activity or Application.
The only time you could run into a potential issue is if you are manually creating a Context for another package (e.g. using Context.createPackageContext()).
SharedPreferences data stores all have a name, and as long as you use the same name you'll always get the same data store.
Hat tip to #kcoppock who has pointed out that in the particular case of PreferenceManager.getDefaultSharedPreferences(), the only thing the generated name is dependent on is the Context's package. Since any Application or Activity instance you pass is exceedingly likely to have the same package name, in your case you should always get the same data store.
There are other ways to retrieve SharedPreferences stores, though. Activity.getPreferences() will generate the name based on the Activity's class name, so calling getPreferences() from inside two different activities will give you two different data stores.
You can also call Context.getSharedPreferences() directly (both PreferenceManager and Activity just call through to this) and pass a data store name explicitly. There's no requirement for how the name should look; as long as you use the same name you'll always get the same data store.
https://developer.android.com/reference/android/content/Context#getSharedPreferences(java.lang.String,%20int)
My program consists of a MainActivity and two fragment activities. I need one fragment to take a String value from the user and pass it to the second fragment.
I am trying to wrap my head around how to do this. Since I am familiar with intents, I found this answer on another post and decided to try it out. Everything looks fine until I get to step 4, when I try to use Intent i = getIntent(); in my second fragment, Studio won't let me use it and says "getIntent(java.lang.String) is deprecated".
This doesn't make sense to me since I have used getIntent() in other programs without issue, and it is letting me use it in my MainActivity (step 2 from the other post) without screaming at me.
I know this can be done without using intents, but I can't figure it out and can't find any really thorough tutorials in order to do so. So I guess my questions are:
Can I make intents work for this purpose still? What should I do to get around this deprecation issue?
Any other advice, explanations, or links to "explain it like I'm 5" tutorials would be very helpful and welcome. I have Googled and read a few, but I am still not understanding this and am becoming increasingly frustrated. It seems like this should be a relatively simple concept.
It is too late for answer but still I am providing my answer for other persons. It is happen because Intent is basically work with an activity. And fragments are not activity, but attached to activity. So simply you need to do this:
Intent intent=getActivity().getIntent();
Having the same problem while passing Object from an Activity to a Java Class.
Here is what I did
This Activity sends data
Salary newSalary = new Salary();
Intent intent = new Intent(ViewData.this,Data.class);
intent.putExtra("SalaryObj", newSalary);
It recieves data(In Data.class)
Here I tried this but Android Studio says getIntent is deprecatedIntent intent = Intent.getIntent();
So What can I use in place of getIntent(), because all the solutions I find on Internet to this problem, uses getIntent().
EDIT:
I was playing around with this and found that I was trying to receive data in Java Class(Not an Activity). But when I used it in an Activity then it works fine. But it takes me to another question that how to send data from an Activity to Java Class(which is not an Activity).
On your onCreate
val bundle = intent.extras
if (bundle != null) {
idEmployee = bundle?.getString("idEmployee", "")
idClient = bundle?.getString("idClient", "")
listAvailable = bundle?.getStringArrayList("listAvailable") as ArrayList<String>
Log.i("list:", "$listAvailable" )
}
It means, that this method could not be supported in further releases. The method is still in the API for backward compability for an unspecified amount of time. Mostly it is dangerous to use deprecated methods or there is a better way to achieve this.
Like it is described here
In this case you should rather use: parseUri(String, int) to achieve this ( according to the android developer api).
I've implemented a notification listener to look out for a Gmail notification.
I want to collect the expanded text (bigtext) from the notification as shown in the notification below:
See "----Forwarded message---", etc. which only appears when the user expands the notification to show the action buttons.
This string value does not appear in the notification's "EXTRAS" data...
http://developer.android.com/reference/android/app/Notification.html
After viewing the above link, I further investigate the bundle (EXTRAS) data. When you debug it and look at the variable, you can find that all information regards the notification are stored in the bundle and you can get the detail by
notifyBundle.extras.getCharSequence("android.textLines") for multi line notification and
notifyBundle.extras.getString("android.text") for single line notification.
For more information, look at the variable in eclipse debugging mode
Image of variables in bundle for single line notification
Image of variables in bundle for multi line notification
Note: The extra bundle only available in API19 (Kitkat). I've never tried in device which lower than API19.
I ran into a similar problem with Gmail running on Android 7.
Eventually, I've realized (with some help from another thread) that he solution was different from the existing answers here - what you're looking for can be accessed in a different way:
Bundle extras = statusBarNotification.getNotification().extras;
extras.get(Notification.EXTRA_BIG_TEXT) == null ? null : extras.get(Notification.EXTRA_BIG_TEXT).toString();
This way you will always get either a String or null, even if the value you're looking for isn't originally a string. This is done because calling getString(Notification.EXTRA_BIG_TEXT) directly would return null instead, at least in some cases.
If there are any other values you're not sure where they might be stored, you can try iterating through the entire extras bundle, as explained here.
Finally, I am able to solve this issue by using this code.
ArrayList<StatusBarNotification> groupedNotifications = new ArrayList<>();
for(StatusBarNotification statusBarNotification : mNotificationManager.getActiveNotifications()) {
if(statusBarNotification.getNotification().getGroup().equals(NOTIFICATION_GROUP)) {
groupedNotifications.add(statusBarNotification);
}
}
CharSequence stackNotificationMultiLineText[] = groupedNotifications.get(ZEROTH_INDEX).getNotification().extras.getCharSequenceArray(NotificationCompat.EXTRA_TEXT_LINES);
If you try to use getCharSequence("android.textLines"), it returns you null because NotificationCompat.EXTRA_TEXT_LINES returns you the array of CharSequence and not a single CharSequence object.
I found a shorter way of accessing the expanded notification. This works in API 24.
If you are getting null while accessing getCharSequence("android.textLines"), this is because it actually returns an array of CharSequence as rightly pointed out by Puneet above.
So rather access them like this:
if (extras.getCharSequenceArray("android.textLines") != null)
text = Arrays.toString(extras.getCharSequenceArray("android.textLines"));
My code is as follows:
First, I was wondering about line 20:
I had two questions:
a. Why is MY_MESSAGE assigned to com.example.myfirstapp.MESSAGE?
b. What is com.example.myfirstapp.MESSAGE?
c. I mever made MESSAGE anywhere; is this automatically made like the variables in r.java file, or do i need to make it somewhere?
Secondly, about line 40: intent.putExtra(EXTRA_MESSAGE, message);
I am not sure if this method adds a message to the upcoming activity to be called or what... Partly, I am struggling to understand this due to not knowing the point of an Intent fully.
I want to read my 200 fundamental section on what everything is, but I have set deadlines and I have been told not to take that approach for the time being for this project
With given the explanation of the Android Docs , I know an intent is:
The Intent itself, an Intent object, is a passive data structure holding an abstract description of an operation to be performed
A.) Could someone explain what the intent is used for or give some better quick articles than just the docs?
B.) Explain what putExtra( ) does and and these parameters more clearly:
name The name of the extra data, with package prefix.
value The String array data value
An Intent is appropriately named; it's what you want to be done. As the documentation says:
Its most significant use is in the launching of activities, where it can be thought of as the glue between activities. It is basically a passive data structure holding an abstract description of an action to be performed.
By your code, you are familiar with starting an Activity via Intent:
new Intent(this, DisplayMessageActivity.class);
This uses your current Activity as the context from which to start the Intent, and gives the target class to launch. You already know this, I think. Basically, the Intent is just a guide for the Android device to follow so that it launches the right target with the right information.
Onto your real questions:
"What is the intent used for?" This is described above; basically, it's used to tell the OS what your target is, where it's coming from, and what data it should provide. You've seen most of this in action without realizing; this constructor is the one you've been using, detailing the "from" and "to" portions. When you use putExtra, you are providing the Intent with data it can give to the "to" part of the code.
The name parameter is best summed up by the documentation: "The name of the extra data, with package prefix." This is like a key in a HashMap; it is a string identifier of the content you are packaging. They tell you to use your package's prefix, just to prevent confusion. In your case, you should be using "com.SG.Three_Piece_Radio.YOURKEYNAME"; this does not have to be declared anywhere, nor is it a constant. Just a string. The value is just the contents of the extra (the data); this can be a ton of different things--short, int, String, Parcelable, and many more. (These can all be found in the various putExtras in the Intent docs.)
Once your Intent is received, you can use those same bits of data (for example, String myStr = getIntent().getStringExtra("com.SG.Three_Piece_Radio.YOURKEYNAME");) and do whatever you wish with them in the Activity you called.
I think people have been very helpful here in giving great explanations about Intent itself and its purpose. I got to learn a lot from these answers.
However, there was a small aspect that I think needs a little more explanation.
So to answer your very first question that says :-
a. Why is MY_MESSAGE assigned to com.example.myfirstapp.MESSAGE? b. What is com.example.myfirstapp.MESSAGE? c. I mever made MESSAGE anywhere; is this automatically made like the variables in r.java file, or do i need to make it somewhere?
My answer would be :-
So as all explained, putExtra is meant for carrying additional information/data along with the intent for the new activity that is going to be started. This additional information that putExtra carries is given in Intent in the form of a Key-Value pair.
In this Key-Value pair, the Key syntactically always has to be a String.
In your case, the value is also a String and the "key" can be any random string.
Now, to make sure that the system does not confuse your KEY with some other app's KEY you should always append the entire packet structure of the string along with it. And hence you use :-
com.example.myfirstapp.MESSAGE
where MESSAGE is actually the name of the key, (the String as is required, as I mentioned above) that would be associated with the string value that would be passed with the intent to the new activity.
Now you could have very well written the following as well :-
intent.putExtra("com.example.myfirstapp.MESSAGE", message);
instead of :-
intent.putExtra(EXTRA_MESSAGE, message);
but then this would reduce the flexibility of your code for changes to be made later. As for any changes in the key name you will have to change it everywhere. So to avoid this we rather assign the name of our key (in your case, MESSAGE) to a String variable (in your case EXTRA_MESSAGE).
This also makes it easier for other activities to reference this key by a simple String variable. And so to make it accessible to other activities (coupled with other self explained features)you make it as :-
public final static String EXTRA_MESSAGE = "com.example.myfirstapp.MESSAGE";
Please correct me if I happened to miss something or went wrong somewhere.
The most common use of intents is to start new activities (screens)within an application (line 41). The extras Bundle is a way of passing data between activities. Extras are entered as key value pairs so EXTRA_MESSAGE is a key is used to identify a particular value so it can be retrieved and used by another activity.
i am struck with passing String array from one activity to another using intent. while passing only a String from two activities, its work clear. but whenever i tried to access string array i am getting force close error. can any one guide me to rectify the problem.
You can use just the Intent.setExtra(String, Serializable) method. It accepts any Serializable object. If that does not work, please attach results from your LogCat, "Closed unexpectedly" just means your app failed, LogCat tells why it failed.
use Parcelable to do this.
please refer to this link http://shri.blog.kraya.co.uk/2010/04/26/android-parcel-data-to-pass-between-activities-using-parcelable-classes/