所以我按照这篇文章的指示,我仍然无法让我的应用程序打开应用程序内的链接。
我做了很多研究。请记住,我对开发 android 非常陌生。谁能帮我?
如果有人单击链接(例如:styleonem.co/about)以转到该页面中的该 URL,我想做的是打开一个意图。
现在发生的事情是它打开了应用程序(很好!)但它永远不会在 Webview 中加载 url。我以为我已经把这一切都整理好了,但我已经连续数周搜索并尝试学习这个新的应用程序界面,但我仍然无法弄清楚。
下面是我的代码
AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="co.styleonem.styleclothingco">
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity"
android:configChanges="orientation|screenSize"
android:launchMode="singleInstance"
android:alwaysRetainTaskState="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" />
<category android:name="android.intent.category.BROWSABLE" />
<data android:scheme="https" android:host="styleonem.co"/>
<data android:scheme="http" android:host="www.styleonem.co"/>
</intent-filter>
</activity>
</application>
</manifest>
MainActivity.java:
package co.styleonem.styleclothingco;
import android.app.Application;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.webkit.CookieManager;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import com.onesignal.OSNotification;
import com.onesignal.OSNotificationOpenResult;
import com.onesignal.OneSignal;
import org.json.JSONObject;
import javax.sql.RowSet;
import static android.R.attr.data;
public class MainActivity extends AppCompatActivity {
private WebView mywebView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mywebView = (WebView) findViewById(R.id.webview);
WebSettings webSettings= mywebView.getSettings();
webSettings.setJavaScriptEnabled(true);
// Line of Code for opening links in app
mywebView.setWebViewClient(new WebViewClient());
mywebView.setWebViewClient(new HelloWebViewClient());
CookieManager.getInstance().setAcceptCookie(true);
final WebView webview = (WebView)findViewById(R.id.webview);
webview.getSettings().setUserAgentString("style-co-app");
mywebView.loadUrl("https://styleonem.co/");
OneSignal.startInit(this)
.inFocusDisplaying(OneSignal.OSInFocusDisplayOption.Notification)
.unsubscribeWhenNotificationsAreDisabled(true)
.init();
OneSignal.startInit(this).setNotificationOpenedHandler(new OneSignal.NotificationOpenedHandler() {
@Override
public void notificationOpened(OSNotificationOpenResult result) {
String launchURL = result.notification.payload.launchURL;
if (launchURL != null) {
Log.d(Const.DEBUG, "Launch URL: " + launchURL);
Intent intent = new Intent(getApplicationContext(), MainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT | Intent.FLAG_ACTIVITY_NEW_TASK);
intent.putExtra("url", launchURL);
startActivity(intent);
} else {
Log.d(Const.DEBUG, "Launch URL not found");
Intent intent = new Intent(getApplicationContext(), MainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT | Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
}
}
}).init();
}
private class HelloWebViewClient extends WebViewClient {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (Uri.parse(url).getHost().equals("styleonem.co")) {
// This is my web site, so do not override; let my WebView load the page
return false;
}
// Otherwise, the link is not for a page on my site, so launch another Activity that handles URLs
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivity(intent);
Uri data = getIntent().getData();
String extUrl = toString();
if (data != null) {
mywebView.loadUrl(toString());
} else {
mywebView.loadUrl(url);
}
return true;
}
}
//Code For Back Button
@Override
public void onBackPressed() {
if(mywebView.canGoBack())
{
mywebView.goBack();
}
else
{
super.onBackPressed();
}
}
}