Code Monkey home page Code Monkey logo

androidandh5's Introduction

AndroidAndH5

安卓H5交互,独立规则。 1,在交互规则里,js调用安卓,统一传一个action,作为标记,表明要执行的行动中的特定方法,然后传入参数,json格式的参数, 最后一个是回调方法。可以匿名实现回调方法。非常的方便。js调用安卓只需要一个方法,就能保证传入参数,action,还有回调方法。 2,安卓端,要声明方法实现。只需要在service包中建立一个继承抽象类ActionService的类。重写和实现里面的方法。 同时也对应着json字符串转对象的方法,或者不转对象,直接返回字符串。把类名作为字符串的action发给js端,把参数对象的格式也发给js端, js就按照这个方式去像1,中一样去调用安卓本地方法。 import android.app.Activity;

/**

  • Created by TL on 2016/6/1. */ public abstract class ActionService {

    protected Activity activity; public ActionService(Activity activity) { this.activity=activity; } public abstract void execute(T param,CallBackJs callBackJs); public abstract T fromJson(String params);

}

一个execute方法是js调用本地的方法,需要实现。参数param就是被fromJson转化来的参数。CallBackJs就是回调给js的方法。在实现execute中 可以调用回调方法,也可以不调用这个回调方法给jS.

例如js端执行这个方法。

var TestBean={ name:"lili", sex:"男", age:12 }; Local.execute('Test',TestBean,function(json){ alert(json); })

	传入action Test,TestBean参数。然后是匿名实现的回调方法。安卓中执行CallbackJs的回调,fucntion(json){ alert(json);}就会执行。
	
	大大方便了安卓端的逻辑,还有js端的调用。

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.