Skip to content

Commit

Permalink
可以被作为 第三方浏览器打开
Browse files Browse the repository at this point in the history
  • Loading branch information
youlookwhat committed Dec 14, 2018
1 parent 86d1b87 commit 752cdc1
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 42 deletions.
3 changes: 0 additions & 3 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,6 @@ dependencies {
})
implementation 'com.android.support:appcompat-v7:27.1.1'
testImplementation 'junit:junit:4.12'
//增加这二句
api 'com.jakewharton:butterknife:8.8.1'
annotationProcessor "com.jakewharton:butterknife-compiler:8.8.1"
debugImplementation 'com.squareup.leakcanary:leakcanary-android:1.6.1'
releaseImplementation 'com.squareup.leakcanary:leakcanary-android-no-op:1.6.1'
}
19 changes: 17 additions & 2 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="com.example.jingbin.webviewstudy">

<!-- 网络 -->
Expand All @@ -16,7 +17,9 @@
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<activity
android:name=".MainActivity"
android:launchMode="singleTask">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
Expand All @@ -26,8 +29,20 @@
android:name=".WebViewActivity"
android:configChanges="orientation|screenSize"
android:hardwareAccelerated="true"
android:launchMode="singleTask"
android:screenOrientation="portrait"
android:theme="@style/WebViewTheme" />
android:theme="@style/WebViewTheme">

<intent-filter tools:ignore="AppLinkUrlError">
<action android:name="android.intent.action.VIEW" />

<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<!--使用http,则只能打开http开头的网页-->
<data android:scheme="https" />
</intent-filter>

</activity>

<!--用于DeepLink,html跳到此页面 scheme_Adr: 'will://link/testid',-->
<activity android:name=".DeepLinkActivity">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,52 +19,35 @@

import com.example.jingbin.webviewstudy.utils.StatusBarUtil;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
* Link to: https://github.com/youlookwhat/WebViewStudy
* contact me: http://www.jianshu.com/users/e43c6e979831/latest_articles
* contact me: https://www.jianshu.com/u/e43c6e979831
*/
public class MainActivity extends AppCompatActivity implements View.OnClickListener {

@BindView(R.id.bt_baidu)
TextView btBaidu;
@BindView(R.id.bt_call)
TextView btCall;
@BindView(R.id.bt_upload_photo)
TextView btUploadPhoto;
@BindView(R.id.bt_movie)
TextView btMovie;
@BindView(R.id.activity_main)
LinearLayout activityMain;
@BindView(R.id.bt_deeplink)
TextView btDeepLink;
@BindView(R.id.et_search)
AppCompatEditText etSearch;
@BindView(R.id.bt_openUrl)
AppCompatButton btOpenUrl;
@BindView(R.id.bt_java_js)
TextView btJavaJs;
@BindView(R.id.tv_version)
TextView tvVersion;

private AppCompatEditText etSearch;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ButterKnife.bind(this);

StatusBarUtil.setColor(this, ContextCompat.getColor(this, R.color.colorPrimary), 0);
initView();
}

