Cannot Get ACTION_VIEW Data From Intent - android

In my manifest file I have the following:
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="audio/*" />
</intent-filter>
At the top of my main activity in onCreate, I have:
mIntent = new Intent(Intent.ACTION_VIEW);
In my onResume in main activity, I have:
if(mIntent != null){
if(mIntent.getExtras() != null){
Log.d("INTENTDATA", "" +mIntent.getExtras());
}
}
Even though mIntent isn't null, mIntent.getExtras() returns null. When I tried mIntent.getData(), it also returned null.
Is there something wrong with my syntax?

Use getIntent at onCreate instead of creating a new one.

SOLUTION:
I needed to call getIntent();
mIntent = getIntent();
if (Intent.ACTION_VIEW.equals(mIntent.getAction())) {
Log.d("INTENTDATA", "ACTION_VIEW");
if(mIntent.getData() != null){
Log.d("INTENTDATA", "" +mIntent.getData());
}
}

Related

Receiving Simple Data Through Intent for Android App while App is in background

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
}
}

Android deep linking is not working

Android deep linking is not working
===================================
Android link:-notification://id=notificationid
1:-In manifest
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
<category android:name="android.intent.category.BROWSABLE" />
<data
android:host="id"
android:scheme="notification" />
</intent-filter>
2:-and coding side
#Override
protected void onResume() {
super.onResume();
Intent intent = getIntent();
String string=intent.getAction();
Uri data = intent.getData();
Log.d("hello","cgbdsuyfdkv");
}
but its not working please any body can help me !!!!!
you can simply modify your url
Android link:-notification://id=notificationid instead of
Android link:-notification://page?id=notificationid //page is host name
I tried a lot and find out this is working for me.
manifest code
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.LAUNCHER" />
<category android:name="android.intent.category.BROWSABLE" />
<data
android:host="page"
android:scheme="notification" />
</intent-filter>
2.java code #get your notification id here
Intent intent = getIntent();
String string = intent.getAction();
Uri data = intent.getData();
if (data != null) {
String path = data.getPath();
String path1 = data.getPath();
providerUrl = intent.getData().toString();
UrlQuerySanitizer sanitizer = new UrlQuerySanitizer();
sanitizer.setAllowUnregisteredParamaters(true);
sanitizer.parseUrl(providerUrl);
String id = sanitizer.getValue("id");
if (id != null)
Log.d("notification id", id);
}
Add onNewIntent() method to your Activity and then use intent object which is variable in OnNewintent() method , which has updated method.
onNewIntent() is meant as entry point for singleTop or singleTask activities which already run somewhere else in the stack and therefore can't call onCreate(). From activities lifecycle point of view it's therefore needed to call onPause() before onNewIntent(). I suggest you to rewrite your activity to not use these listeners inside of onNewIntent(). For example most of the time my onNewIntent() methods simply looks like this:
#Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
//use this intent object to get notificationid for second time
}
1st step:
<intent-filter>
<data android:scheme="your_uri_scheme" />
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
</intent-filter>
2nd step:
Uri data = this.getIntent().getData();
if (data != null && data.isHierarchical()) {
String uri = this.getIntent().getDataString();
Log.i("MyApp", "Deep link clicked " + uri);
}
I think you Intent Filter is incomplete
<!-- To open app using link -->
<intent-filter>
<action android:name="android.intent.action.VIEW"></action>
<category android:name="android.intent.category.DEFAULT"></category>
<category android:name="android.intent.category.BROWSABLE"></category>
<data android:scheme="http"
android:host="yourdomain.com"
android:pathPrefix="/someurlparam">
</data>
</intent-filter>

How to fix null Uri data = intent.getData() in Android

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

How to send data between one application to other application in android?

