My app is registering an intent to receive URLs so when the user shares a url, my app will be in the list of apps.
<intent-filter
android:label="my app">
<action android:name="android.intent.action.SEND" />
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="text/plain" />
</intent-filter>
-
in my MainActivity's onCreate() method:
String action = intent.getAction();
if (action.equalsIgnoreCase(Intent.ACTION_SEND)) {
Uri data = intent.getData();
String s = data.toString();
output.setText(s); //output: a TextView that holds the URL
}
-
The problem I got is: data is null which is strange. Since this code works perfectly with ACTION_VIEW. However, it didn't work with ACTION_SEND.
How could I modify it to work?
You can try
String action = intent.getAction();
if (action.equalsIgnoreCase(Intent.ACTION_SEND) && intent.hasExtra(Intent.EXTRA_TEXT)) {
String s = intent.getStringExtra(Intent.EXTRA_TEXT);
output.setText(s); //output: a TextView that holds the URL
}
Related
I want my app to be shown in every kind of sharing like plain text, images, videos or any files.
I also want to handle them accordingly. How can I do that? I'm completely new with sharing related stuff and I can't find any proper documentation for it.
Add this intent filter for receiving all types of sharing intent.
<activity
android:name=".YourActivity">
<intent-filter>
<action android:name="android.intent.action.SEND" />
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="*/*" />
</intent-filter>
</activity>
And in your receiving activity
Intent intent = getIntent();
String action = intent.getAction();
String type = intent.getType();
if (Intent.ACTION_SEND.equals(action) && type != null) {
if ("text/plain".equals(type)) {
String sharedText = intent.getStringExtra(Intent.EXTRA_TEXT);
} else {
Uri fileUri = intent.getParcelableExtra(Intent.EXTRA_STREAM);
}
}
You will get shared text and file Uri using the above code
I have an app that receives simple data (text) from other apps through the other app sharing to my app. Part of the manifest looks like this:
<activity
android:name=".presentation.ui.activities.MainActivity"
android:launchMode="singleTop">
<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.SEND" />
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="text/plain" />
</intent-filter>
</activity>
When the activity hasn't been created yet, I share the text data from the other app to my app and handle the intent with action intent.action.Send in the onCreate method. However, I also want to be able to share the text data while the app is in the background. When the app is in the background and I share the text data from the other app, the action is intent.action.MAIN in the onRestart or onResume methods.
This is the handleIntent method
private void handleIntent(){
Intent intent = getIntent();
String action = intent.getAction();
String type = intent.getType();
if (Intent.ACTION_SEND.equals(action) && type != null) {
if ("text/plain".equals(type)) {
String text = intent.getStringExtra(Intent.EXTRA_TEXT);
// logic
}
}
}
How do I receive the text data from the other app while my app is in the background?
I have figured it out.
I overrode the newIntent method
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
handleIntent(intent);
}
Sending Simple Data to Other Apps
Intent sendIntent = new Intent();
sendIntent.setAction(Intent.ACTION_SEND);
sendIntent.putExtra(Intent.EXTRA_TEXT, "This is my text to send.");
sendIntent.setType("text/plain");
startActivity(Intent.createChooser(sendIntent, getResources().getText(R.string.send_to)));
Receiving Simple Data from Other Apps
Update Your Manifest
<intent-filter>
<action android:name="android.intent.action.SEND" />
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="text/plain" />
</intent-filter>
Handle the Incoming Content
void onCreate (Bundle savedInstanceState) {
// Get intent, action and MIME type
Intent intent = getIntent();
String action = intent.getAction();
String type = intent.getType();
if (Intent.ACTION_SEND.equals(action) && type != null) {
if ("text/plain".equals(type)) {
handleSendText(intent); // Handle text being sent
}
}
}
void handleSendText(Intent intent) {
String sharedText = intent.getStringExtra(Intent.EXTRA_TEXT);
if (sharedText != null) {
// Update UI to reflect text being shared
}
}
I have created a gallery app. It opens images and photos but System isn't get it as a gallery app. Could anyone help me to set it as a gallery app?
Thank you!
update your manifest, This will tell other apps to receive content
<activity android:name=".ui.MyActivity" >
<intent-filter>
<action android:name="android.intent.action.SEND" />
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="image/*" />
</intent-filter>
<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>
<action android:name="android.intent.action.SEND_MULTIPLE" />
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="image/*" />
</intent-filter>
Handle the Incoming Content.
void onCreate (Bundle savedInstanceState) {
// Get intent, action and MIME type
Intent intent = getIntent();
String action = intent.getAction();
String type = intent.getType();
if (Intent.ACTION_SEND.equals(action) && type != null) {
if ("text/plain".equals(type)) {
handleSendText(intent); // Handle text being sent
} else if (type.startsWith("image/")) {
handleSendImage(intent); // Handle single image being sent
}
} else if (Intent.ACTION_SEND_MULTIPLE.equals(action) && type != null) {
if (type.startsWith("image/")) {
handleSendMultipleImages(intent);
// Handle multiple images being sent
}
} else {
// Handle other intents, such as being started from the home screen
}
}
void handleSendText(Intent intent) {
String sharedText = intent.getStringExtra(Intent.EXTRA_TEXT);
if (sharedText != null) {
// Update UI to reflect text being shared
}
}
void handleSendImage(Intent intent) {
Uri imageUri = (Uri) intent.getParcelableExtra(Intent.EXTRA_STREAM);
if (imageUri != null) {
// Update UI to reflect image being shared
}
}
void handleSendMultipleImages(Intent intent) {
ArrayList<Uri> imageUris = intent.getParcelableArrayListExtra(Intent.EXTRA_STREAM);
if (imageUris != null) {
// Update UI to reflect multiple images being shared
}
}
official docs:
https://developer.android.com/training/sharing/receive.html
You should use Intents and Intents Filters
In the link above you should read about "Receiving an implicit intent"
To advertise which implicit intents your app can receive, declare one or more intent filters for each of your app components with an element in your manifest file. Each intent filter specifies the type of intents it accepts based on the intent's action, data, and category. The system delivers an implicit intent to your app component only if the intent can pass through one of your intent filters.
<activity android:name="ShareActivity">
<intent-filter>
<action android:name="android.intent.action.SEND"/>
<category android:name="android.intent.category.DEFAULT"/>
<data android:mimeType="text/plain"/>
</intent-filter>
</activity>
^ the code above (taken from the documentation) show how to make sure your app open when other activity use SEND intent.
change the action and mimeType to get the resault you want (sending photo?, display photo? etc).
I want Open this link in my application, not open in browser. for this job i want use DeepLink.
But when start application, show me null for URI.
My link : Link
I write this code in manifest :
<activity
android:name=".Activities.PostShow_page"
android:screenOrientation="portrait"
android:theme="#style/AppThemeWithStatus">
<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="www.kolony.ir"
android:scheme="http" />
<data
android:host="kolony.ir"
android:scheme="http" />
</intent-filter>
</activity>
Activity codes:
// Deep Linking
Intent intent = getIntent();
String action = intent.getAction();
Uri data = intent.getData();
Log.e("Deep", "Link : " + data);
but when running application, in LogCat show this : Link : null
How can i fix null for this URI and show uri in logCat ?
Simply instead of using getIntent() use intent parameter of onNewIntent(Intent intent) event handler
Try this:
Intent intent = getIntent();
Uri data = (Uri) intent.getExtras().get(Intent.ACTION_VIEW);
Try this
intent.getStringExtra(Intent.EXTRA_TEXT)
You will get the exact location. It helps me hope so it helps you too.
String action = intent.getAction();
String data = intent.getDataString();
if (Intent.ACTION_VIEW.equals(action) && data != null) {
String recipeId = data.substring(data.lastIndexOf("/") + 1);
}
I've already enabled deep linking & it opens app from url. However it opens only the main url i.e. m.example.com & not m.example.com\products\iphone-5s\.
I'm using WebView to load my mobile website into an Android app.
Here's a code of xml file:
<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="m.example.com"
android:pathPrefix="/"
android:scheme="http" />
</intent-filer>
Java file:
private String url='m.example.com';
#Override
protected void onCreate(Bundle savedInstanceState) {
Intent intent = getIntent();
String action = intent.getAction();
Uri data = intent.getData();
mainWebView.loadUrl(url);
}
Can anybody help to resolve this?
Get the URL link with this code, right after Uri data = intent.getData();
String link = intent.getDataString();
Then, set simple if/else
if (link == null) {
mWebView.loadUrl("yourMainURL");
} else {
mWebView.loadUrl(link);
}