利用phonegap(Cordova)的拓展我们可以利用本地的安卓或者ios代码拓展web App的功能,下面就来介绍一个分享插件,利用这一功能我们可以自定义好短信的内容然后发送给你的好友。
1.首先介绍phonegap必备的两个文件,分别是本地.java代码Share.java
/**
*
* Phonegap share plugin for Android
* Kevin Schaul 2011
*
*/
package com.tricedesigns;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.content.Intent;
import org.apache.cordova.api.Plugin;
import org.apache.cordova.api.PluginResult;
public class Share extends Plugin {
@Override
public PluginResult execute(String action, JSONArray args, String callbackId) {
try {
JSONObject jo = args.getJSONObject(0);
doSendIntent(jo.getString("subject"), jo.getString("text"));
return new PluginResult(PluginResult.Status.OK);
} catch (JSONException e) {
return new PluginResult(PluginResult.Status.JSON_EXCEPTION);
}
}
private void doSendIntent(String subject, String text) {
Intent sendIntent = new Intent(android.content.Intent.ACTION_SEND);
sendIntent.setType("text/plain");
sendIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, subject);
sendIntent.putExtra(android.content.Intent.EXTRA_TEXT, text);
this.cordova.startActivityForResult(this, sendIntent, 0);
}
}2.(.js文件share.js)
/**
*
* Phonegap share plugin for Android
* Kevin Schaul 2011
*
*/
var Share = {
show:function(content, success, fail) {
return cordova.exec( function(args) {
success(args);
}, function(args) {
fail(args);
}, 'Share', '', [content]);
}
};3.然后我们在phonegap项目中添加上述两个文件
4.在plugin.xml中添加语句(记得修改packageName)
<plugin name="Share" value="com.schaul.plugins.share.Share"/>
5.定义调用的js
function shareClick(){
Share.show({
subject: 'I like turtles',
text: 'http://www.mndaily.com'},
function() {}, // Success function
function() {alert('Share failed')} // Failure function
);
}效果如下:项目下载可进我的qq群共享(224711028 )
关于利用实参传递数组讨论.
简单说下背景:
最开始要求将一个数组的最小值取出来.例如int a[10] = { 0,11,2,23,4,95,64,71,8,29}
现在想要取最小值0.
题目本身很简单,做法如下.
int GetMin( int* a,int n )
{
int iIndex = 0;
for ( int i = 1;i <= 9;++ i )
{
if ( a[i] <= a[iIndex] )
{
iIndex = i;
}
}
return iIndex;
}
int main()
{
int a[10] = { 10,11,2,23,4,95,64,71,8,29};
cout<<"数组最小值为:"<<a[GetMin( a,10 )]<<endl;
system("pause");
return 0;
}
Main函数里面使用a[GetMin( a,10 )]其实本身是没有太多讨论的地方(起码我不想讨论),你可以单独写个语句存放索引,然后输出.
好,程序很快搞定,但是面试的人又突然问一个问题:
像这样的做法,如果a数组本来只有10个.但是别人在使用的时候出现了错误.传入了100.
那么程序将怎样修改呢?
说实话,之前确实没有想过这类问题,还记得传递数组的时候还是在学习老谭那本C的时候才有的经历.当然了,对于GetMin中的形参是指针,理论上应该可以判断是否为空的.
当时我并没给出判空条件.他给的意见是,为a判断是否为空操作.这样可以防止a索引非法.我给出又一个问题是:如果程序在使用的时候a这个指针内存后面(就是10个int后面)还有其他变量怎么办?如果简单的这样做,岂不出现错误?
后来回来我也一直在想这个问题,是啊,如果真如我所给出的问题那样,岂不悲剧了.
其实,这是一个编程常见的问题:接口调用参数非法问题. 别人需要的是数组索引,而你却传了一个错误索引.
其实,如果需要细说,得从两个方面来讲:
1. 指针判空处理当然是不正确的,因为程序可能处理的是一些不是很重要的数据,但是当数据显示在界面上的时候,如果出错就会造成问题.产品开发出来的时候,可能会经过几轮测试,测试人员最开始可能注重的是重要功能的测试,不重要的测试只是带过一下.而且,也可能出现的一种情况是,测试机上面a[]数组后面没有数据.测试机上没有问题.这样,等待产品上线了....回来定位的时候无法重现.这样定位其实有时候是很难的.
2. 我们就不应该在开发的时候就决解这个问题吗?如果把接口加上"容错机制",表面上程序没有问题,可是加重定位问题的难度.接口调用者非法调用函数,直接报错(对本题而言,报错的概率会很大).我认为这种方式最好.
最后还是要发下牢骚,公司的试题太坑爹了.居然会问1MB等于多少KB.做软件不知道这个,做啥软件啊?
IP地址查询,可以根据IP地址查询到该IP所在的地理经纬度坐标,比如下述例子查询IP 地址58.192.32.1,所在经纬度为118.777802,32.061699,为南京大学所在地。
package com.pstreets.gisengine.demo.lwuit;
//--------------------------------- IMPORTS ------------------------------------
import com.mapdigit.gis.DigitalMap;
import com.mapdigit.gis.MapPoint;
import com.mapdigit.gis.geometry.GeoLatLng;
import com.mapdigit.gis.raster.MapType;
import com.mapdigit.gis.service.IIpAddressGeocodingListener;
import com.mapdigit.gis.service.IpAddressLocation;
import com.pstreets.gisengine.demo.MapDemoLWUIT;
import com.sun.lwuit.Command;
import com.sun.lwuit.events.ActionEvent;
public class MapIpSearchLWUIT extends MapDemoLWUIT
implements IIpAddressGeocodingListener{
private Command mapFindAddressCommand;
public void startApp() {
init();
canvas.setTitle("IP Search");
mapFindAddressCommand=new Command("Find Address"){
public void actionPerformed(ActionEvent evt) {
map.getIpLocations("58.192.32.1");
}
};
canvas.addCommand(mapFindAddressCommand);
GeoLatLng center = new GeoLatLng(32.0616667, 118.7777778);
map.setCenter(center, 13, MapType.MICROSOFTCHINA);
map.setIpAddressGeocodingListener(this);
canvas.show();
}
public void done(String query, IpAddressLocation result) {
if (result != null && result.error.length() == 0
&& result.longitude.length() > 0
&& result.latitude.length() > 0) {
try {
MapPoint mapPoint = new MapPoint();
String latLng = "[" + result.longitude + ","
+ result.latitude + ",0]";
mapPoint.point = DigitalMap.fromStringToLatLng(latLng);
mapPoint.setName(result.organization);
mapPoint.setNote(result.city + " " + result.country);
map.panTo(mapPoint.point);
} catch (Exception e) {
result.error = "IP_NOT_FOUND";
}
}
}
}
注:目前IP查询结果总是以英文返回,如上述结果详细内容。
ISP:”China Education and Research Network”
Organization: “Nan Jing University”
Country: “CN”
City: “Nanjing”
若想知道该经纬度对应的地名,可以使用地址反编码服务。
你可以输入 127.0.0.1 查询本机地址。
LWUIT 引路蜂地图开发包Ver2.1下载