无法从我的 WebView 在 Whatsapp 上分享。无法加载位于 whatsapp://send?text= 的网页,因为 net::ERR_UNKNOWN_URL_SCHEME
Posted
技术标签:
【中文标题】无法从我的 WebView 在 Whatsapp 上分享。无法加载位于 whatsapp://send?text= 的网页,因为 net::ERR_UNKNOWN_URL_SCHEME【英文标题】:Unable to share on Whatsapp from my WebView . Getting The webpage at whatsapp://send?text= could not be load because net::ERR_UNKNOWN_URL_SCHEME 【发布时间】:2019-01-25 04:33:11 【问题描述】:使用我的 Webview 应用在 whatsapp 上分享新闻时出错
**包 com.agraleaks.imgauravanimator;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.ProgressDialog;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.Uri;
import android.support.v4.app.NotificationCompat;
import android.support.v4.content.LocalBroadcastManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.view.View;
import android.graphics.Bitmap;
import android.os.Handler;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v4.widget.SwipeRefreshLayout.OnRefreshListener;
import com.agraleaks.imgauravanimator.Common.Config;
public class MainActivity extends AppCompatActivity implements OnRefreshListener
private WebView webView;
private ProgressDialog dialog;
private BroadcastReceiver mRegistrationBroadcastReceiver;
private SwipeRefreshLayout swipeLayout;
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
swipeLayout = (SwipeRefreshLayout) findViewById(R.id.swipe_container);
swipeLayout.setOnRefreshListener(this);
swipeLayout.setColorScheme(android.R.color.holo_blue_bright,
android.R.color.holo_green_light,
android.R.color.holo_orange_light,
android.R.color.holo_red_light);
//WebView
webView = (WebView)findViewById(R.id.webView);
webView.getSettings().setjavascriptEnabled(true);
webView.setWebChromeClient(new WebChromeClient());
webView.setWebViewClient(new WebViewClient()
@Override
public void onPageFinished(WebView view, String url)
//hide loading image
findViewById(R.id.imageView1).setVisibility(View.GONE);
findViewById(R.id.progressBar1).setVisibility(View.GONE);
//show webview
findViewById(R.id.webView).setVisibility(View.VISIBLE);
);
webView.loadUrl("http://www.agraleaks.com/");
webView.setHorizontalScrollBarEnabled(false);
webView.setScrollBarStyle(View.SCROLLBARS_OUTSIDE_OVERLAY);
webView.setBackgroundColor(128);
mRegistrationBroadcastReceiver = new BroadcastReceiver()
@Override
public void onReceive(Context context, Intent intent)
if (intent.getAction().equals(Config.STR_PUSH))
String message = intent.getStringExtra(Config.STR_PUSH);
showNotification ("Agra Leaks- Fastest News Channel in the City",message);
;
onNewIntent(getIntent());
@Override
public void onPageStarted(WebView view, String url, Bitmap favicon)
// TODO Auto-generated method stub
super.onPageStarted(view, url, favicon);
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url)
// TODO Auto-generated method stub
if(url != null && url.startsWith("whatsapp://"))
Intent sendIntent = new Intent();
sendIntent.setAction(Intent.ACTION_SEND);
sendIntent.putExtra(Intent.EXTRA_TEXT, "Agra Leaks - Digital Newspaper.");
sendIntent.setType("text/plain");
sendIntent.setPackage("com.whatsapp");
startActivity(sendIntent);
return true;
else
return false;
private void showNotification(String title, String message)
Intent intent = new Intent(getBaseContext(),MainActivity.class);
intent.putExtra(Config.STR_KEY,message);
intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent contentIntent = PendingIntent.getActivity(getBaseContext(),0,intent,PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder builder = new NotificationCompat.Builder(getBaseContext());
builder.setAutoCancel(true)
.setWhen(System.currentTimeMillis())
.setDefaults(Notification.DEFAULT_ALL)
.setSmallIcon(R.mipmap.ic_launcher_round)
.setContentTitle(title)
.setContentText(message)
.setContentIntent(contentIntent);
NotificationManager notificationManager = (NotificationManager)getBaseContext().getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(1,builder.build());
@Override
public boolean onKeyDown(int keyCode, KeyEvent event)
if (event.getAction()== KeyEvent.ACTION_DOWN)
switch (keyCode)
case KeyEvent.KEYCODE_BACK:
if (webView.canGoBack())
webView.goBack();
else
finish();
return true;
return super.onKeyDown(keyCode, event);
@Override
protected void onPause()
LocalBroadcastManager.getInstance(this).unregisterReceiver(mRegistrationBroadcastReceiver);
super.onPause();
@Override
protected void onResume()
super.onResume();
LocalBroadcastManager.getInstance(this).registerReceiver(mRegistrationBroadcastReceiver,new IntentFilter("registrationComplete"));
LocalBroadcastManager.getInstance(this).registerReceiver(mRegistrationBroadcastReceiver,new IntentFilter(Config.STR_PUSH));
@Override
public void onRefresh()
new Handler().postDelayed(new Runnable()
@Override public void run()
// webView.reload();
swipeLayout.setRefreshing(false);
webView.loadUrl( "javascript:window.location.reload( true )" );
, 5000);
**
这些是我的代码 无法从我的 WebView 在 Whatsapp 上分享。出现错误 - 无法加载位于 whatsapp://send?text= 的网页,因为 net::ERR_UNKNOWN_URL_SCHEME。
这是我的activity_main
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_
android:layout_
tools:context="com.agraleaks.imgauravanimator.MainActivity">
<android.support.v4.widget.SwipeRefreshLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/swipe_container"
android:layout_
android:layout_>
<WebView
android:id="@+id/webView"
android:layout_
android:layout_ />
</android.support.v4.widget.SwipeRefreshLayout>
<ImageView
android:id="@+id/imageView1"
android:layout_
android:layout_
android:src="@drawable/splash" />
<ProgressBar
android:id="@+id/progressBar1"
android:layout_
android:layout_
android:layout_below="@+id/imageView1"
android:layout_centerHorizontal="true" />
</RelativeLayout>
【问题讨论】:
任何人都可以帮助... 【参考方案1】:我自己找到了答案,
我只需要输入这些代码
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url)
if(url != null && url.startsWith("whatsapp://"))
Intent sendIntent = new Intent();
sendIntent.setAction(Intent.ACTION_SEND);
sendIntent.putExtra(Intent.EXTRA_TEXT, "Moon TV");
sendIntent.setType("text/plain");
sendIntent.setPackage("com.whatsapp");
startActivity(sendIntent);return true;
else
return false;
【讨论】:
以上是关于无法从我的 WebView 在 Whatsapp 上分享。无法加载位于 whatsapp://send?text= 的网页,因为 net::ERR_UNKNOWN_URL_SCHEME的主要内容,如果未能解决你的问题,请参考以下文章
如何从我的应用程序发送应用程序邀请(通过whatsapp、hike、消息)
如何从我的 Android 应用程序将短信转发给选定的联系人到 whatsapp?