【发布时间】:2017-03-15 12:08:58
【问题描述】:
我知道这可能是一个重复的问题,我已经尝试了堆栈中的所有答案,但没有一个完整的答案。
大多数答案只是给出了如何启动一个活动,但没有提示如何在 Android 清单文件中配置活动以及在哪里保存活动布局和清单文件。
谁能提供一个完整的代码结构来从cordova插件开始一个活动。
【问题讨论】:
标签: android cordova android-activity cordova-plugins
我知道这可能是一个重复的问题,我已经尝试了堆栈中的所有答案,但没有一个完整的答案。
大多数答案只是给出了如何启动一个活动,但没有提示如何在 Android 清单文件中配置活动以及在哪里保存活动布局和清单文件。
谁能提供一个完整的代码结构来从cordova插件开始一个活动。
【问题讨论】:
标签: android cordova android-activity cordova-plugins
这是从 cordova 插件开始活动的完整步骤
1.安装 Plugman 以创建插件
npm install -g plugman
2。使用 plugman 创建 cordova 插件
plugman create --name PluginName --plugin_id com.example.sample.plugin --plugin_version 0.0.1
注意:插件 id 永远不会以大写字母开头
现在将创建 PluginName 目录。插件结构将是
插件名称/
|- 插件.xml
|- src/
|- www/PluginName.js
3.将android平台添加到插件
plugman platform add --platform_name android
现在插件结构将是
插件名称/
|- 插件.xml
|- src/android/PluginName.java
|- www/PluginName.js
4.现在在 src/android 目录中创建一个名为 NewActivity.java 的 java 文件
此活动将使用我们的插件显示。
NewActivity.java
package com.example.sample.plugin;
import android.app.Activity;
import android.os.Bundle;
public class NewActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
String package_name = getApplication().getPackageName();
setContentView(getApplication().getResources().getIdentifier("activity_new", "layout", package_name));
}
}
5.现在在src/android目录下创建布局文件activity_new.xml
这是我们新活动的布局文件
activity_new.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="16dp"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:paddingTop="16dp"
tools:context="com.example.sample.plugin.NewActivity">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="New Activity"
android:id="@+id/textView"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="77dp" />
</RelativeLayout>
6.现在在 src/android
中编辑 PluginName.java现在我们需要处理请求并开始我们的新活动。
插件名称.java
package com.example.sample.plugin;
import android.content.Context;
import android.content.Intent;
import org.apache.cordova.CordovaPlugin;
import org.apache.cordova.CallbackContext;
import org.apache.cordova.CordovaWebView;
import org.apache.cordova.CordovaInterface;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
public class PluginName extends CordovaPlugin {
public void initialize(CordovaInterface cordova, CordovaWebView webView) {
super.initialize(cordova, webView);
}
@Override
public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException {
Context context = cordova.getActivity().getApplicationContext();
if(action.equals("new_activity")) {
this.openNewActivity(context);
return true;
}
return false;
}
private void openNewActivity(Context context) {
Intent intent = new Intent(context, NewActivity.class);
this.cordova.getActivity().startActivity(intent);
}
}
7.现在编辑 www 目录中的 PluginName.js
现在创建新方法来调用以启动我们的新活动。
var exec = require('cordova/exec');
function plugin() {
}
plugin.prototype.new_activity = function() {
exec(function(res){}, function(err){}, "PluginName", "new_activity", []);
}
module.exports = new plugin();
8.现在编辑 plugin.xml
现在我们需要将我们的文件指定为插件并在cordova AndroidManifest.xml 文件中进行必要的更改
plugin.xml
<?xml version='1.0' encoding='utf-8'?>
<plugin id="com.example.sample.plugin" version="0.0.1" xmlns="http://apache.org/cordova/ns/plugins/1.0" xmlns:android="http://schemas.android.com/apk/res/android">
<name>PluginName</name>
<js-module name="PluginName" src="www/PluginName.js">
<clobbers target="PluginName" />
</js-module>
<platform name="android">
<config-file parent="/*" target="res/xml/config.xml">
<feature name="PluginName">
<param name="android-package" value="com.example.sample.plugin.PluginName" />
</feature>
</config-file>
<config-file target="AndroidManifest.xml" parent="/manifest/application">
<activity android:label="New Activity" android:name="com.example.sample.plugin.NewActivity"></activity>
</config-file>
<config-file parent="/*" target="AndroidManifest.xml"></config-file>
<source-file src="src/android/PluginName.java" target-dir="src/com/example/sample/plugin" />
<source-file src="src/android/NewActivity.java" target-dir="src/com/example/sample/plugin" />
<source-file src="src/android/activity_new.xml" target-dir="res/layout"/>
</platform>
</plugin>
9.现在创建一个cordova项目
cordova create CordovaProject com.example.sample.cordovaproject "Cordova App"
10.将 android 平台添加到您的 cordova 项目
cordova platform add android
11.现在添加你的插件
cordova plugin add your-plugin-local-path
eg: cordova plugin add "C:\PluginName"
12.在www目录中的index.html中添加一个按钮
index.html
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Security-Policy" content="default-src 'self' data: gap: https://ssl.gstatic.com 'unsafe-eval'; style-src 'self' 'unsafe-inline'; media-src *">
<meta name="format-detection" content="telephone=no">
<meta name="msapplication-tap-highlight" content="no">
<meta name="viewport" content="user-scalable=no, initial-scale=1, maximum-scale=1, minimum-scale=1, width=device-width">
<link rel="stylesheet" type="text/css" href="css/index.css">
<title>Hello World</title>
</head>
<body>
<div class="app">
<h1>Apache Cordova</h1>
<div id="deviceready" class="blink">
<p class="event listening">Connecting to Device</p>
<p class="event received">Device is Ready</p>
</div>
<button id = "new_activity">New Activity</button>
</div>
<script type="text/javascript" src="cordova.js"></script>
<script type="text/javascript" src="js/index.js"></script>
</body>
</html>
13.在 www/js 目录中的 index.js 中为新按钮添加点击处理程序
当我们的按钮被点击时,我们将调用我们的插件方法来启动我们的新活动
index.js
var app = {
initialize: function() {
this.bindEvents();
},
bindEvents: function() {
document.addEventListener('deviceready', this.onDeviceReady, false);
},
onDeviceReady: function() {
app.receivedEvent('deviceready');
},
receivedEvent: function(id) {
var parentElement = document.getElementById(id);
var listeningElement = parentElement.querySelector('.listening');
var receivedElement = parentElement.querySelector('.received');
listeningElement.setAttribute('style', 'display:none;');
receivedElement.setAttribute('style', 'display:block;');
console.log('Received Event: ' + id);
document.getElementById("new_activity").addEventListener("click", new_activity);
}
};
app.initialize();
function new_activity() {
PluginName.new_activity();
}
14.现在在安卓手机上运行这个应用
cordova run android
如果所有这些步骤都成功并且当我们点击New Activity按钮时,我们的新Activity就会出现。
【讨论】:
你好,你可以使用这个插件
cordova plugin add sevensky-cordova-plugin-intent
用法:
document.getElementById("btn_device_name").addEventListener("click", test);
function test() {
var obj = new Object();
obj.name = "Ahmad"; //bundle string extra 1 string
obj.family = "Aghazadeh"; //bundle string extra 2
intentPlugin.startActivity("com.sevensky.test", "TestActivity", JSON.stringify(obj));
}
【讨论】: