Why doesn't my app receive Intents? - android

i'm trying my app receive data shared by other apps. I.e. when somebody send a youtube link via Whatsapp, Youtube app can receive it and open to show.
I set this at the manifest:
<activity
android:name="com.example.app.MainActivity"
android:configChanges="orientation|screenSize|keyboardHidden"
android:label="#string/app_name"
android:launchMode="singleTask" >
<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>
And this code is into MainActivity onCreate method:
Intent intent = getIntent();
String action = intent.getAction();
String type = intent.getType();
if (Intent.ACTION_SEND.equalsIgnoreCase(action) && type != null) {
if ("text/plain".equals(type)) {
String codigoIn = intent.getStringExtra(Intent.EXTRA_TEXT);
if (codigoIn!=null) Toast.makeText(context, codigoIn, Toast.LENGTH_LONG).show();
}
}
I try to test sending myself links via Hangouts. When I click on link, the chooser only show Chrome and Internet navigators.
Please, what's wrong? Thank you very much
UPDATE: SOLVED
I already solved. I was very lost. I should not use the category ACTION_SEND but ACTION_VIEW. Now, Manifest is:
<intent-filter android:label="my app">
<data android:scheme="http" />
<data android:host="myHost.com" />
<data android:host="www.myHost.com" />
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.BROWSABLE" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
And I put at Activity -> onResume method:
Intent intent = getIntent();
String action = intent.getAction();
String type = intent.getType();
if (action.equalsIgnoreCase(Intent.ACTION_VIEW)){
String dato = intent.getData().toString();
Intent nuevoIntent = new Intent(context, Resultado.class);
nuevoIntent.putExtra("codigo", dato);
startActivity(nuevoIntent);
}
I hope it's useful to someone. It took two days to fix

You can try the code from this answer :
String action = intent.getAction();
if (action.equalsIgnoreCase(Intent.ACTION_SEND) && intent.hasExtra(Intent.EXTRA_TEXT)) {
String s = intent.getStringExtra(Intent.EXTRA_TEXT);
Toast.makeText(context, s, Toast.LENGTH_LONG).show();
}

Related

How to get intent content in receiving app when launched with getLaunchIntentForPackage()?

I have an intent filter for receiving intents with a specific URI. When another app opens my app with that specific URI, I use that deeplink/unilink, and redirect in the app using a Flutter plugin, depending on which query parameters are included in the URI.
In AndroidManifest:
<activity android:name=".MainActivity" android:theme="#style/LaunchTheme" android:configChanges="orientation|keyboardHidden|keyboard|screenSize|locale|layoutDirection|fontScale|screenLayout|density|uiMode" android:hardwareAccelerated="true" android:windowSoftInputMode="adjustResize" android:screenOrientation="portrait">
<meta-data android:name="io.flutter.embedding.android.SplashScreenDrawable" android:resource="#drawable/launch_background" />
<meta-data android:name="io.flutter.embedding.android.NormalTheme" android:resource="#style/NormalTheme"/>
<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="appspecifictheme" />
</intent-filter>
<intent-filter android:autoVerify="true">
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data
android:scheme="https"
android:host="www.myappslandingpage.com"
android:pathPrefix="/myappspath" />
<data
android:scheme="https"
android:host="www.myappslandingpage.com"
android:pathPrefix="/myspecificpathprefix" />
</intent-filter>
</activity>
This is working fine when launching from another app with uri https://www.myappslandingpage.com/myspecificpathprefix/?param1=xxxx&param2=xxxx&param3=xxxx
Now to my issue: another app wants to launch my app using this intent from package manager, and passing the query parameters as extras instead:
Intent launchIntent = getPackageManager().getLaunchIntentForPackage("receiving.apps.package.name");
launchIntent.putExtra("param1", "xxxx");
launchIntent.putExtra("param2", "xxxx");
launchIntent.putExtra("param3", "xxxx");
startActivity(launchIntent);
Question 1: When I launch my app from another app using above code, and the app is opened, but how do I get the content from the intent? I have tried using getIntent() in my MainActivity's onCreate(), but it doesn't seem to be working (stringExtras and other data in the intent is null).
#Override
protected void onCreate(Bundle savedInstanceState) {
checkIntent(getIntent());
FlutterMain.startInitialization(this);
super.onCreate(savedInstanceState);
}
void checkIntent(Intent i) {
final String param1 = i.getStringExtra("param1");
final String param2 = i.getStringExtra("param2");
final String param3 = i.getStringExtra("param3");
final Bundle extras = i.getExtras();
if(extras != null) {
for (String key : extras.keySet()) {
L.d(TAG, "checkIntent() extra: " + key + " : " + (extras.get(key) !=
null ? extras.get(key) : "NULL"));
//Above logs nothing - there are no keys in extras.
}
}
if(param1 != null) {
//Do something
} else {
L.d(TAG,"checkIntent(): param1 is null");
}
}
I have also tried adding URI to the intent using launchIntent.setData("uri") before running startActivity() from the other app, and changing the intent filter in my receiving app as below, but getIntent().getData() in MainActivity also returns null.
<category android:name="android.intent.category.LAUNCHER" />
Question 2: If I find a way to receive the intent and get the stringExtras, is there a simple way to "transform" that intent so that it is interpreted the same way as if the app was opened with a deeplink?

