interrupting link to my app - android

I have a problem. I'm using the below code to interrupt links to my app as
<intent-filter>
<action android:name="android.intent.action.VIEW"/>
<category android:name="android.intent.category.DEFAULT"/>
<category android:name="android.intent.category.BROWSABLE"/>
<data android:scheme="http" android:host="twitter.com"/>
<data android:scheme="http" android:host="facebook.com"/>
</intent-filter>
But the problem is that I need to set data scheme and host at runtime i.e. I can add or delete the host at runtime. How to set the value of data scheme and host at runtime? I am using below code but it is not working
IntentFilter filter = new IntentFilter();
filter.addAction("android.intent.action.VIEW");
filter.addCategory("android.intent.category.DEFAULT");
filter.addCategory("android.intent.category.BROWSABLE");
filter.addDataScheme("http");
filter.addDataAuthority("www.facebook.com", null);
RecieveBroadcaster receiver = new RecieveBroadcaster();
registerReceiver(receiver, filter);

Strictly speaking, the string corresponding to ACTION_VIEW is an activity action by convention; the fact that you place it into the intent-filter element of an activity in your manifest, makes it an activity action! The system listens for these on your application's behalf, which is basically why you don't (can't) listen for them yourself. The Context.startActivity() method generates these Intents.
The rules of intent resolution actually determine whether a particular Intent matches any IntentFilters. For activity intents, there may be multiple matches, and that usually displays the "Chooser" interface, so the user can select a target.
There are three Intent "streams" that never cross: startActivity(), sendBroadcast() and startService(). These are all initiated via methods in Context and each has a specific target Activity, BroadcastReceiver and Service respectively.
It is a simple matter to set up a BroadcastReceiver (not ReceiveBroadcaster did you even try that code?) to get the events you are interested in, and then use Context.startActivity() with the Intent you want. You can even use a custom action, so you know it was triggered by the receiver, and not the user.
The only question is: is there a broadcast event you can arrange to receive? There may be a system event you can register for, or you may be able to generate a custom event yourself, via Context.sendBroadcast().
Remember you can inspect the incoming Intent your activity was started with, and "forward" the same or a modified Intent if it doesn't exactly match what you are looking for. As you correctly determined, you cannot dynamically alter an activity's set of IntentFilters, so you will have to inspect the host of every request.
Remember you can also register receivers in your manifest as well, and have that implementation called automatically by the system.

Related

Filtering an Android Broadcast receiver by action and extra

I am able to filter the broadcasts in my Android application by action. Eg:
<receiver android:name=".receiver.TasksModifiedReceiver">
<intent-filter>>
<action android:name="myapp.TASKS_MODIFIED_ACTION" />
</intent-filter>
</receiver>
However, can I further filter them by "extras" that are supplied in the intent?
Eg, application A sends a broadcast:
Intent taskBroadcastIntent = new Intent();
taskBroadcastIntent.setAction(ApplicationConstants.TASKS_MODIFIED_BROADCAST_ACTION);
taskBroadcastIntent.putExtra("TaskType", taskType);
taskBroadcastIntent.putExtra("UserName", CommonCache.getCommonCache().getValue(CommonCache.USER_NAME));
taskBroadcastIntent.putExtra("Environment", Constants.ENVIRONMENT);
context.sendBroadcast(taskBroadcastIntent);
Now, application B should only receive the broadcast if "TaskType" extra is set to "task1",
and application C should only receive the broadcast if "TaskType" extra is set to "task2".
Any helpful suggestions would be appreciated.
I cannot find this ability in the Android developer documentation.
I dont think that is possible. But here is what I will do,
Create a common receiver, You will be receiving the app context and intent. So get the taskType using getStringExtra("TaskType") and based on that trigger the respective broadcast intent.

making NFC Activity private without android:exported=false

I have an NFC activity which specifies an intent filter and data. This is something like
<intent-filter>
<action android:name="android.nfc.action..." />
<category android:name="android.intent.category.DEFAULT" />
<data
android:host="..."
android:path="..." />
</intent-filter>
Now the concern here is that since I have not included android:exported="false". it makes this activity susceptible to malicious attack from outside apps as the activity is now publicly available. The problem here is that if I put the exported attribute as false, I am not able to get the activity called from the outside intent (which is supposed to trigger the activity via onNewIntent() )
Is there a way to make the activity "safe" while not affecting its general triggering mechanism i.e. via outside intent?
If you need to get notified about tags, you need the intent filter. The tag dispatcher uses this to find interested activities to consider when a tag is scanned. If you make your activity private, there will be no way of notifying it, as you have seen. What 'malicious attacks' are you concerned about? Your activity is as safe as you code it to be: if you only handle NFC intents, you will be 'safe' (unless of course the NFC payload triggers a device-wiping code or something...)

Enale and disable SEARCH_LONG_PRESS in Android applications

I want my application to be opened by long-pressing search button, but I want to add the option to deactivate this.
I added to my shortcutActivity this:
<intent-filter>
<action android:name="android.intent.action.SEARCH_LONG_PRESS"/>
<category android:name="android.intent.category.DEFAULT"/>
</intent-filter>
so when I long-press the search button it is opened, but now, I have to be able to turn this feature on.
This is not the main activity, this activity just calls to the main one and then finish().
How can I do this?
Thank you.
Unfortunently intent filters are static
An intent filter is an instance of the IntentFilter class. However, since the Android system must know about the capabilities of a component before it can launch that component, intent filters are generally not set up in Java code, but in the application's manifest file (AndroidManifest.xml) as elements. (The one exception would be filters for broadcast receivers that are registered dynamically by calling Context.registerReceiver(); they are directly created as IntentFilter objects.)
This is from the IntentFilter Documetation.
Actually, this is easily done using the PackageManager:
ComponentName cnShortcutActivity = new ComponentName("my.package", "my.package.ShortcutActivity");
getPackageManager().setComponentEnabledSetting(cnShortcutActivity, PackageManager.COMPONENT_ENABLED_STATE_DISABLED, PackageManager.DONT_KILL_APP);

Android: Understanding Intent-Filters

I would like to create an Intent-Filter, so that certain links will trigger the start of my application (see this stackoverflow-thread for example: How to register some URL namespace (myapp://app.start/) for accessing your program by calling a URL in browser in Android OS? )
While trying, I figured out, that I dont quite understand how Intents and Intent-Filters (defined in the Manifest.xml) actually work. What is the difference between the following:
<action android:name="android.intent.action.VIEW" />
<action android:name="android.intent.action.MAIN" />
or the following:
<category android:name="android.intent.category.LAUNCHER" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.MAIN" />
And what is actually the difference between category and action Intent-Filters. I read this page http://developer.android.com/reference/android/content/Intent.html but I still missing a basic understanding.
Instead of looking at it from your app's point of view, flip it around and look at it from the Intent side.
When an Intent is created, the creator has no idea what apps are on the system to handle that Intent. But the creator does know what it wants to do (e.g., an app might want to let the user pick out a contact from somewhere on the device), and needs to reach out to other apps on the system to ask for what's desired.
To do this, Intents have several pieces of information attached to them. Among them are actions and categories.
The actions define in a general way the action the Intent wants to do, like VIEW a contact, PICK an image from the Gallery, etc.
The category is an additional piece of information that gives the Intent another way to differentiate itself. For example, when a link in the browser is clicked, the Intent that is created has the BROWSABLE category attached to it.
So, when the OS resolves the Intent, it will look for registered Activities or BroadcastReceivers that have an intent filter that includes all of pieces of information. If the Intent specifies the PICK action, Activities that do not have an intent-filter with the PICK action will be discarded from the list of candidates to handle the Intent.
In this way, the combined set of action, categories, type, and (possibly) scheme associated with an Intent serve to pinpoint the set of Activities that can handle the Intent. When you set up your intent-filter in your manifest, you are telling the OS which class of Intents you can handle.
I had to examine the code of android.content.IntentFilter.matchCategories(Set<String> categories) to understand the matching of categories:
Successful match, if your IntentFilter has categories and the Intent doesn't provide Categories
Successful match, if your IntentFilter has all categories of the Intent. The filter also can have additional categories.
No match, if your IntentFilter has no categories and the Intent has categories
No match, if your IntentFilter has not the categories the Intent has
Especially #1 and #3 aren't obvious.

What are Intent Filters exactly?

I've read lots of articles about intent filters and I really can't understand exactly what they do?
so please if anybody can explain to me with a clear example what is the role of intent filters exactly ?
thanks
I think it's well documented here. Summarizing, when (e.g.) you pass an intent to Context.startActivity, or Context.startService, or broadcast it with Context.sendBroadcast, etc, what activity or service (or, what set of broadcast receivers) is the intent delivered to?
Answer: if the intent is "explicit", i.e., it has the component name field set, then it's delivered to the target component it designates -- this typically happens in communication within an application.
Otherwise ("implicit" intent, as is typical for communication between applications), Android must determine the best activity or service (or set of receivers) on its own. How does Android do that? That's where filters come in, and I quote:
It does so by comparing the contents
of the Intent object to intent
filters, structures associated with
components that can potentially
receive intents. Filters advertise the
capabilities of a component and
delimit the intents it can handle.
They open the component to the
possibility of receiving implicit
intents of the advertised type. If a
component does not have any intent
filters, it can receive only explicit
intents. A component with filters can
receive both explicit and implicit
intents.
The web page in question continues with many details and, at the end, a complete, simple example, and I think it would be redundant to copy and paste them here.
Simply put, Intent filters are a way of telling the OS how to launch/communicate with the different activities/services/broadcast receivers in your app. So for example, if you want links that start with http://www.mycompany.com to lead people into your app, an intent filter is the way to accomplish that. Once its setup, anytime someone clicks on a link that starts with that (in any app) the user will be presented with the option to use your app to view that page. You've probably seen this with youtube urls. Likewise, if you want the 'share' link commonly seen in many apps to list your app, would use an intent filter to do that.
hope this helps...
An intent filter lets the system know what data or service requests a component can handle. A component might be an activity, service or broadcast receiver.
If you are writing an image viewer, you would add an intent filter (or several) to the manifest describing the images you can handle. If you are writing a file browser, you might package up the details of an image file in an intent, and the system would sift through intent filters until it found a best match to handle that image. The same goes for any type of data or service that might be passed from one component to the next.
Intent Filters is a way of telling OS that let me handle/provide these kind of Activities/Services
By adding given filter to Manifest tells OS that i can also handle Sms service and whenever you send sms it will show in a list or you can also explicitly use this as your sms service.
<intent-filter>
<action android:name="android.intent.action.SENDTO" />
<action android:name="com.example.code.SMS_INTENT" />
<category android:name="android.intent.category.DEFAULT" />
<data android:scheme="smsto" />
</intent-filter>
And to explicitly using this service call it like ...
Intent intent = new Intent("com.example.code.SMS_INTENT", Uri.parse(uri));
intent.putExtra("from", "code");
startActivity(intent);
IntentFilters are used to declare a pattern of Intent attributes that the declaring component will respond to. You can specify with an IntentFilter that a given Activity, Service or BroadcastReceiver will respond to a combination of action, data mime type, scheme, path, etc. For example if you register an Activity with IntentFilter for ACTION_SEND with data type "text/plain", your Activity will be called each time the users wants to send some text.
intentFilters advertise the capabilities of a component and delimit
the intents that can handle. an IntentFilter that a given Activity,
Service or BroadcastReceiver will respond to a combination of action,
data mime type, scheme, path, etc.
The intent by comparing the intent to intent filters based on three
aspects:
1:- The intent action 2:- The intent data (both URI and data type) 3:- The intent
category
action : Declares the intent action accepted, in the name
attribute. The value must be the literal string value of an action,
not the class constant.
data : Declares the type of data accepted, using one or more
attributes that specify various aspects of the data URI (scheme, host,
port, path, etc.) and MIME type.
category : Declares the intent category accepted, in the name
attribute. The value must be the literal string value of an action,
not the class constant.
For example, This activity handles "SEND" actions with text data.
<intent-filter>
<action android:name="android.intent.action.SEND"/>
<category android:name="android.intent.category.DEFAULT"/>
<data android:mimeType="text/plain"/>
</intent-filter>
Intent filter tells the android system for which intent or event the android components(activity,service ,broadcast receiver) should listen.

Categories

Resources