i've tried to sending data between App1 to App2 via Intent in Android
i used this code but i couldn't resolve my problem.
App1 MainActivity :
Intent i2 = new Intent("com.appstore.MainActivity");
i2.setPackage("com.appstore");//the destination packageName
i2.putExtra("Id", "100");
startActivity(i2);
App2 MainActivity :
Bundle data = getIntent().getExtras;
if(data!=null){
String myString = b.getString("Id");
}
Manfiest App2 MainActivity:
<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="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>
</activity>
Final code:
App 1 :
Intent intent = new Intent();
intent.setClassName("com.appstore", "com.appstore.MyBroadcastReceiver");
intent.setAction("com.appstore.MyBroadcastReceiver");
intent.putExtra("KeyName","code1id");
sendBroadcast(intent);
App 2:
Reciver:
public class MyBroadcastReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context, "Data Received from External App", Toast.LENGTH_SHORT).show();
}
}
Manifest :
<receiver
android:name=".MyBroadcastReceiver"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="first_app_packagename" />
</intent-filter>
</receiver>
MainActivity :
MyBroadcastReceiver mReceiver = new MyBroadcastReceiver();
registerReceiver(mReceiver,
new IntentFilter("first_app_packagename"));
My requirement was to send the "user id" from App1 to App2 and get "username" back to App1.
I needed to launch my app directly without any chooser. I was able to achieve this using implicit intent and startActivityForResult.
App1 > MainActivity.java
private void launchOtherApp() {
Intent sendIntent = new Intent();
//Need to register your intent filter in App2 in manifest file with same action.
sendIntent.setAction("com.example.sender.login"); // <packagename.login>
Bundle bundle = new Bundle();
bundle.putString("user_id", "1111");
sendIntent.putExtra("data", bundle);
sendIntent.setType("text/plain");
if (sendIntent.resolveActivity(getPackageManager()) != null) {
startActivityForResult(sendIntent, REQUEST_CODE);
}
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == REQUEST_CODE) {
if (resultCode == Activity.RESULT_OK) {
Bundle bundle = data.getBundleExtra("data");
String username = bundle.getString("user_name");
result.success(username);
}
} else {
super.onActivityResult(requestCode, resultCode, data);
}
}
I had two activity in App2 ie. MainActivity and LoginActivity.
App2 > AndroidManifest.xml
<activity android:name=".LoginActivity">
<intent-filter>
<!--The action has to be same as App1-->
<action android:name="com.example.sender.login" />
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="text/plain" />
</intent-filter>
</activity>
Sorry for this I had a little mix up with Java and Kotlin. My second app was in Kotlin, not that it will effect in any way.
App2 > LoginActivity.java
override fun onResume() {
super.onResume()
var userId = "No data received"
val intent = intent
if (intent != null
&& intent.action != null
&& intent.action.equals("com.example.sender.login")
) {
val bundle = intent.getBundleExtra("data")
if (bundle != null) {
userId = bundle.getString("user_id")
userId = " User id is $userId"
}
}
tvMessage.text = "Data Received: $userId"
}
fun onClickBack(view: View) {
val intent = intent
val bundle = Bundle()
bundle.putString("sesion_id", "2222")
intent.putExtra("data", bundle)
setResult(Activity.RESULT_OK, intent)
finish()
}
When you do this:
Intent i2 = new Intent("com.appstore.MainActivity");
i2.setPackage("com.appstore");//the destination packageName
i2.putExtra("Id", "100");
startActivity(i2);
you are calling the single-argument constructor of Intent. In this constructor, the argument is interpreted as the Intent ACTION. You then set the package name in the Intent.
When you call startActivity() with this Intent, Android will look for an Activity that contains an <intent-filter> with the specified ACTION. There are no installed applications that have an Activity defined like this in the manifest:
<activity>
<intent-filter>
<action android:name="com.appstore.MainActivity"/>
<category android:name="android.intent.category.DEFAULT"/>
</intent-filter>
</activity>
So Android will not be able to find and launch the Activity that you want.
As you want to specify explicitly the component that you want to use, instead of using the 1-argument Intent constructor, you should do this instead:
Intent i2 = new Intent();
i2.setClassName("com.appstore", "com.appstore.MainActivity");
i2.putExtra("Id", "100");
startActivity(i2);
Using setClassName() you provide the package name and the class name of the component that you want to launch.
This should work:
APP1
Intent i2 = new Intent();
i2.setComponent(new ComponentName(PACKAGE,ACTIVITY));//the destination packageName
i2.putExtra("Id", "100");
startActivity(i2);
APP2
String myString = getIntent().getStringExtra("Id");
Using Bundle.putSerializable(Key,Object); and Bundle.putParcelable(Key, Object);
the former object must implement Serializable, and the latter object must implement Parcelable.
Content providers:
Content providers are the standard interface that connects data in one process with code running in another process.
See Android Docs.
Content provider working demo here.

how do i handle this string to open my app

i get this string in a browser redirect
intent://view?id=123#Intent;package=com.myapp;scheme=myapp;launchFlags=268435456;end;
how do i work with it ?
found in : http://fokkezb.nl/2013/09/20/url-schemes-for-ios-and-android-2/
You have your answer in the part 1 of the same article :
http://fokkezb.nl/2013/08/26/url-schemes-for-ios-and-android-1/
Your activity must have an intent filter matching the given intent
Here you have :
package=com.myapp;scheme=myapp
Your app package must be com.myapp and the url scheme is myapp://
So you must declare your activity like that :
<activity android:name=".MyActivity" >
<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>
</activity>
Then your activity will be automatically opened by android.
Optionnaly you can work with the uri received from your code, for example in the onResume method (why onResume ? -> because it is always called after onNewIntent) :
#Override
protected void onResume() {
super.onResume();
Intent intent = getIntent();
if (intent != null && intent.getData() != null) {
Uri uri = intent.getData();
// do whatever you want with the uri given
}
}
If your activity uses onNewIntent, i recommend to use setIntent so that code above is always executed on last intent :
#Override
protected void onNewIntent(Intent intent) {
setIntent(intent);
}
Does this answers your question ?

Categories

Resources