private void initView() {
findViewById(R.id.bt_deeplink).setOnClickListener(this);
findViewById(R.id.bt_openUrl).setOnClickListener(this);
findViewById(R.id.bt_baidu).setOnClickListener(this);
findViewById(R.id.bt_movie).setOnClickListener(this);
findViewById(R.id.bt_upload_photo).setOnClickListener(this);
findViewById(R.id.bt_call).setOnClickListener(this);
findViewById(R.id.bt_java_js).setOnClickListener(this);

etSearch = findViewById(R.id.et_search);
TextView tvVersion = findViewById(R.id.tv_version);
tvVersion.setText(String.format("版本:v%s", BuildConfig.VERSION_NAME));
btBaidu.setOnClickListener(this);
btCall.setOnClickListener(this);
btUploadPhoto.setOnClickListener(this);
btMovie.setOnClickListener(this);
btJavaJs.setOnClickListener(this);
btDeepLink.setOnClickListener(this);
btOpenUrl.setOnClickListener(this);
tvVersion.setOnClickListener(this);
/** 处理键盘搜索键 */
etSearch.setOnEditorActionListener(new TextView.OnEditorActionListener() {
Expand Down Expand Up @@ -177,4 +160,6 @@ public boolean onOptionsItemSelected(MenuItem item) {
}
return super.onOptionsItemSelected(item);
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.util.DebugUtils;
import android.util.Log;
import android.view.KeyEvent;
Expand All @@ -34,9 +36,11 @@
import com.example.jingbin.webviewstudy.utils.BaseTools;
import com.example.jingbin.webviewstudy.utils.StatusBarUtil;

import java.util.List;

/**
* 网页可以处理:
* 点击相应控件:
* 点击相应控件
* - 拨打电话、发送短信、发送邮件
* - 上传图片(版本兼容)
* - 全屏播放网络视频
Expand All @@ -45,6 +49,7 @@
* JS交互部分:
* - 前端代码嵌入js(缺乏灵活性)
* - 网页自带js跳转
* 被作为第三方浏览器打开
*/
public class WebViewActivity extends AppCompatActivity implements IWebPageView {

Expand All @@ -70,13 +75,15 @@ protected void onCreate(Bundle savedInstanceState) {
initTitle();
initWebView();
webView.loadUrl(mUrl);
getDataFromBrowser(getIntent());
}

private void getIntentData() {
mUrl = getIntent().getStringExtra("mUrl");
mTitle = getIntent().getStringExtra("mTitle");
}


private void initTitle() {
StatusBarUtil.setColor(this, ContextCompat.getColor(this, R.color.colorPrimary), 0);
mProgressBar = findViewById(R.id.pb_progress);
Expand Down Expand Up @@ -125,8 +132,10 @@ public boolean onOptionsItemSelected(MenuItem item) {
BaseTools.share(WebViewActivity.this, shareText);
break;
case R.id.actionbar_cope:// 复制链接
BaseTools.copy(webView.getUrl());
Toast.makeText(this, "复制成功", Toast.LENGTH_LONG).show();
if (!TextUtils.isEmpty(webView.getUrl())) {
BaseTools.copy(webView.getUrl());
Toast.makeText(this, "复制成功", Toast.LENGTH_LONG).show();
}
break;
case R.id.actionbar_open:// 打开链接
BaseTools.openLink(WebViewActivity.this, webView.getUrl());
Expand Down Expand Up @@ -327,6 +336,42 @@ protected void onActivityResult(int requestCode, int resultCode, Intent intent)
}
}


/**
* 使用singleTask启动模式的Activity在系统中只会存在一个实例。
* 如果这个实例已经存在,intent就会通过onNewIntent传递到这个Activity。
* 否则新的Activity实例被创建。
*/
@Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
getDataFromBrowser(intent);
}

/**
* 作为三方浏览器打开
* Scheme: https
* host: www.jianshu.com
* path: /p/1cbaf784c29c
* url = scheme + "://" + host + path;
*/
private void getDataFromBrowser(Intent intent) {
Uri data = intent.getData();
if (data != null) {
try {
String scheme = data.getScheme();
String host = data.getHost();
String path = data.getPath();
String text = "Scheme: " + scheme + "\n" + "host: " + host + "\n" + "path: " + path;
Log.e("data", text);
String url = scheme + "://" + host + path;
webView.loadUrl(url);
} catch (Exception e) {
e.printStackTrace();
}
}
}

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
Expand Down
1 change: 0 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ buildscript {
}
dependencies {
classpath 'com.android.tools.build:gradle:3.0.1'
classpath 'com.jakewharton:butterknife-gradle-plugin:8.8.1'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
Expand Down

0 comments on commit 752cdc1

Please sign in to comment.