I wrote an long live Service...which actually lauch Notification Panel. I create Pending Intent to launch Activity element of same app.
So here is there any way to identify pending intent launch event?
Put something in the extras of the intent that you're passing to the getActivity method when you're creating your PendingIntent. Then, in the onCreate method of the activity you're opening, check if that field has been set in the extras.
Related
I am trying to send data from one app to another. The mother app will launch the child app using this
Intent LaunchIntent = getPackageManager().getLaunchIntentForPackage("com.example.stuff");
LaunchIntent.putExtra("Stuffkey", "blahblah");
startActivity(LaunchIntent);
What do I have to write in the child app so that it can read the extra?
You need to call getIntent() in the onCreate() method of the main activity of the second app. After that you can call getStringExtra() on the intent to get the parameter.
Inside a broadcast receiver I want to start my app (Activity) and pass in some data.
My problem is that the extras don't seem to carry over into the activity. I am trying to get the data inside the onNewIntent(Intent i) function.
Any ideas?
Here is my current attempt in the BroadcastReceiver:
Intent intSlider = new Intent();
intSlider.setClass(UAirship.shared().getApplicationContext(), SliderMenuActivity.class);
intSlider.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intSlider.putExtra("action", ScreensEnum.Object);
intSlider.putExtra("objectId", objectId);
intSlider.putExtra("objectCode", objectCode);
intSlider.putExtra("userId", userId);
UAirship.shared().getApplicationContext().startActivity(intSlider);
EDIT - Added code used in onNewIntent() and onCreate()
The following code works great in onCreate() when the app isn't currently running. For when the app is already running the same code doesn't work (i.e. no extras) from the onNewIntent() function.
Intent intent = getIntent();
if(intent.hasExtra("objectId")) {
loadDetail(intent.getStringExtra("objectId"), "2w232");
}
The problem is getIntent() method. It always returns the intent that started the activity, not the most recent one. You should use intent that was passed to onNewIntent method as an argument.
We stumbled upon this problem once, when we were trying to launch/call onNewIntent on an Activity in response to a local notification tap. The extras that we put on our Intent were disappearing at the time onNewIntent received it.
I don't remember this being documented anywhere back then, but the "problem" was that we weren't setting the action field on the Intents that we prepared. Turns out if the Intent received by your Activity doesn't have an action set using setAction, the system still delivers the Intent to its destination, but doesn't transmit the extras you have set while creating the Intent.
TL;DR:
If you encounter this problem with an Intent with no action, calling setAction to set an arbitrary action value before sending the Intent might fix it.
Extract from the docs
This is called for activities that set launchMode to "singleTop" in
their package, or if a client used the FLAG_ACTIVITY_SINGLE_TOP flag
when calling startActivity(Intent). In either case, when the activity
is re-launched while at the top of the activity stack instead of a new
instance of the activity being started, onNewIntent() will be called
on the existing instance with the Intent that was used to re-launch
it.
An activity will always be paused before receiving a new intent, so
you can count on onResume() being called after this method.
Note that getIntent() still returns the original Intent. You can use
setIntent(Intent) to update it to this new Intent.
I think the last paragraph explains your problem.
You have to set the flag FLAG_ACTIVITY_SINGLE_TOP or set launchMode singleTop in the manifest file.
Of course when onNewIntent is called you do not use getIntent but the Intent received as argument.
onNewIntent will be called when the activity instance already exists. For example, if last time you pressed the Home Screen button.
I wrote a solution that worked for me here: Intent with old extra in onCreate() for singleTask Activity
You can store the last received intent in a member variable (mLastIntent).
Then you can use this member in your onResume() method to query for your extra data.
private Intent mLastIntent;
#Override
protected void onNewIntent(Intent intent) {
mLastIntent = intent;
};
In Android 2.3.3, how does one differentiate Intents that start a particular Activity. For example, if both Activity_A and Activity_B have intents that call startActivityForResult( intent, requestCode), how does Activity_C differentiate between which Activity has started it? Also, I know that one passes a requestCode to the starting Activity, but how does this Activity handle the requestCode? There is no method in Intent that says getRequestCode(). Is the only way to do this to place the requestCode in a Bundle in addition to the method startActivityForResult? Thanks!
Intent API:
http://developer.android.com/reference/android/content/Intent.html
One solution would be to pass along an extra piece of identifying data. For example:
intent.putExtra("activity", "com.whatever.MyActivity");
Then the receiving Activity can read it:
Bundle extras = getIntent().getExtras();
String activityName = extras.getString("activity");
It seems like there should be an easy method call to tell what the sending Intent was, but if so, I'm not aware of it.
I launch an activity from my widget using an Intent with some extra, anyway I can only get the Intent when the activity is in background.. How can I get the Intent when activity is created? Tried with this.getIntent() but extras are null.
Thanks in advance
How can I get the Intent when activity is created?
Call getIntent() on the Activity.
Tried with this.getIntent() but extras are null.
One possibility is that you are arranging for an existing instance of the activity to return to the foreground (e.g., including FLAG_ACTIVITY_REORDER_TO_FRONT), in which case you will need to override onNewIntent() and collect the Intent there.
Another possibility is that you originally created a PendingIntent for the Intent with no extras, then later tried to create a new PendingIntent on an equivalent Intent (e.g., identifying the same activity) and included extras. In that case, you need to include FLAG_UPDATE_CURRENT or FLAG_CANCEL_CURRENT when creating the PendingIntent, so your new/changed extras are taken into account.
I am using android-c2dm, and my device successfully receives messages from it. I want to call a non-static function (in an already-existing Activity) to do something with that message, but simply calling it from C2DMBaseReceiver is illegal. How can I transfer this information back to the activity?
Edit: What if I call a static function to assign variables (or set Shared Preferences), then call a handler which will use those variables to do what needs to be done? Is that bad style?
What you could do is put the message details in an Intent somehow (the crudest way would be serialize the message to a String and add it as an Intent extra) and then send that Intent to the Activity using startActivity. The Activity could check for the extra, know that it is a message, extract and deserialize the message and then go to town.
You might need to set the appropriate launch mode or Intent flags if you want to make sure that an existing instance of the target Activity receives the message.
You can get message from Intent in onMessage method, then show Notification and startActivity after user clicks on Notification. You can use Intent flags for bring background activity in foreground.
So if activity is not started - it will be started, if activity in background - it will be show in foreground and if it in foreground - then we need only change TextView text.
You also can startActivity without showing Notification.
Also you can use onNewIntent(Intent intent) Activity method for changing text. You can put message from google intent in onMessage into new Intent and startActivity with this intent and FLAG_ACTIVITY_SINGLE_TOP flag.