跳至主要内容

how to hook android app function use html with javascript

Html5 and Android

为什么要这么做

在使用html开发手机端的时候我们就会常请求app来拿到手机内的数据,这时候我们就会用到接下来我要讲到的内容

怎么做

一开始我也一头雾水,没做过。 但是不要怕,跟着哥哥走,你会发现原来这很简单

这么做

我们的需求暂定如下
我们要加载一个html,在这个html里写一段javascript检查手机是否已经开启网络连接,如果没有开启网络连接我们给出提示,如果网络连接没有开启不能加载webview默认的错误页面(那样太不专业了)
So 跟着哥哥的脚步,一步一步来
先看这个类(其实这个就是我们准备在javascript中调用的类)
// call from html
private final class JsResload {

    @SuppressWarnings("unused")
    public void reload() {
        reloadWebView();
    }
}
reloadWevView文件实体如下
private void reloadWebView() {
    if (hasNetwork()) {
        mWebView.loadUrl(BluelionReading.BASE_API_URL + BookMethods.BOOK_STORE, getHeadersMap());
    } else {
        Toast.makeText(this, getString(R.string.network_notgelivable_refresh), Toast.LENGTH_SHORT).show();
        mWebView.loadUrl(DEFAULT_PAGE);
    }
}
其中的常量值如下:
public static final String  BOOK_STORE          = "/bookstore";
private static final String     DEFAULT_PAGE    = "file:///android_asset/about/wrong.html";
关键还是在这里,如何使用javascript调用这个java类呢? 接下来就是我们见证奇迹的时刻
JsResload jsResload = new JsResload();
mWebView.addJavascriptInterface(jsResload, "BluelionJSFrame");
我们通过webView的方法mWebView.addJavascriptInterface 把这个java类设给了webview,然后我们在javascript中调用这个方法如下:
function reloadPath() {
    BluelionJSFrame.reload();
}
当我们调用javascript的reloadPath这个方法就会请求webView, 当webview拦截到这个方法时就会调用我们写好的java类来处理。So easy! 是不是很简单啊。将来你们要使用html请求Android的数据 ,就应该知道如何使用了吧!
例如我们想写一个javascript方法从App中读取一个json格式的电话列表。这时候你就知道怎么去实现啦。 Html call Native

结论

Android在一开始 设计WebView时就把将来使用html做App应该的功能给设计进去了。 所以我们在现在有一个javascript和app沟通的桥,通过javascript与webview这个桥就可以访问手机上的数据了,还可以调用手机上的程序。

评论

此博客中的热门博文

How to change ViewPager scroll animation duration and velocity

When you call change current selected view pager position you may call like this. // change current position default is viewPager.setCurrentItem(position, true); viewPager . setCurrentItem ( position ); There will show transaction animation for change position , but this animation is too fast. So I want change this animation. When I see source code I find this, like flow. where is the scroll animation and speed.     int duration = 0 ;     velocity = Math . abs ( velocity );     if ( velocity > 0 ) {         duration = 4 * Math . round ( 1000 * Math . abs ( distance / velocity ));     } else {         final float pageWidth = width * mAdapter . getPageWidth ( mCurItem );         final float pageDelta = ( float ) Math . abs ( dx ) / ( pageWidth + mPageMargin );         duration = ( int ) (( pageDelta + 1 ) * 100 );     }     duration = Math . min ( duration , MAX_SETTLE_DURATION );     mScroller . startScroll ( sx , sy , dx , d

how to open context menu for android listview item button

Like title, here is this problem in my activity like this: An more button in every row of listview item . When I click this more button then show activity context menu. So I try to click button, but open context menu is the Adaper's context menu. My  Adapter  is extends from BaseAdapter ,  BaseAdapter  has not context menu. My solution like example : <ImageButton android:id="@+id/more" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:layout_centerVertical="true" android:layout_marginRight="15dp" android:background="#00000000" android:src="@drawable/btn_addressbook_more" android:onClick="onMoreClick"/> Add an event named  onMoreClick , when user click this button will do  onClick  event in  activity . // maybe you can registe

How to use Realm database in Kotlin

Hot to use Realm in android project. Befor you use realm we should be read documentation https://realm.io/docs/java/latest#getting-started Then we got the way how to install. Install Realm as a Gradle plugin. Setp 1:   change build.gradle in project level buildscript { repositories { jcenter() } dependencies { classpath "io.realm:realm-gradle-plugin:5.14.0" } } Step 2:   Apply the realm-android plugin to the top of the application level build.gradle file. Create reaml map Object There has two way for mapping Object Make   Object   extends   ReamlObject Or make   Object   implement   ReamlModel Declear reaml mapping objects Realm not support   data class   yet. So we can't use it now. And object will make it open, because Realm object will generate subclass after   aapt   or   kapt . // 1. open class User : RealmObject() { var id: String = UUID . random UUID() . to String() var name: String = "