Dynamic Intent in Notification Library - android

Have a Android technique question for those seasoned vets out there... I want to create a library that dynamically opens an activity. Furthermore, I want the project to be able to contain the activity and then pass this to the library. (This is all based around C2DM)
So, my project 'hotdogs' will have a reference to the library, and will tell it to open the activity 'TodaysToppings' and the library will open up the activity 'TodayToppings'. My other project 'Weather' will also extend the same library and tell it to open the activity 'TodaysForecast' and the library will open the activity 'TodaysForecast'.
Does that make sense?

Idea is simple. You can pass package name and action if necessary, or if it's main then Intent.ACTION_MAIN. So, your library can use Context to start new activity based on package name and action to open a specific activity. What about activity, it can be placed inside any other application, no need to pass it somewhere.

This is an old question but I came across this while trying to save the problem myself. I figured out a simpler way to do this without having to configure any strings or passing in any parameters.
String packageName = getPackageName();
Intent notificationIntent = getPackageManager().getLaunchIntentForPackage(packageName);
Now you have the intent you can add on extras, etc.

Related

Opening A Specific Activity based on parameters in a URI

I'm using Xamarin.Android to build an app for a customer that's meant to be supplementary to another app. The idea is that once they reach a point in their workflow where my app steps in, they will click a button inside their app that will launch my app, and will open an activity from my app based on some attached parameters.
My problem is that in the "calling" app, all that's exposed for me to work with is a parameterized URI that's passed directly to Android, so I have no means of creating and passing an Intent object. I can change the URI it sends to be whatever I like, so I've already added an intent filter that looks for the custom scheme myapp://.
I am new to SO, so I apologize if this question has been answered elsewhere, but I have looked for a few hours and in my searches so far, all I've seen are answers related to how to call another app from my own. The question I have is... how do I parse that request on the other end and know not only which activity to open, but the rest of the data or parameters that were in the URI? Is it possible to open an activity based on a parameter in that URI, or can I only point it to one activity?
Thanks in advance!
What you have to do is quite simple in your URL somewhere pass your activity name that you want to navigate to
Then in your code maintain a enum with all the possible activities you wanna navigate to:
enum ActivityName
{
MainAcitivity,
SomeotherAcitivity
}
Then in your received notification get the string where you have given the name of the activity and do something like this :
If(youractivityName==ActivityName.MainActivity.toString())
{
StartActivity(typeof(MainActivity));
}
Revert in case of queries.

Two SearchView components with different searchable configuration

Hi I have problem with planning implementation of specific search interface.
I am making navigation application where you can navigate from point to point.
For this I need two seachviews both with different searchable configuration, where I can define different custom intent action to recognize if user enter source or destination and react properly. I have already created suggestion logic because I am using one SearchView in other activity and already have different configuration there.
So my plan is creating an activity for result which will be in same time SearchActivity where I will setup two different SearchViews components where based on user input there will appear suggestions and after clicking them (first later second) the intent will be picked by CUSTOM1/2 ation intent in onNewIntent() as it will be "singleTop" activity and stored properly. Then i will process stored source&destination data and finish with proper response code to the source activity with extras based on processed data.
I don't know if I explained it properly. The main problem for me here is that I need to have two different SeachViews widgets. How to achieve this?
Eventually I can manage both SearchView suggestion results with default ACTION_VIEW and recognize them by sent data but I think the first plan is more clear.
PS I am planning to put them directly to the layout without appbar.

Android - Pass String from activity to library activity

I'm currently experimenting with creating my own libraries (aar) and I've come to the point that I have a library with an activity in it up and running in my project. But I have one small problem that I can't solve: how do I send a String from my Mainactivity to the activity that I created in my library?
I know that if I was working with activities that belong to the same project I could just create an intent in activity 1, add a putExtra with the String, start the activity and dig up that intent in activity 2. But the activity in my library doesn't know the activity in my project exists, so that doesn't work.
I could put the info in my SharedPreferences, but I'd like to avoid that.
Any help? I've been searching the web and I've found a solution for the other way around, but sadly that's useless to me :-)
The activity in your library doesn't have to "know" the first one... It has just to check if the intent contains the extra you sent:
if(getIntent().getStringExtra("yourStringExtraName") != null){
//Do your stuff here
}else{
//Do stuff when there isn't your string
}

How do I check whether an Intent is explicit or implicit?

Is there a simple way to find out whether an Intent is explicit or implicit?
I'm trying to implement a navigation drawer with the behaviour recommended in the Android docs, i.e. that the drawer should open automatically when the app starts, until the user has opened it manually.
However, the activities that use the navigation drawer can be started from a variety of places within my app, as well as outside it. I want to open the drawer only when the activities are launched from outside my app, but I can't find a simple way to find out where an intent came from.
There are a few methods in Intent that initially looked promising:
getComponent() would seem to give you the information (null/not null) on the sending end (where you already know!) but according to the docs is never null on the receiving end.
hasCategory() could be used to find known categories of external intents such as android.intent.category.LAUNCHER. This might be what I end up using but I'd rather have a general solution than try to account for each possibility here.
getPackage() looks like it might do the right thing, but unless I'm not using it correctly it always seems to return null in my tests.
Alternatively, I could always use putExtra() to add a flag to all of my internal intents, but that feels too much like a hack so I'd rather avoid it if there's another option.
Is there a simple way to find out whether an Intent is explicit or implicit?
That's not what you need, given the rest of your question. What you really should be asking is:
How can I determine internal Intent invocations from external ones, with limited hassle?
In that case, for many apps, you can use this algorithm: if getAction() is null, it's an internal explicit Intent. Otherwise, assume it's external.
If, for various reasons, you are using action strings for starting local activities (despite also setting the component on the Intent and making it explicit), then you will need to fall back to the "tack on an extra" thing. Normally, if you are starting a local activity with an explicit Intent, you are not setting an action string, which makes the action string a clear delineator between implicit (action not null) and explicit (action null).
The best way to implement this is to pass extras with the Intent that identify the Activity that called it, like you mentioned. It's not a hack in particular when you consider that Intents are frequently checked for static identifiers when the Activity finishes and the calling activity checks the result code. Yours is the inverse case of checking the calling Activity.

Open an activity without declaring it in the manifest file in ANDROID?

I want to open an activity without declaring it in an manifest file.
I don't know if it is possible or not.
What I actually want is to dynamically open an activity from my program using intents.
Can anyone help me if it is possible.
Not possible. Although I am unsure what you mean "dynamically open an activity".
See: http://developer.android.com/reference/android/app/Activity.html
Under Class Overview it states "To be of use with Context.startActivity(), all activity classes must have a corresponding declaration in their package's AndroidManifest.xml"
You can have an Activity in your package and not define it in the manifest, however you would not be able to start it successfully.
Your 'Dynamic' activity start is actually the normal way of starting an activity (as you have said in a comment to the answer of Matt M). Though you HAVE to add the Activities in manifest as Matt M said. In list view, clicking an activity will call a function that will start respective activity with startActivity() function.
I tried this very long, but since the Instrumentation class uses the IActivityTaskManager, which is a class of the internal API located at com.android.server.wm, that is not in the Activity classloader, I solved this by another method:
Using a subclass
I just made an Gist, with sample code.
GIST

Categories

Resources