How to open an activity using custom URI? - android

I am trying to start an activity within my app from a custom uri returned from my service.
For example my service is going to return me something like: "activity2". Basically I want to start the activity that has the correct filter listening for the "activity2" intent. I am kind of stuck on how I would do this. Can anyone give me an example of some sort?
In my Activity2 declaration I put this as an intent filter but it doesnt seem to work:
<intent-filter>
<action android:name="activity2"/>
<category android:name="android.intent.category.DEFAULT" />
<data android:scheme="foo"/>
</intent-filter>

Related

Can I start an AsyncTask through a share activity, without displaying the activity?

The question is pretty much in the title. I want to know if I can start and execute an AsyncTask or something else in the background with the Intent data from the share activity, without actually launching the share activity. I looked for some information, but the only thing I found is how to register a service with your app, which isn't really what I was looking for. Bonus question: can I also display a notification or a small disappearing textbox on the display upon the AsyncTask completion/addition
To avoid confusion, this is what I mean with share activity:
<activity android:name=".ShareActivity">
<intent-filter>
<action android:name="android.intent.action.SEND" />
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="image/*" />
</intent-filter>
</activity>
Thanks a lot in advance
Why don't you have your shared method in a separate class and then the activities can access it from there?
Just using psuedo code.
class MyTask {
void myAsyncMethod (){
}
}
and call it from your activities.
MyTask _myTask = new MyTask();
_myTask.myAsyncMethod();

Reading gmail attachment in my app

I am trying to read (first time - new to me) a gmail attachment (.gcsb extension) in my app. The intent filter looks like this:
<intent-filter>
<action android:name="android.intent.action.VIEW"></action>
<category android:name="android.intent.category.DEFAULT"></category>
<category android:name="android.intent.category.BROWSABLE"></category>
<data android:pathPattern="*.gcsb"></data>
<data android:mimeType="application/*"></data>
</intent-filter>
and that appears to get the 'download' and 'preview' buttons to appear next to the attachment in gmail (without that filter, the buttons do not appear).
In the activity (onCreate() / onRestart()) I do:
....
Intent intent = getIntent();
if (!Intent.ACTION_VIEW.equals(intent.getAction())) {
// Deal with the file from gmail here
}
....
to check if it is gmail that has caused this to start the activity or not.
However intent.getAction() always resolves to android.intent.action.MAIN, so it never does anything. There is another intent filter in the activity:
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
being for the app launch, but I don't understand why I never see the android.intent.action.VIEW from gmail?
The answer appears to be that the activity was set to be 'single instance' for various reasons. This means that whilst it is brought to the foreground again, it comes back with the intent it was originally started with, not the one created by gmail.
Removing the 'single instance' attribute allowed the correct intent to be used.

intent-filter for a service : using a custom mime type

I want to add a custom mime so that my Android Service or a BroadcastReceiver would detect my custom files.
I have seen answers to questions like How to add custom mime type? and How to add custom mime type?
The answers here are shown for an Activity. But what modifications are required for service or BroadcastReceiver? I tried the same code for them, but it did not work.
I am new to android development. Some nice and detailed explanation is welcome. Is it possible?
Where am I going wrong.
The code that I used is :
<service android:name="XcardReceivedService"
android:exported="true"
android:enabled="true">
<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="file" />
<data android:mimeType="*/*" />
<data android:pathPattern=".*\\.Xcard" />
<data android:host="*" />
</intent-filter>
</service>
but before the activity is started I want to add the data from that file to the database
You have to do that in the activity.
or that purpose I wanted to write a service or a broadcast receiver.
Files do not open in to a service or a broadcast receiver.
Is there any other possible way?
Put your code into your activity.
how should i differentiate between "how the activity was started" whether from other activity or when user clicked on this file.
Examine the Intent that was used to start your activity. You get this Intent via getIntent(). You can call getData() on the Intent to get the Uri associated with it. If this is not null, it should be the path to your file.

Android Intent launch from browser

