Creating a URL safehouse in Android

Creating a URL safehouse in Android

Problem:

I was trying to open some links to the LinkedIn platform on some websites but the LinkedIn app kept crashing.

I tried repeatedly to no avail.

I thought of saving the links somewhere so I could get them later.

When I hit the Share button in my browser, I realized I don't have an app I can share links to and retrieve them later.

Yes, I have an app I can share links to, that app is a reader app, but that app has not been reliable.

You can't be sure a URL you share with the app will be saved.

To make sure URLs are saved in the app, I have to open the app and wait for the URL to be downloaded by the app.

When you are trying to get some tasks done and you need to share lots of URLs with the app, using the app will be a pain in the ass.

I needed something reliable.

Since I figured I could create an app that does just what I needed and it won't take too much of my time, I ventured into creating it.

Solution

The following are the steps you can take to create such an app on the Android platform:

Step 1: Register your app

Typically, the question on your mind is how to create an app you can share URLs to and it will be saved in the app and when you open the app, you should see the URL.

Now, if you copy and paste the question to Google or ChatGPT, you might not get the response that will give you what you want.

So what you can do is ask a part of the question: How can I get my Android app to appear when the Share button is tapped on a website? How can my Android app receive what is shared?

With the above two questions answered, you already have what you need to create the app.

As an experienced Android developer, the rest are routine - persisting the shared URL; displaying the saved URL to the user.

How can you get your Android app to appear when the Share button is tapped on a website?

Well, what you should do is register your app as an app that can handle the action.SEND intent.

The implementation of the Share button is such that when the button is tapped, an action.SEND intent is fired and a list of apps that are registered as apps that can handle the intent appears.

Any app that has been registered with the Android OS to receive the action.SEND intent would then appear.

The user will then select the app and hope that the app has received the shared text.

How do you register your app as an app that can handle the action.SEND intent?

Let's assume you want to handle the shared text in the MainActivity class, you will add the below inside the activity tag for the MainActivity class in the manifest file:

           <intent-filter>
                <action android:name="android.intent.action.SEND" />
                <category android:name="android.intent.category.DEFAULT" />
                <data android:mimeType="text/plain" />
            </intent-filter>

The first code in the intent-filter tag registers the app, the second code specifies that this activity should be the activity to handle the intent, and the third code specifies the kind of data the intent is associated with. Since text/plain is specified, we don't expect the app to appear when the user in another app is trying to share an image or a video or a PDF file.

With the above code, you are done registering the app.

NB: This step assumes you've created a project on Android Studio. if you've not, create one and open the AndroidManifest.xml file and paste the code where it should be pasted.

Step 2: Implement receiving the shared URL

The next question to be tackled is: How can my Android app receive what is shared?

At this point, the app will appear when the user tries to share a URL in another app.

It won't be the only app that will appear by the way.

When it appears and the user selects it, the user will assume the URL has been shared.

But what you should know is that unless you write code to receive the URL and do something with it, the shared URL won't appear in your app.

What code can be used to receive the shared URL?

The below code

Intent intent = getIntent();
String action = intent.getAction();
String type = intent.getType();

if (Intent.ACTION_SEND.equals(action) && type != null && "text/plain".equals(type)) {
            // Get the shared URL
            String sharedUrl = intent.getStringExtra(Intent.EXTRA_TEXT);

}

The first line of code gets the intent from the Android OS. It uses a method getIntent.

The second and third line gets data that you will use to ensure that you are getting the data from the action.SEND intent and not some other action and also that the data you are getting is of type plain text and not of type jpg or mp4.

The if statement makes the necessary check and then the getStringExtra method is used to get the shared URL and save it in a variable sharedUrl.

With just that code, you now have the shared URL in your app.

Todo: Add the code inside the onCreate method of the MainActivity class.

Now what do you do with the Shared URL? Well, since you want the user to see the URL any time the app is opened, what you should do is persist the shared URL.

In Android, you have two easy-to-implement choices:

  1. Persisting the URL in shared preferences

  2. Persisting the URL in an SQLite database

For testing purposes, I persisted the URL in shared preference and configured the app to display it in a toast when the app is launched.

But for the final app, I persisted the URL in an SQLite database.

You can check out the project on my Github

You can as well check out the app on the Play Store

In the app, I displayed the URLs in a RecyclerView, implemented swipe to delete functionality, click to open URL functionality, and hold to copy URL functionality.

I also enabled the user to see externally copied URLs in the app (as long as the user opens the app as soon as copying the URL).

Finally, I integrated Firebase Crashlytics to monitor crashes.

Someone reported a crash when he tried to open a URL and I couldn't figure out why the crash happened.

A few days later, I experienced the same crash and Crashlytics revealed it was a problem with the RecyclerView. I've fixed it.

Hopefully.

Conclusion

This article walked you through how to implement URL sharing in an Android app.

I hope you found it insightful and helpful.

If you think something is missing, have questions, or would like to offer any thoughts or suggestions, go ahead and leave a comment below.

I’d appreciate the feedback!