当listview的item里面有button的时候,发现listview的onitemclick事件就无效了,解决两者冲突的方法是:button里面加上android:focusable="false"属性
phonegap在android的创建方法见连接:http://www.phonegapcn.com/developers/get-started-13/get-started
phonegap插件指的就是能够在js中被调用的java程序
phonegap-plugin的创建步骤:
1、用java程序编写java类
2、在js文件中创建java类并调用java方法
3、在android项目的配置文件中添加插件声明
4、在html文件中调用js文件,实现插件功能
例子如下:
Html文件:
<!DOCTYPE HTML> <html>
<head>
<meta name="viewport" content="width=320; user-scalable=no" />
<meta http-equiv="Content-type" content="text/html; charset=utf-8">
<title>Simple Plugin Demo</title>
<script type="text/javascript" charset="utf-8" src="/blog_article/cordova-1.7.0.js"></script>
<script type="text/javascript" charset="utf-8" src="/blog_article/simplePlugin.js"></script>
<script type="text/javascript" charset="utf-8">
document.addEventListener('deviceready', function() {
var btn = document.getElementById("hello"); //获得Button的ID
var textbox = document.getElementById("name"); //获得输入框的ID
var output = document.getElementById("output"); //获得输出的ID
btn.addEventListener('click', function() { //为Button添加点击的事件监听器
var text = textbox.value; //得到输入框的的内容
//window.plugins是固定的; //simpleplugin是JavaScript中PhoneGap.addPlugin('simpleplugin', new SimplePlugin()); 的那个'simpleplugin'插件名称 //hello是JavaScript中 SimplePlugin.prototype.hello方法的名字"hello" window.plugins.simpleplugin.hello(text, function(result)
{ //成功时执行,传入的是Javascript中对象方法的参数 output.innerHTML = result;//把输入框的的内容的内容显示在输出div上
}, function(err) { //错误时执行
// failure callback
output.innerHTML = 'err: ' + err
+ ', Failed to invoke simple plugin';
});
});
}, true);
</script>
</head>
<body>
<p>
<label for="name">Enter Name: </label> <input type="text" name="name"
id="name" value="" />
<button id="hello">Say Hello</button>
</p>
<p>
<div id="output"></div>
</p>
</body>
</html>
JavaScript文件:
var SimplePlugin = function() {};
SimplePlugin.prototype.hello = function(name, successCallback, failureCallback) {
// exec 內的參數分別是: Success Callback, Failure Callback, Registered Plugin name:就是在XML文件配置的那个所对应的name,
// 'hello'是传入Java文件的execute方法中的参数String action
// name (從 HTML 傳進來的參數)
return PhoneGap.exec(successCallback, failureCallback, 'SimplePlugin',
'hello', [ name ]);
};
// 这里是 PhoneGap Plugin 的註冊,Plugin 的名稱還有 Native Class 的名稱別打錯了,就是我們剛剛輸入的那些
PhoneGap.addConstructor(function() {
// Register the javascript plugin with PhoneGap
PhoneGap.addPlugin('simpleplugin', new SimplePlugin()); //simpleplugin是插件名称, new SimplePlugin()实例化的是本Javascript的类名 });
JAVA文件:
package org.apache.cordova.example;
import java.util.Date;
import org.apache.cordova.api.PluginResult.Status;
import org.json.JSONArray;
import org.json.JSONException;
import com.phonegap.api.Plugin;
import com.phonegap.api.PluginResult;
public class SimplePlugin extends Plugin {
public static String ACTION_HELLO = "hello";
@Override
public PluginResult execute(String action, JSONArray data, String callbackId) { //action和data是javascript传进来的参数 PluginResult pluginResult = null;
if (ACTION_HELLO.equals(action)) {
String name;
try {
name = data.getString(0);
String result = "Hello " + name + "! The time is "
+ (new Date()).toString();
pluginResult = new PluginResult(Status.OK, result);
return pluginResult;
} catch (JSONException e) {
pluginResult = new PluginResult(Status.JSON_EXCEPTION,
"missing argument name");
}
} else {
pluginResult = new PluginResult(Status.INVALID_ACTION,
"Allowed actions is hello");
}
return pluginResult;
}}
XML文件:
<plugins>
<plugin name="App" value="org.apache.cordova.App"/>
<plugin name="Geolocation" value="org.apache.cordova.GeoBroker"/>
<plugin name="Device" value="org.apache.cordova.Device"/>
<plugin name="Accelerometer" value="org.apache.cordova.AccelListener"/>
<plugin name="Compass" value="org.apache.cordova.CompassListener"/>
<plugin name="Media" value="org.apache.cordova.AudioHandler"/>
<plugin name="Camera" value="org.apache.cordova.CameraLauncher"/>
<plugin name="Contacts" value="org.apache.cordova.ContactManager"/>
<plugin name="File" value="org.apache.cordova.FileUtils"/>
<plugin name="NetworkStatus" value="org.apache.cordova.NetworkManager"/>
<plugin name="Notification" value="org.apache.cordova.Notification"/>
<plugin name="Storage" value="org.apache.cordova.Storage"/>
<plugin name="Temperature" value="org.apache.cordova.TempListener"/>
<plugin name="FileTransfer" value="org.apache.cordova.FileTransfer"/>
<plugin name="Capture" value="org.apache.cordova.Capture"/>
<plugin name="Battery" value="org.apache.cordova.BatteryListener"/>
<plugin name="SplashScreen" value="org.apache.cordova.SplashScreen"/>
<!--添加的插件-->
<plugin name="SimplePlugin" value="org.apache.cordova.example.SimplePlugin" /> //name是参见的名字 ;value是创建插件的全称类名</plugins>
此示例根据用户手指在屏幕的x轴方向上的位置,来选择频率并生成音频样本。
因为代码比较简单,那么话不多说,直接上代码。
public class FingerSynthesis extends Activity implements OnTouchListener { AudioSynthesisTask audioSynth;//播放音频样本的线程 static final float BASE_FREQUENCY = 440;//基准音频频率 float synth_frequency = BASE_FREQUENCY; // 440 Hz, 随着手指的移动会发生改变 boolean play = false;//控制是否生成音频样本 @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); View mainView = this.findViewById(R.id.MainView); mainView.setOnTouchListener(this); audioSynth = new AudioSynthesisTask(); audioSynth.execute(); } @Override public void onPause() { super.onPause(); play = false; finish(); } public boolean onTouch(View v, MotionEvent event) { int action = event.getAction(); switch (action) { case MotionEvent.ACTION_DOWN: play = true;//根据用户操作将play设置为true或者false synth_frequency = event.getX() + BASE_FREQUENCY;//跟踪用户手指的位置,做出相应的改变 Log.v("FREQUENCY", "" + synth_frequency); break; case MotionEvent.ACTION_MOVE: play = true; synth_frequency = event.getX() + BASE_FREQUENCY; Log.v("FREQUENCY", "" + synth_frequency); break; case MotionEvent.ACTION_UP: play = false; break; case MotionEvent.ACTION_CANCEL: break; default: break; } return true; } private class AudioSynthesisTask extends AsyncTask<Void, Void, Void> { @Override protected Void doInBackground(Void... params) { final int SAMPLE_RATE = 11025; int minSize = AudioTrack.getMinBufferSize(SAMPLE_RATE, AudioFormat.CHANNEL_CONFIGURATION_MONO, AudioFormat.ENCODING_PCM_16BIT); AudioTrack audioTrack = new AudioTrack(AudioManager.STREAM_MUSIC, SAMPLE_RATE, AudioFormat.CHANNEL_CONFIGURATION_MONO, AudioFormat.ENCODING_PCM_16BIT, minSize, AudioTrack.MODE_STREAM); audioTrack.play(); short[] buffer = new short[minSize]; float angle = 0; while (true) { if (play) { for (int i = 0; i < buffer.length; i++) { float angular_frequency = (float) (2 * Math.PI) * synth_frequency / SAMPLE_RATE; buffer[i] = (short) (Short.MAX_VALUE * ((float) Math .sin(angle)));//生成音频样本 angle += angular_frequency; } audioTrack.write(buffer, 0, buffer.length); } else { try { Thread.sleep(50); } catch (InterruptedException e) { e.printStackTrace(); } } } } } }