This topic has been covered before, but I can't find an answer specific to what I'm asking.
Where I am: I followed hackmod's first piece of advice here: Make a link in the Android browser start up my app? and got this to work with a link in the webpage.
However, I'm having trouble understanding the second option (intent uri's). here's what I've got:
<activity android:name="com.myapps.tests.Layout2"
android:label="Auth Complete"
>
<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="mydomain.com"
android:path="/launch" />
</intent-filter>
</activity>
Now, with that I can go to "mydomain.com/launch" and it launches my activity. this all works well, except that I get the chooser. what I want is for it to just launch my activity without giving options.
From the explanation in the post I referenced it looks like thats what intent uris are for,but I can't find a straightforward example. what should my link in my webpage look like in order to launch this with no chooser?
I've seen a couple of examples that look something like this:
<a href="intent:#Intent;action=com.myapp.android.MY_ACTION;end">
However, that doesn't seem to work when I try it.
My test device is a Galaxy Tab 2.
any help would be appreciated.
I was also trying to launch the app in the recomended way. The following code worked for me.
Code inside the <activity> block of YourActivity in AndroidManifest.xml :
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<intent-filter>
<action android:name="your.activity.namespace.CUSTOMACTION" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
</intent-filter>
Code in the activities onCreate() method :
Intent intent = getIntent();
if(intent != null && intent.getAction() == "your.activity.namespace.CUSTOMACTION") {
extraValue = intent.getStringExtra("extraValueName");
Log.e("AwseomeApp", "Extra value Recieved from intent : " + extraValue);
}
For the code in HTML, write an intent for launching the specific Activity, with the action your.activity.namespace.CUSTOMACTION, and your application package name your.activity.namespace. Your can also put some extra in the intent. For example intent.putExtra("extraValueName", "WOW"). Then get the required URL by printing the value of intent.toUri(Intent.URI_INTENT_SCHEME) in the Log. It should look like :
intent:#Intent;action=your.example.namespace.CUSTOMACTION;package=your.example.namespace;component=your.example.namespace/.activity.YourActivity;S.extraValueName=WOW;end
So your HTML code should look like :
<a href="intent:#Intent;action=your.example.namespace.CUSTOMACTION;package=your.example.namespace;component=your.example.namespace/.activity.YourActivity;S.extraValueName=WOW;end">
Launch App
</a>
This is as per what #hackbod suggested in here and this page from developers.google.com.
Depending on your intent filter this link should work:
start my app
But you should note that the android system will ask the user if your app or any other browser should be started.
If you want to avoid this implement a custom protcol handler. So just your app will listen for that and the user won't get the intent chooser.
Try to add this data intent:
<data android:scheme="mycoolapp" android:host="launch" />
With the code above this link should work:
start my app
I needed a small change to abhishek89m'a answer to make this work.
<a href="intent:#Intent;action=your.example.namespace.CUSTOMACTION;package=your.example.namespace;component=your.example.namespace/.YourActivity;S.extraValueName=WOW;end">
Launch App
</a>
I removed ".activity" after the slash in component name.
And I want to add, that custom action is probably the best answer to this problem if you don't want the app chooser to show up.
p.s. I would add this as comment, but I'm a new user and I don't have enough reputation points.
<a href="your.app.scheme://other/parameters/here">
This link on your browser will launch the app with the specific schema
like that on your intent
<intent-filter>
<data android:scheme="your.app.scheme" />
<action android:name="android.intent.action.VIEW" />
</intent-filter>

How can I start the next matching activity if my app is the default one

I have an activity that works as a hook for various intents. Specifically, the VOICE_COMMAND and CALL_PRIVILEGED. (It is a requirement to use these.)
<activity android:name=".MyActivity"
android:enabled="true">
<intent-filter>
<action android:name="android.intent.action.VOICE_COMMAND" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.CALL_PRIVILEGED" />
<category android:name="android.intent.category.DEFAULT" />
<data android:scheme="tel" />
</intent-filter>
</activity>
I ask the user to set my app as the default action for these intents so they don't have to select it every time. This is also a requirement.
The problem is that in certain cases I want my activity to work transparently and pass the intent to the dialer or other app. This should be selectable by the user. I achieved this by using getPackageManager().setComponentEnabledSetting(myCompName, isEnabled, PackageManager.DONT_KILL_APP) on my activity in certain places of the code.
Is there a more elegant way to do this? I tried startNextMatchingActivity(getIntent()) but that does not start anything (returns false). Does this mean that if there is a default action, then everything else is ignored from the intent resolution?
Currently (on Android 2.3) there seems to be no other way of forwarding the intent than doing it manually. Additionally, to send CALL_PRIVILEGED intent the app must have special permissions that only system apps have. (The app will receive not allowed exceptions otherwise.)
All in all, the intent must be converted to some other intent that my app can send and start the next activity manually by retrieving the list of applicable activities from the PackageManager.queryIntentActivities() API.

Categories

Resources