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 :)
Related
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
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")) {
}
}
We must write an Android app who knows how to open .xyz files.
However when we assign the .xyz files to our application, and after that we tap on an .xyz file in the Android's File Explorer, the application is properly started but the program doesn't see the file name as a parameter.
The code
ShowMessage(ParamStr(0)); //for debug...
ShowMessage(ParamStr(1));
Instead of showing in message boxes the application's full path and after this the full path of the tapped (clicked) file name it shows two empty strings.
How can we get the full path of the tapped (clicked) file?
UPDATE:
(A part of) The manifest looks like this:
<activity android:name="com.embarcadero.firemonkey.FMXNativeActivity"
android:label="MMBook"
android:configChanges="orientation|keyboardHidden">
<!-- Tell NativeActivity the name of our .so -->
<meta-data android:name="android.app.lib_name"
android:value="MMBook" />
<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" />
<data android:scheme="file" android:host="*" android:pathPattern=".*\\.mma" android:mimeType="*/*" />
</intent-filter>
</activity>
You need to call SharedActivity.getIntent() to check if your app was started by an Intent or not. If a non-nil Intent is returned, you can retrieve its data (ie, the filename) and act accordingly. See my answer to the following question for an example:
Handling Custom URI in Delphi XE5 Android App
https://stackoverflow.com/a/20465775/65863
On Android, launching an activity and passing a file from a different app is implemented with Intents and Intent filters. (See Allowing Other Apps to Start Your Activity)
So usually your app must implement an Intent handler. In order to decide what action to take in your activity, you can read the Intent that was used to start it.
The URI of the selected file then can be retrieved with Intent.getData()
Java Example:
// 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().indexOf("image/") != -1) {
// Handle intents with image data ...
} else if (intent.getType().equals("text/plain")) {
// Handle intents with text ...
}
See also: http://codeverge.com/embarcadero.delphi.firemonkey/android-intent-filter-associate/1057456
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
I'm trying to develop an Android application that provides an extra option when pasting data anywhere.
I know how to capture data from the clipboard. I just need to know how to listen to longclick events in any text area in other applications such as browsers,facebook,twitter...etc so that my application would be triggered giving the user the option to paste the data on the clipboard after processing it, as an alternative to pasting it in the normal way.
We've come a long way since you asked this question but there are actually 2 ways to do that:
call to to ClipboardManager.addPrimaryClipChangedListener() and sign up as a listener when a user copies text. can be found in the Documentation
Add the ACTION_PROCESS_TEXT Intent Filter so the user can pick a custom action you created/start your app. More can be found in this Blog Post
You need to add an intent filter to the activity in question, like so:
<activity android:name=".PostActivity">
<intent-filter>
<action android:name="android.intent.action.SEND" />
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="text/plain" />
</intent-filter>
</activity>
Then you just need to handle the data sent to you in the intent in your Activity
Uri data = getIntent().getData();
Bundle extras = getIntent().getExtras();
String messageText = "";
if (data != null) {
messageText = data.toString();
} else if (extras != null) {
messageText = extras.getString(Intent.EXTRA_TEXT);
}