Receiving data from other application comes null in android - 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.

Related

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.

No response from intent to prompt user to set default handler for SMS

I'm currently having trouble getting the app to send the user a dialogue to set the default handler for SMS on API 29.
I initially followed https://developer.android.com/guide/topics/permissions/default-handlers.
It didn't work so I looked around and saw this: How to set Default SMS prompt for KitKat.
So I followed https://android-developers.googleblog.com/2013/10/getting-your-sms-apps-ready-for-kitkat.html and added the snippet to my manifest XML (without actual implementation, as the StackOverflow user said in the post). Unfortunately every time I click the button that starts the ACTION_CHANGE_DEFAULT intent, nothing happens except he log.
My main activity has the following onCreate:
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
var btn = findViewById<Button>(R.id.button3)
btn.setOnClickListener {
Log.d("TAG", "Sending change default intent")
val setSmsAppIntent = Intent(Telephony.Sms.Intents.ACTION_CHANGE_DEFAULT)
setSmsAppIntent.putExtra(Telephony.Sms.Intents.EXTRA_PACKAGE_NAME, packageName)
startActivityForResult(setSmsAppIntent, CHANGE_SMS_DEFAULT_RESULT)
}
}
Manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.testapplication">
<uses-permission android:name="android.permission.READ_SMS" />
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:roundIcon="#mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<service
android:name=".FetchSMS"
android:exported="false"></service>
<activity
android:name=".DisplayMessageActivity"
android:parentActivityName=".MainActivity" />
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<!-- These are ONLY used to set app as default SMS -->
<receiver android:name=".SmsReceiver"
android:permission="android.permission.BROADCAST_SMS">
<intent-filter>
<action android:name="android.provider.Telephony.SMS_DELIVER" />
</intent-filter>
</receiver>
<!-- BroadcastReceiver that listens for incoming MMS messages -->
<receiver android:name=".MmsReceiver"
android:permission="android.permission.BROADCAST_WAP_PUSH">
<intent-filter>
<action android:name="android.provider.Telephony.WAP_PUSH_DELIVER" />
<data android:mimeType="application/vnd.wap.mms-message" />
</intent-filter>
</receiver>
<!-- Activity that allows the user to send new SMS/MMS messages -->
<activity android:name=".ComposeSmsActivity" >
<intent-filter>
<action android:name="android.intent.action.SEND" />
<action android:name="android.intent.action.SENDTO" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data android:scheme="sms" />
<data android:scheme="smsto" />
<data android:scheme="mms" />
<data android:scheme="mmsto" />
</intent-filter>
</activity>
<!-- Service that delivers messages from the phone "quick response" -->
<service android:name=".HeadlessSmsSendService"
android:permission="android.permission.SEND_RESPOND_VIA_MESSAGE"
android:exported="true" >
<intent-filter>
<action android:name="android.intent.action.RESPOND_VIA_MESSAGE" />
<category android:name="android.intent.category.DEFAULT" />
<data android:scheme="sms" />
<data android:scheme="smsto" />
<data android:scheme="mms" />
<data android:scheme="mmsto" />
</intent-filter>
</service>
</application>
</manifest>
Log:
Would appreciate some help. Thanks in advance.
My minSdkVersion is 19
Edit: I just tried this on the emulator on KitKat (API 19) and it worked - but it doesn't on Q (API 29)
Try below piece of code.
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.Q) {
RoleManager roleManager = mContext.getSystemService(RoleManager.class);
// check if the app is having permission to be as default SMS app
boolean isRoleAvailable = roleManager.isRoleAvailable(RoleManager.ROLE_SMS);
if (isRoleAvailable){
// check whether your app is already holding the default SMS app role.
boolean isRoleHeld = roleManager.isRoleHeld(RoleManager.ROLE_SMS);
if (isRoleHeld){
Intent roleRequestIntent = roleManager.createRequestRoleIntent(RoleManager.ROLE_SMS);
startActivityForResult(roleRequestIntent,requestCode);
}
}
} else {
Intent intent = new Intent(Telephony.Sms.Intents.ACTION_CHANGE_DEFAULT);
intent.putExtra(Telephony.Sms.Intents.EXTRA_PACKAGE_NAME, defaultSystemApp);
startActivityForResult(intent, requestCode);
}
Add this permission is manifest
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
<category android:name="android.app.role.SMS"/>
</intent-filter>
I had to modify Praveen's code to make it work. Main difference is with if(isRoleHeld). Here is how it look like.
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.Q) {
RoleManager roleManager = getApplicationContext().getSystemService(RoleManager.class);
// check if the app is having permission to be as default SMS app
assert roleManager != null;
boolean isRoleAvailable = roleManager.isRoleAvailable(RoleManager.ROLE_SMS);
if (isRoleAvailable){
// check whether your app is already holding the default SMS app role.
boolean isRoleHeld = roleManager.isRoleHeld(RoleManager.ROLE_SMS);
if (!isRoleHeld){
Intent roleRequestIntent = roleManager.createRequestRoleIntent(RoleManager.ROLE_SMS);
startActivityForResult(roleRequestIntent,resultCode);
}
}
} else {
Intent intent = new Intent(Telephony.Sms.Intents.ACTION_CHANGE_DEFAULT);
intent.putExtra(Telephony.Sms.Intents.EXTRA_PACKAGE_NAME, getApplicationContext().getPackageName());
startActivityForResult(intent, resultCode);
}
try this code it works fine with me
if(Build.VERSION.SDK_INT < Build.VERSION_CODES.Q){
//String mypackagename = getPackageName();
if(Telephony.Sms.getDefaultSmsPackage(this)!=null){
if (Telephony.Sms.getDefaultSmsPackage(this).equals(getPackageName())){
//todo go nain activity
}else{
Intent setSmsAppIntent = new Intent(Telephony.Sms.Intents.ACTION_CHANGE_DEFAULT);
setSmsAppIntent.putExtra(Telephony.Sms.Intents.EXTRA_PACKAGE_NAME,getPackageName());
startActivityForResult(setSmsAppIntent, 1);
}
}else{toast("no tlphony");}
}
else{
RoleManager rolemanager = getApplicationContext().getSystemService(RoleManager.class);
if (rolemanager.isRoleAvailable(RoleManager.ROLE_SMS)){
if (rolemanager.isRoleHeld(RoleManager.ROLE_SMS)){
//todo go nain activity
}
else{
Intent roleRequestIntent = rolemanager.createRequestRoleIntent(RoleManager.ROLE_SMS);
startActivityForResult(roleRequestIntent,1);
}
}
}

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");
}
}

Why doesn't my app receive Intents?

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();
}

Categories

Resources