Asked  6 Months ago    Answers:  5   Viewed   22 times

Say I want to define that an URI such as:

myapp://path/to/what/i/want?d=This%20is%20a%20test

must be handled by my own application, or service. Notice that the scheme is "myapp" and not "http", or "ftp". That is precisely what I intend: to define my own URI schema globally for the Android OS. Is this possible?

This is somewhat analogous to what some programs already do on, e.g., Windows systems, such as Skype (skype://) or any torrent downloader program (torrent://).

 Answers

72

This is very possible; you define the URI scheme in your AndroidManifest.xml, using the <data> element. You setup an intent filter with the <data> element filled out, and you'll be able to create your own scheme. (More on intent filters and intent resolution here.)

Here's a short example:

<activity android:name=".MyUriActivity">
    <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" android:host="path" />
    </intent-filter>
</activity>

As per how implicit intents work, you need to define at least one action and one category as well; here I picked VIEW as the action (though it could be anything), and made sure to add the DEFAULT category (as this is required for all implicit intents). Also notice how I added the category BROWSABLE - this is not necessary, but it will allow your URIs to be openable from the browser (a nifty feature).

Tuesday, June 1, 2021
 
SJain
answered 6 Months ago
40

Picasso uses the HTTP client for disk caching and if one is already configured it will use that instead of installing its own.

For the built-in UrlConnection the docs for installing a cache are here: https://developer.android.com/reference/android/net/http/HttpResponseCache.html

If you are using OkHttp then you just call setCache: http://square.github.io/okhttp/2.x/okhttp/com/squareup/okhttp/OkHttpClient.html#setCache-com.squareup.okhttp.Cache-

Wednesday, June 9, 2021
 
talkhabi
answered 6 Months ago
37

Don't create your IntentFilter inline, then use the addAction method to add the UNREGISTERED action, i.e.:

IntentFilter filter = new IntentFilter(SIPEngine.SIP_REGISTERED_INTENT);
filter.addAction(SIPEngine.SIP_UNREGISTERED_INTENT);
context.registerReceiver(sipRegistrationListener, filter);
Wednesday, August 11, 2021
 
user3929962
answered 4 Months ago
67

Instead of data android:mimeType="application/customtype", you should use data android:mimeType="*/*". That will catch all mime types that the OS on your device supports. You simply cannot define custom types which the OS doesn't know about.

Saturday, November 13, 2021
 
user680111
answered 2 Weeks ago
84

After a full day scouring the internet, I finally found a solution. To get a "Nested Master Details Flow" effect simply use a ViewPager with FragmentPageAdapter. The Master Detail Flow will look like this:

enter image description here

To change to a two panel mode when the user switches to landscape, in your extended FragmentPagerAdapter class, override the following method:

@Override
public float getPageWidth(int position) {
    DisplayMetrics metrics = getResources().getDisplayMetrics();
    // if the width is greater than 900dp halve the width of the page
    if ((metrics.widthPixels / metrics.density) > 900) {
        return (0.5f);
    }
    return super.getPageWidth(position);
}

To provide an "up button" for the view pager:

viewpager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
    // This method will be invoked when a new page becomes selected.
    @Override
    public void onPageSelected(int position) {
        if (position == 0) {
            getSupportActionBar().setDisplayHomeAsUpEnabled(false);
        } else {
            getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        }

    }
});

You can tell the "up button" to go back a page like this (where viewpager is a member variable of your activity, holding the a reference to your ViewPager):

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    int position = viewpager.getCurrentItem();
    if (position > 0) viewpager.setCurrentItem(position-1);
    return true;
}

REFERENCES:

ViewPager with FragmentPagerAdapter

Display back button on action bar

Multiple-View ViewPager Options

How to implement a ViewPager with different Fragments / Layouts and example github project

Friday, November 19, 2021
 
Evan
answered 1 Week ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :
 
Share