I want to know if it is possible to read the value of android:scheme from the Android-manifest-file (example below) programmatically.
<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="zxtest" />
</intent-filter>
You should be able to get this information from the PackageManager:
PackageManager pm = getPackageManager();
ActivityInfo activityInfo = pm.getActivityInfo(component,
PackageManager.GET_INTENT_FILTERS);
Unfortunately, however, you can't :-( This call SHOULD return the intent filters (according to the documentation), but it seems that the Android developers haven't gotten around to actually implementing it yet :-(
See Unable to get intent filters from a package
I'm assuming you mean that the intent has been called and you're trying to find the scheme from your activity's code?
In your activity's onCreate method, you can do the following:
Intent intent = getIntent();
Uri data = intent.getData();
String scheme = data.getScheme(); // "zxtest"
Related
I want to associate files of particular extension (say, any file with the extension .xyz) with my app. This means, when user taps SaveFile1.xyz in File explorer (or other places, like gmail, drive etc.), my app should launch and the Uri of the file will be passed in an intent to my activity.
While this is the expected behavior, I don't know how to associate custom files. I have read and understood how intent matching works and the possible values in data tag, but it didn't work. I have checked some stackoverflow answers (Ex, this and this), but they use file scheme and pathPatterns.
We cannot use the pathPattern because we get a content uri (Uri with scheme=content), which is the abstracted path and will not have any extensions.
The mime type setting can be used only for the predefined values (like image/png can associate all png images with the app).
I could set the mime type as "*/*", but I don't want my app to accept every file (and then reject it in onCreate or something).
Has anyone solved this problem? Any help will be greatly appreciated.
I have the following in the activity tag of the manifest file.
<activity
android:name=".MainActivity"
android:exported="true">
<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:mimeType="application/pdf"/>
<data android:mimeType="image/png"/>-->
<!--<data android:scheme="content" android:mimeType="*/*"/>-->
</intent-filter>
</activity>
Edit1:
I set the mime type as "*/*" and added the following line in Activity.onCreate to see the type in the intent (and set it in the manifest file) ...
Intent dataIntent = getIntent();
Log.i(TAG, "dataIntent.getType() = " + dataIntent.getType());
.... but got the following as output.
dataIntent.getType() =
Its not even null.
I'm trying to make the gallery app share images to my app, the problem is that I don't know how to get the image data sent to me by the gallery. I assumed that I may found the data in the method .getData() but it returns null
this is my intent-filter
<intent-filter>
<action android:name="android.intent.action.SEND"/>
<action android:name="android.intent.action.VIEW"/>
<data android:mimeType="image/*"/>
<category android:name="android.intent.category.default"/>
</intent-filter>
and here is my code in MainActivity
Intent data = getIntent(); //get the intent that starts this activity, in this case: gallery intent
if(data != null){
Uri uri = data.getData();
//set the image view: the problem is: uri is always null
imageView.setImageURI(uri);
}
if the method getData() is not what I want, then how can I get the image that meant to be shared with my app?
So, I think my answer is a little bit late, but anyhow, here it is:
You have already set the intent-filter in the AndroidManifest.xml correct like this:
<intent-filter>
<action android:name="android.intent.action.SEND" />
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="image/*" />
</intent-filter>
As on this Website stated, you get the URI with this command:
Uri imageUri = (Uri) intent.getParcelableExtra(Intent.EXTRA_STREAM);
//set content of ImageView to the specific Uri
imageView.setImageURI(imageUri);
Dokumentation about the Intent class can be found here.
I noticed that there are two ways to start an Activity with Intent.
1) Using - [android:name=".MainActivity"]
Intent intent = new Intent(context, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent);
2) Using - [action android:name="com.example.MainActivity"] from the Intent_Filter
String actionName = "com.example.MainActivity";
Intent intent = new Intent();
intent.setAction(CUSTOM_ACTION);
context.startActivity(i);
So what's the difference? Why do we have to set both the name and the intent_filter in the manifest if they both do the same thing?
===---==
Second confusion I have is... Is there a way to use the "OK Google" voice-launch option to launch an Activity that's NOT shown in the app list? Basically actual App Launcher launches the "default homepage" of the app, whereas voice launch takes you to a specific Activity directly?
I suspect some combo of these might get it done:
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
<category android:name="android.intent.category.HOME" />
<category android:name="android.intent.category.DEFAULT" />
If your class is in your main package, you can use this way:
.YourClass
or
com.example.YourClass
If your class isn't in your main package, you should use this way:
packagename.YourClass
I am new in android development, I want to build a music player but the problem in front of me is how do i make my app open from gallery.
For example,
if we want to open any music file then we select it, the the android mobile ask which player do we want to use.
so how can I add my app in that option.
please help.
If you want your app to be in the list for opening an audio file, you need to tell the system your app can open those files. You can do that by adding something like the following to your activity tag in your AndroidManifest.xml:
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<data android:scheme="content"/>
<data android:scheme="file"/>
<data android:mimeType="audio/*"/>
<data android:mimeType="application/ogg"/>
<data android:mimeType="application/x-ogg"/>
<data android:mimeType="application/itunes"/>
</intent-filter>
Intent filters are part of the core principles in Android development. You should try to get some knowledge on these basic topics before getting started.
It helps you
Intent intent = new Intent();
intent.setAction(android.content.Intent.ACTION_VIEW);
File file = new File(YOUR_SONG_URI);
intent.setDataAndType(Uri.fromFile(file), "audio/*");
startActivity(intent);
Actually you can open activity not app at self just simply add action intent filter in mainfest like this
<activity class=".foo" android:label="#string/title_notes_list">
<intent-filter>
<action android:name="com.me.love" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
other app code :
Intent intent = new Intent("com.me.love");
startActivity(intent);
however if you want to tell android system that your activity can handle some actions like share or send data you just have to add "send" action to your activity in mainfest like so :
<activity class=".boo" android:label="#string/title_notes_list">
<intent-filter>
<action android:name="android.intent.action.SEND" />
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="text/plain" />
</intent-filter>
</activity>
so now when ever user click share button android system will check all activitys that have action string "android.intent.action.SEND" then give the user list of activitys that have send action and if you did added same action as boo activity dose to your activity then it will be one of chooses in the list.
My program has a list of tabs that each contain a list of people. Clicking a person should edit their details, but instead, clicking a person throws an ActivityNotFoundException. My code that starts the edit activity:
Intent intent = new Intent("my.package.EDIT"); // Person is both
intent.putExtra("my.package.PERSON", (Parcelable) myPerson); // Parcelable and
GroupArrayAdapter.this.getContext().startActivity(intent); // Serializable
Relevant part of AndroidManifest.xml:
<activity android:name="EditActivity">
<intent-filter>
<action android:name="my.package.EDIT" />
</intent-filter>
</activity>
What's going on?
There's two ways to start an Activity, either explicitly or implicitly. Explicitly means that you use the class name directly; usually if you're calling an Activity that is within your own application, what you mean to use is the explicit method because it's far easier:
Intent intent = new Intent(context, EditActivity.class);
context.startActivity(intent);
However, if you really do mean to use implicit filtering, then your intent filter is missing something. Every implicit intent filter requires the category android.intent.category.DEFAULT (with few exceptions). Try this and see if it works:
<activity android:name="EditActivity">
<intent-filter>
<category android:name="android.intent.category.DEFAULT" />
<action android:name="my.package.EDIT" />
</intent-filter>
</activity>
Suggested reading: intent filters.
When you instantiate your Adapter, pass the context from the calling activity. Then in your adapter use context.startActivity(...);