I'm trying to pass data to my activity, but unfortunately I'm still unsuccessful. What I am trying to accomplish is to select a file in the file browser, share it and pass the data to my activity.
Inside my manifest I added an intent filter:
<activity android:name=".MyActivity" android:label="#string/app_name">
<intent-filter>
<action android:name="android.intent.action.SEND"/>
<category android:name="android.intent.category.DEFAULT"/>
<data android:mimeType="*/*"/>
</intent-filter>
</activity>
Inside my Java file i'm trying to get the data:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.myactivity);
Intent intent = getIntent();
Uri data = intent.getData();
if (data != null) {
// process the data
} else {
// no data received
}
}
When I select a file from My Files and share it, my app is visible in the list, when i click it it launches my activity, but intent.getData(); always returns null.
Am i missing something? Thanks.
you can get the data from the example given below:
Use an with a element. For example, to handle all links to twitter.com, you'd put this inside your in your AndroidManifest.xml:
<intent-filter>
<data android:scheme="http" android:host="twitter.com"/>
<action android:name="android.intent.action.VIEW" />
</intent-filter>
Then, when the user clicks on a link to twitter in the browser, they will be asked what application to use in order to complete the action: the browser or your application.
Of course, if you want to provide tight integration between your website and your app, you can define your own scheme:
<intent-filter>
<data android:scheme="my.special.scheme" />
<action android:name="android.intent.action.VIEW" />
</intent-filter>
Then, in your web app you can put links like:
<a href="my.special.scheme://other/parameters/here">
And when the user clicks it, your app will be launched automatically (because it will probably be the only one that can handle my.special.scheme:// type of uris). The only downside to this is that if the user doesn't have the app installed, they'll get a nasty error. And I'm not sure there's any way to check.
Edit: To answer your question, you can use getIntent().getData() which returns a Uri object. You can then use Uri.* methods to extract the data you need. For example, let's say the user clicked on a link to http://twitter.com/status/1234:
strong textUri data = getIntent().getData();
String scheme = data.getScheme(); // "http"
String host = data.getHost(); // "twitter.com"
List<String> params = data.getPathSegments();
String first = params.get(0); // "status"
String second = params.get(1); // "1234"
You can do the above anywhere in your Activity, but you're probably going to want to do it in onCreate(). You can also use params.size() to get the number of path segments ienter code heren the Uri. Look to javadoc or the android developer website for other Uri methods you can use to extract specific parts.
Launch custom android application from android browser
Related
What I want to do: I have json file in file manager, i want click on him, then must shown app chooser with my app on list to use. I want open json file in MyApp and get data.
Json dont have default app to deal with him.
Actually, I use sharing. I use long click to make tools visible and use share -> MyApp.
AndroidManifest.xml
<activity android:name=".MyClassToDealWithJson">
<intent-filter>
<action android:name="android.intent.action.SEND" />
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="application/json" />
</intent-filter>
</activity>
MyClassToDealWithJson.class
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
intent = getIntent();
String action = intent.getAction();
String type = intent.getType();
uri = intent.getParcelableExtra(intent.EXTRA_STREAM);
if (Intent.ACTION_SEND.equals(action) && type != null) {
if ("*/*".equals(type)) {
//dealing with json
}
Its hard to find sample or i dont searching with right words :) Thanks for help!
Yeah, ACTION_VIEW works good. I just didnt run code code, because my Mainfest had red underline :)
I created a music player and now I would like to implement a functionality that if I click a file (for example in Total Commander) it will open in my application. It already works on desktop but now I would like to also implement this feature in Android.
I know it must have been asked before but I couldn't find the answer.
I already found out that I need to define an intent-filter in AndroidManifest.xml:
<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="audio/*"/>
</intent-filter>
I did it and now my application opens (or I can choose it) if I click a music file. But now I need to do something in the java part (I do believe in the main activity onCreate function) too. How do I handle this?
Thank you.
The intent filter you specified, is already a step into the right direction.
Now other (Explorer-like-) Applications can start your app.
As a next step, you need to receive the data in the activities onCreate(...) function. This can be done like that:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//setContentView(R.layout.main);
// Get the intent that started this activity
Intent intent = getIntent();
Uri data = intent.getData();
// Figure out what to do based on the intent type
if (intent.getType().equals("audio")) {
// Handle intents with audio ...
String filePath = data.toString();
// Do some handling here !
}
}
First you need to capture the Intent, that other apps use, to call your app. The intent contains the path of your file (as URI) structure. The only thing you need to do then is to get the URI String and get the path of your music file out of it.
Information about the intent filtering can be read here:
https://developer.android.com/training/basics/intents/filters
I want to broadcast my custom intent when i click a link of my html page from browser. I know android system will broadcast "android.intent.action.VIEW" for this and i can receive this in my application but doing this will list my application for every clickable link so i want to broadcast my custom intent action.
I want to broadcast my custom intent when i click a link of my html
page from browser.
You can do that in two ways, the exact choice depend on your use case.
Create an <intent-filter> to open selected links (links of your website)
Change your link on the website to <a href="intent://...>
The first method gives you flexibility to leave the links on your website as it is and it will also helps in deeplinking and AppIndex. Where as the second method will make you change all the links in the website.
I know android system will broadcast "android.intent.action.VIEW" for
this and i can receive this in my application but doing this will list
my application for every clickable link so i want to broadcast my
custom intent action.
It will not list your application for every clickable link on the web, it will only open your app for your website links. If you don't want to do that and only open app for one specific link of your website, you should use method 2 above.
Just a note for the second mehtod
Make sure to implement a fallback url, as suggested by Google
When an intent could not be resolved, or an external application could
not be launched, then the user will be redirected to the fallback URL
if it was given.
The link should look like this
<a href="intent://scan/#Intent;scheme=zxing;package=com.google.zxing.client.android;S.browser_fallback_url=http%3A%2F%2Fzxing.org;end">
Notice the S.browser_fallback_url for fallback
I have solved this by following
create test.html file with this single line
Open Your Application Directly
Here "example.com/test?id=12345" can be any thing you want this will be passed as intent data in our in our onCreate() method so i have given id for example.
"scheme" can be any string we need to write same scheme in our menifest.xml for intent-filter
"package" is your app package name to differentiate it from other app with same scheme
Note : If app is not installed in device then it will open google playstore from given valid package name
in AndroidMenifest.xml file
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<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="myapp" />
</intent-filter>
you can add intent filter for any activity i have given to launcher activity
note the <data android:scheme="myapp" /> scheme name must be same as given in HTML file before.
Done! open html file in any browser of your device & click on link it will directly open your app.
You can get intent data in your activity's onCreate() method like
Intent intent = getIntent();
if (intent != null && intent.getData() != null) {
Uri data = intent.getData();
String path = data.getPath();
String id = data.getQueryParameter("id");
Log.d("ID", ": " + id);
}
currently i'm building blog/news portal app based on web version in android, i want if someone search a query on google/bing or another search engine in android browser and found my web www.newsportal.com/this-is-article-slug then browser tell there are choices to open it, open in browser itself or open on my newsportal app instead, then my app catch my link www.newsportal.com/this-is-article-slug, extract this slug this-is-article-slug then open new activity, search the article via API and done. I tried read some reference like http://developer.android.com/training/sharing/receive.html but I confuse where do i start,,
Thanks for your help..
Open search result in application you need to add in your activity tag in Manifest.xml like below
<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:host="your host"
android:scheme="your scheme" />
</intent-filter>
And handle result in in your activity like
Intent intent = getIntent();
Uri data = intent.getData();
String action = intent.getAction();
if (Intent.ACTION_VIEW.equalsIgnoreCase(action) && data != null) {
if (data.getScheme().equals("your scheme")
&& data.getAuthority().equals("your host")) {
}
}
Suppose I have a web page, and I load that page in a browser on an android device. What I expect is, when I click a button in the web page, an app can be opened.
Is there any way to do that? Thanks a lot.
you can achieve this using <intent-filter> with a <data>.
For example, to handle all links to sample.com, you'd put this inside your in your AndroidManifest.xml:
<intent-filter>
<data android:scheme="http" android:host="sample.com"/>
<action android:name="android.intent.action.VIEW" />
</intent-filter>
this one was well explained by Felix in his answer here
hopes this helps you...
If you have the option to customize the app in question, you could add an Intent Filter for a specific URI scheme which is unique to your app. Then in the click event for the button of the web page, use this URI scheme to launch your app.
For example, Google Play uses a market:// scheme to open the Google Play app from links.
using IntentFilters it is possible..
Chack out the following code:-
<intent-filter>
<data android:scheme="**myapp**" />
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" /> <--Not positive if this one is needed
...
</intent-filter>
now you can start your app like myapp://
you can use getIntent().getData() which returns a Uri object. You can then use Uri.* methods to extract the data you need. For example, let's say the user clicked on a link to http://twitter.com/status/1234:
Uri data = getIntent().getData();
String scheme = data.getScheme(); // "http"
String host = data.getHost(); // "twitter.com"
List<String> params = data.getPathSegments();
String first = params.get(0); // "status"
String second = params.get(1); // "1234"
You can do the above anywhere in your Activity, but you're probably going to want to do it in onCreate(). You can also use params.size() to get the number of path segments in the Uri. Look to javadoc or the android developer website for other Uri methods you can use to extract specific parts.