I am writing an android app that simply handles android app links. I am on Android 11. Basically when the user clicks on a weblink I want to receive that link in my app so that I can perform some action on the received URL. I've tried following this guide but it didn't help me at all: https://developer.android.com/training/app-links
Is there a thorough guide on how to achieve this? I am going blind here but from what I understand I basically need mainly three things:
For the simplicity of the task, let's just say I want to print the URL to console for now. This is what I've added in the Manifest:
<activity android:label="test" android:name="com.developer.app.main.BrowserActivity"
android:exported="true">
<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="http" android:host="api.myserver.com" android:pathPrefix="/discord/"/>
</intent-filter>
<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="https" android:host="maps.google.com" android:pathPrefix="/maps"/>
</intent-filter>
<intent-filter>
<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>
</activity>
The layout of my activity:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:aapt="http://schemas.android.com/aapt" android:orientation="vertical" android:id="@+id/layout" android:layout_width="match_parent" android:layout_height="match_parent">
<androidx.swiperefreshlayout.widget.SwipeRefreshLayout android:id="@+id/swipeContainer" android:layout_width="match_parent" android:layout_height="match_parent">
<LinearLayout android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent">
<TextView android:textAppearance="@style/TextAppearance.AppCompat.Display1" android:gravity="center" android:id="@+id/txtLoading" android:visibility="visible" android:layout_width="match_parent" android:layout_height="match_parent" android:text="loading" android:textAlignment="center"/>
<com.developer.app.main.WebView android:id="@+id/webView" android:visibility="gone" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1"/>
</LinearLayout>
</androidx.swiperefreshlayout.widget.SwipeRefreshLayout>
</LinearLayout>
BrowserActivity:
public abstract class BrowserActivity extends AppCompatActivity {
public static final String TAG = BrowserActivity.class.getSimpleName();
SwipeRefreshLayout swipeContainer;
TextView txtLoading;
WebView webView;
public BrowserActivity getActivity() {
return this;
}
public void onCreate(Bundle bundle) {
super.onCreate(bundle);
setContentView(R.layout.activity_browser);
//ButterKnife.bind((Activity) this);
this.swipeContainer = findViewById(R.id.swipeContainer);
this.txtLoading = findViewById(R.id.txtLoading);
this.webView = findViewById(R.id.webView);
this.txtLoading.setVisibility(View.VISIBLE);
this.webView.setVisibility(View.GONE);
setupActionBar();
handleIntent(getIntent());
}
private void setupWebView(String str)
{
this.webView.getSettings().setJavaScriptEnabled(true);
this.webView.getSettings().setDomStorageEnabled(true);
this.txtLoading.setVisibility(View.GONE);
this.webView.setVisibility(View.VISIBLE);
this.webView.loadUrl(str);
}
public void handleIntent(Intent intent) {
String action = intent.getAction();
if ("android.intent.action.VIEW".equals(action)) {
setupWebView(intent.getDataString());
} else if (!"android.intent.action.SEND".equals(action)) {
String str = TAG;
Log.w(str, "Received unknown action: " + action);
} else if (intent.getClipData() == null || intent.getClipData().getItemCount() <= 0) {
finish();
} else {
handleData(intent.getClipData().getItemAt(0).getText().toString());
}
}
private void handleData(String str)
{
System.out.println(str);
}
}
However when I click on link, (the disambiguation dialog does not appear anymore since I've set my app as default handler), the app open and crash immediately. What am I doing wrong?
Edit with the exception:
FATAL EXCEPTION: main
Process: com.developer.app, PID: 11726
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.developer.app/com.developer.app.main.BrowserActivity}: java.lang.InstantiationException: java.lang.Class<com.developer.app.main.BrowserActivity> cannot be instantiated
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3742)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:4027)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:85)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2336)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:247)
at android.app.ActivityThread.main(ActivityThread.java:8676)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:602)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1130)
Caused by: java.lang.InstantiationException: java.lang.Class<com.developer.app.main.BrowserActivity> cannot be instantiated
at java.lang.Class.newInstance(Native Method)
at android.app.AppComponentFactory.instantiateActivity(AppComponentFactory.java:95)
at androidx.core.app.CoreComponentFactory.instantiateActivity(CoreComponentFactory.java:45)
at android.app.Instrumentation.newActivity(Instrumentation.java:1253)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3730)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:4027)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:85)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2336)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:247)
at android.app.ActivityThread.main(ActivityThread.java:8676)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:602)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1130)
Replace:
public abstract class BrowserActivity extends AppCompatActivity
with:
public class BrowserActivity extends AppCompatActivity
You are trying to actually use this class, which means it cannot be abstract
.