How to make my app appear in share board of other apps - example whatsApp

I am trying to read the text file of chat that exported from WhatsApp to my android app.
When I am trying to export the chat text file to my app, it does not appear in the share board of WhatsApp, although I have added the following to the manifest.xml:
<activity android:name=".MainActivity">
<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>
Also, here is the Java Code:
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
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 {
// 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) {
Log.d(TAG, sharedText);
// Update UI to reflect text being shared
}
}}
I went through many explanations, but I could not figure out what is the problem. Thank you.
Have you added the intent-filter under the correct activity in AndroidManifest.xml?
This should work in theory, but some apps might not send the proper intent for your app. It is possible that WhatsApp is using "VIEW" instead of "SEND", so try with this:
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<action android:name="android.intent.action.SEND" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data android:mimeType="text/plain" />
</intent-filter>
Let me know if it works for you.

dribbble authentication url not working in android

This is api doc. http://developer.dribbble.com/v1/oauth/
This is client Ids:
Client ID
3acdea730eaae4ea51dadf296be4e8edf7cd4b6ab030ce69c1de0d1a335b679d
Client Secret
4a9773911cd2304fa23047e703e35fffbd443f32a9c73207aa60b45852e17b64
Client Access Token
57fe6dc09292e4dadce94a3dc9fd895117792a48f7194bbc5b8bd229e6ef1388
Java code
String LOGIN_CALLBACK = "placeholder.com";
String LOGIN_URL = "https://dribbble.com/oauth/authorize?client_id="
+ "3acdea730eaae4ea51dadf296be4e8edf7cd4b6ab030ce69c1de0d1a335b679d"
+ "&redirect_uri=http%3A%2F%2F" + LOGIN_CALLBACK
+ "&scope=public+write+comment+upload";
context.startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(LOGIN_URL)));
in manifest
<activity
android:name=".DribbbleLogin"
android:exported="true"
android:launchMode="singleTop">
<intent-filter>
<data
android:host="dribbble-auth-callback"
android:scheme="plain" />
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
</intent-filter>
</activity>
what should be put in callback url and what should be passed in redirect url, so that the flow redirect to my app?
Your callbackUrl should be like plain://example.com and there is one more thing that needs to be done in the android manifest
android:host="example.com"
android:scheme="plain"
Just put in callbackurl "plain://example.com"
in manifest
<activity
android:name=".DribbbleLogin"
android:exported="true"
android:launchMode="singleTop">
<intent-filter>
<data
android:host="example.com"
android:scheme="plain" />
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
</intent-filter>
</activity>
In DribbbleLogin Activity
String loginCallback = "electedface.com";
String code;
Intent intent = getIntent();
if (intent != null
&& intent.getData() != null
&& !TextUtils.isEmpty(intent.getData().getAuthority())
&& loginCallback.equals(intent.getData().getAuthority())) {
code = intent.getData().getQueryParameter("code");
}
Call login url "https://dribbble.com//oauth/token" with dribbble_client_id, dribbble_client_secret and this code. you will get access_token, token_type, scope in json.
Thanks All

Open my app from a link

