Cordova探险系列(三)

时间:2021-05-12 15:18:49

自从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>

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