自从3.0之后。Cordova默认是关闭全部关于设备原生特性功能的,所以我们要通过加入插件来启动原生特性。

这里以Accelerometer(加速度感应器)为例,来学习怎样使用设备原生特性。

1.加入插件

首先,须要在project文件夹下。通过CLI命令加入插件。

1
cordova plugin add org.apache.cordova.device-motion

通过ls命令。能够查看当前项目下。已经安装的插件。

1
cordova plugin ls

2.在config.xml文件里配置该特性

路径:res/xml/config.xml

1
2
3
<feature name="Accelerometer">
<param name="android-package" value="org.apache.cordova.devicemotion.AccelListener" />
</feature>

完整配置例如以下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<?xml version='1.0' encoding='utf-8'?>
<widget id="com.example.hello.HelloWorld" version="0.0.1"
xmlns="http://www.w3.org/ns/widgets" xmlns:cdv="http://cordova.apache.org/ns/1.0">
<name>HelloCordova</name>
<description>
A sample Apache Cordova application that responds to the deviceready event.
</description>
<author email="dev@cordova.apache.org" href="http://cordova.io">
Apache Cordova Team
</author>
<content src="index.html" />
<access origin="*" />
<feature name="Accelerometer">
<param name="android-package" value="org.apache.cordova.devicemotion.AccelListener" />
</feature>
</widget>

某些插件还须要在Android的AndroidManifest.xml中加入uses-permission

比如:

1
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

当然。这里不须要!

3.API

1
navigator.accelerometer.getCurrentAcceleration(onSuccess, onError)

onSuccess和onError是相应的回调函数

4.完整样例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
<!DOCTYPE html>
<html>
<head>
<title>Acceleration Example</title>
<script type="text/javascript" charset="utf-8" src="cordova.js"></script>
<script type="text/javascript" charset="utf-8"> document.addEventListener("deviceready", onDeviceReady, false); function onDeviceReady() { } function getCurrrentAcceleration() {
navigator.accelerometer.getCurrentAcceleration(onSuccess, onError);
} function onSuccess(acceleration) {
alert('Acceleration X: ' + acceleration.x + '\n' +
'Acceleration Y: ' + acceleration.y + '\n' +
'Acceleration Z: ' + acceleration.z + '\n' +
'Timestamp: ' + acceleration.timestamp + '\n');
} function onError() {
alert('onError!');
}
</script>
</head>
<body>
<button onClick="getCurrrentAcceleration()">click</button>
</body>
</html>

假设用Android的原生API,用Java代码来实现同样功能呢,例如以下:

Activity

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import android.app.Activity;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.view.View;
import me.zeph.shakeshake.R;
import me.zeph.shakeshake.fragment.FireMissilesDialogFragment; import static android.hardware.Sensor.TYPE_ACCELEROMETER;
import static android.hardware.SensorManager.SENSOR_DELAY_NORMAL; public class MyActivity extends Activity { private SensorManager sensorManager;
private Sensor sensor;
private AccelerometerListener listener;
private String text; @Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
initSensor();
} private void initSensor() {
sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
sensor = sensorManager.getDefaultSensor(TYPE_ACCELEROMETER);
listener = new AccelerometerListener();
sensorManager.registerListener(listener, sensor, SENSOR_DELAY_NORMAL);
} @Override
protected void onStop() {
super.onStop();
sensorManager.unregisterListener(listener);
} public void sendMessage(View view) {
FireMissilesDialogFragment fireMissilesDialogFragment = new FireMissilesDialogFragment(text);
fireMissilesDialogFragment.show(getFragmentManager(), "some tag");
} class AccelerometerListener implements SensorEventListener { @Override
public void onSensorChanged(SensorEvent event) {
text = "Acceleration X: \n" + event.values[0] + "\n" +
"Acceleration Y: \n" + event.values[1] + "\n" +
"Acceleration Z: \n" + event.values[2] + "\n" +
"Timestamp: \n" + event.timestamp + "\n";
} @Override
public void onAccuracyChanged(Sensor sensor, int i) { } } }

Dialog

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.DialogFragment;
import android.content.DialogInterface;
import android.os.Bundle;
import me.zeph.shakeshake.R; public class FireMissilesDialogFragment extends DialogFragment {
private String text; public FireMissilesDialogFragment(String text) {
this.text = text;
} @Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder
.setTitle(R.string.dialog_accelerometer)
.setMessage(text)
.setNeutralButton(R.string.ok, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) { }
});
return builder.create();
}
}

main.xml

1
2
3
4
5
6
7
8
9
10
11
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<Button android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/click"
android:onClick="sendMessage"
/>
</LinearLayout>

版权声明:本文博主原创文章,博客,未经同意不得转载。