In my app the server send this link: appname://veryfy?email=test#mail.com&token=asdf-asdf-asdf-xcfghfgh but is it possible to get the values like email= test#mail.com and the token = sdfdkgdkgjgfd.
So far I've only added this intent filter inside my manifest but the app is not called:
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
<action android:name="android.intent.action.SEND"/>
<category android:name="android.intent.category.DEFAULT"/>
<data android:mimeType="text/plain" android:scheme="appname://"/>
</intent-filter>
Note this should open my app when the link is clicked in the browser
You can get the email and token by getting the Activity's intent like this:
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
<action android:name="android.intent.action.SEND"/>
<category android:name="android.intent.category.DEFAULT"/>
<data android:mimeType="text/plain" android:scheme="https"
android:host="api.myapp.com"
android:pathPrefix="/api/v2/verify"/>
</intent-filter>
Intent intent = getIntent();
Uri data = intent.getData();
String email = data.getQueryParameter("email");
String token = data.getQueryParameter("token");
The scheme in your filter should just be appname, not appname://
android:scheme="appname"
try this
<activity
android:name=".MainActivity"
android:label="#string/title_activity_main"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
<action android:name="android.intent.action.SEND"/>
<category android:name="android.intent.category.DEFAULT"/>
<data android:mimeType="text/plain" android:scheme="appname"/>
</intent-filter>
</activity>
To get your url parameter write this in your activity
Uri data = getIntent().getData();
String scheme = data.getScheme();
String host = data.getHost();
List<String> params = data.getPathSegments();
String first = params.get(0);
String second = params.get(1);
Try this:
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
<action android:name="android.intent.action.VIEW"/>
<category android:name="android.intent.category.DEFAULT"/>
<data android:mimeType="text/plain" android:scheme="appname"/>
</intent-filter>
and the link on the web:
<a href="appname://veryfy?email=test#mail.com&token=asdf-asdf-asdf-xcfghfgh">
Getting data in your Activity:
// check if this intent is started via custom scheme link
Intent intent = getIntent();
String action = intent.getAction();
if (action != null && action.equals(Intent.ACTION_VIEW) {
Uri uri = intent.getData();
String scheme = uri.getScheme();
if (scheme.equals("appname") {
String email = uri.getQueryParameter("email");
String token = uri.getQueryParameter("token");
}
}

Receiving data from other application comes null in android

I want to make my application like Instagram i can share pictures
directly from my application , like if i select any image from
gallery and when click on share my application shows on the list of chooser
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<activity
android:name=".ui.activities.SplashActivity"
android:label="#string/app_name"
android:launchMode="singleTop"
android:screenOrientation="sensorPortrait"
android:windowSoftInputMode="adjustResize" >
<intent-filter>
<action android:name="android.intent.action.SEND" />
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="*/*" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
Code for receiving data from intent is
private void initActivityState() {
if (getIntent() != null){
runShareActivityIntent();
}
}
public void runShareActivityIntent(){
Intent passIntent=getIntent();
String action = passIntent.getAction();
String type = passIntent.getType();
if (Intent.ACTION_SEND.equals(action) && type != null) {
Uri imageUri = (Uri) passIntent.getParcelableExtra(Intent.EXTRA_STREAM);
Intent intentcall = new Intent(this, MainActivity.class);
intentcall.putExtra(KEY_TYPE,type);
intentcall.putExtra(KEY_VALUE,imageUri.toString());
startActivity(intentcall);
finish();
}
}
Now in my case when my app is in background i am able to get
String type = passIntent.getType();
and i am able to get image uri but when my app is not running( kill app ) and i
select my application from chooser i get type as null
This worked for me:
<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="*/*" />
</intent-filter>
try adding permissions to your manifest file:
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
You have to change in Manifeast.xml
<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="audio/*" />
</intent-filter>
In your Main activity you have to receive the intent on oncreate() method.
Intent intent = getIntent();
String action = intent.getAction();
String type = intent.getType();
if (Intent.ACTION_SEND.equals(action) && type != null) {
if (type.startsWith("image/")) {
handleSendImage(intent); // Handle single image being sent
}
else if (type.startsWith("audio/")) {
handleSendAudio(intent); // Handle single audio being sent
}
}
Now whenever you call the application using send. You can see the icon of your app. When you get the intent you can change according to your need.
I hope this may help you.

Categories

Resources