Cordova探险系列(三)的更多相关文章

  1. Cordova探险系列(一个)

    最早接触PhoneGap平台是在1年多之前,可以使用HTML.CSS和JavaScript跨平台来编写Android或者IOS设备程序.而且应用的核心代码不须要多少改动就行移植.确实让我感觉的到它应该 ...

  2. Cordova入门系列(三)Cordova插件调用 转发 https://www.cnblogs.com/lishuxue/p/6018416.html

    Cordova入门系列(三)Cordova插件调用   版权声明:本文为博主原创文章,转载请注明出处 上一章我们介绍了cordova android项目是如何运行的,这一章我们介绍cordova的核心 ...

  3. Cordova入门系列(四)自定义Cordova插件--showToast

    前三篇Cordova入门系列,简单讲解了Cordova,以及如何调用Cordova插件,今天我们讲解一下如何自己做一个插件. 自定义插件,就是自己写一些安卓java代码,然后和js代码以及配置文件,封 ...

  4. 前端构建大法 Gulp 系列 (三):gulp的4个API 让你成为gulp专家

    系列目录 前端构建大法 Gulp 系列 (一):为什么需要前端构建 前端构建大法 Gulp 系列 (二):为什么选择gulp 前端构建大法 Gulp 系列 (三):gulp的4个API 让你成为gul ...

  5. Web 开发人员和设计师必读文章推荐【系列三十】

    <Web 前端开发精华文章推荐>2014年第9期(总第30期)和大家见面了.梦想天空博客关注 前端开发 技术,分享各类能够提升网站用户体验的优秀 jQuery 插件,展示前沿的 HTML5 ...

  6. MyBatis学习系列三——结合Spring

    目录 MyBatis学习系列一之环境搭建 MyBatis学习系列二——增删改查 MyBatis学习系列三——结合Spring MyBatis在项目中应用一般都要结合Spring,这一章主要把MyBat ...

  7. MySQL并发复制系列三:MySQL和MariaDB实现对比

    http://blog.itpub.net/28218939/viewspace-1975856/ 并发复制(Parallel Replication) 系列三:MySQL 5.7 和MariaDB ...

  8. WCF编程系列(三)地址与绑定

    WCF编程系列(三)地址与绑定   地址     地址指定了接收消息的位置,WCF中地址以统一资源标识符(URI)的形式指定.URI由通讯协议和位置路径两部分组成,如示例一中的: http://loc ...

  9. 【JAVA编码专题】 JAVA字符编码系列三:Java应用中的编码问题

    这两天抽时间又总结/整理了一下各种编码的实际编码方式,和在Java应用中的使用情况,在这里记录下来以便日后参考. 为了构成一个完整的对文字编码的认识和深入把握,以便处理在Java开发过程中遇到的各种问 ...

随机推荐

  1. How to get the source code of the chromium of the specified revision

    I'd like to get the source code of the chromium 34.0.1847.9. gclient config http://src.chromium.org/ ...

  2. 用qsort排序

     冒泡,快排都是常见的排序方法,这里介绍用头文件里的qsort函数排序.只是自己要先一个cmp函数. #include<stdlib.h>//qsort的头文件 int a[100]= ...

  3. 怎样获取自己的SSL证书

    2.创建证书,注意这里的common name应该填你的server name $ openssl req -new -key key.pem -out request.pem Country Nam ...

  4. 数字证书, 数字签名, SSL(TLS) , SASL .

    因为项目中要用到TLS + SASL 来做安全认证层. 所以看了一些网上的资料, 这里做一个总结. 1. 首先推荐几个文章: 数字证书: http://www.cnblogs.com/hyddd/ar ...

  5. Theano+Keras+CUDA7.5+VS2013+Windows10x64配置

    Visual Studio 2013 正常安装,这里只要C++打勾就可以. ANACONDA ANACONDA是封装了Python的科学计算工具,装这个就可以不用额外装Python了.在安装之前建议先 ...

  6. ThinkPHP 自动创建数据、自动验证、自动完成详细例子介绍(十九)

    原文:ThinkPHP 自动创建数据.自动验证.自动完成详细例子介绍(十九) 1:自动创建数据 //$name=$_POST['name']; //$password=$_POST['password ...

  7. hdu2844(多重背包)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2844 题意:一位同学想要买手表,他有n种硬币,每种硬币已知有num[i]个.已知手表的价钱最多m元,问 ...

  8. Spring3.0官网文档学习笔记(七)--3.4.2

    3.4.2 依赖与配置的细节     3.4.2.1  Straight values (primitives, Strings, and so on)     JavaBeans PropertyE ...

  9. uboot启动阶段修改启动参数方法及分析

    作者:围补 本来启动方式这节不是什么复杂的事儿,不过想简单的说清楚明白,还真是不知道怎么组织.毕竟文字跟有声语言表达有别.但愿简单的东西别让我讲的太复杂! Arm板系统文件一般有三个——bootloa ...

  10. Gnu Linux--Ubuntu系统清理项整理

    /*********************************************************************  * Author  : Samson  * Date   ...