给phonegap添加一个支持跳转的插件



1、版本问题
phonegap 在2.0以前和2.0以后进行插件开发的方式不同,我这一次使用的是1.7的。
2、开发的步骤
第一、编写插件类:

com.ljp.laucher.util.TargetActivityPlugin.java
package com.ljp.laucher.util;


import org.apache.cordova.api.Plugin;
import org.apache.cordova.api.PluginResult;
import org.json.JSONArray;


import android.app.Activity;
import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.util.Log;


public class TargetActivityPlugin extends Plugin {


@Override
public PluginResult execute(String action, JSONArray data, String callbackID) {
if(action.equals("startActivity")){  
            PluginResult result = null;  
              
            try {         
                PluginResult.Status status = PluginResult.Status.OK;  
                if(action.equals("startActivity")){   
                    Log.e("test", "test plugin js -> java~~~~"+data.getString(0));   
                    Log.e("test", "test plugin js -> java~~~~");                   
                    result = new PluginResult(status, data.getString(0));     
                    Message msg=new Message();  
                    msg.what=1;  
                    msg.obj=data.getString(0);  
                    handler.sendMessage(msg);  
                }  
            } catch (Exception e) {  
            }  
            return result;  
        }else {  
            return new PluginResult(PluginResult.Status.INVALID_ACTION);  
        }  
}

private Handler handler = new Handler() {  
       public void handleMessage(Message msg) {  
           if (msg == null) {  
               return;  
           }  
           switch (msg.what) {  
           case 1:  
               String className=msg.obj.toString();  
               try {  
                   Class activityClass = Class.forName(className);  
                   Intent intent = new Intent(ctx.getContext(), activityClass);//浣犳兂鍘荤殑activity(exp:Temp)  
                   ctx.startActivityForResult(TargetActivityPlugin.this, intent, 1);  
               } catch (ClassNotFoundException e) {  
                   e.printStackTrace();  
               }  
               break;  
           }  
       };  
   };  

   
   @Override
   public void onActivityResult(int requestCode, int resultCode, Intent intent) {
      // TODO Auto-generated method stub 
      if(requestCode==1){  
              if(resultCode == Activity.RESULT_CANCELED){  
              }  
          }else{  
              super.onActivityResult(requestCode, resultCode, intent);  
          } 
   }




}



第二、在MiLaucher\res\xml\plugins.xml中添加自己的插件


<?xml version="1.0" encoding="utf-8"?>
<!-- 
       Licensed to the Apache Software Foundation (ASF) under one
       or more contributor license agreements.  See the NOTICE file
       distributed with this work for additional information
       regarding copyright ownership.  The ASF licenses this file
       to you under the Apache License, Version 2.0 (the
       "License"); you may not use this file except in compliance
       with the License.  You may obtain a copy of the License at


         http://www.apache.org/licenses/LICENSE-2.0


       Unless required by applicable law or agreed to in writing,
       software distributed under the License is distributed on an
       "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
       KIND, either express or implied.  See the License for the
       specific language governing permissions and limitations
       under the License.
-->
<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="test01" value="com.elt.phonegaodemo4.test01"></plugin>


//<plugin name="给插件起个名字(有人说这个名字要和插件类名一致,经我测试,不需要)" value="包名.类名(插件类)"></plugin>
<plugin name="TargetActivityPlugin" value="com.ljp.laucher.util.TargetActivityPlugin"></plugin></plugins>






第三、在MiLaucher\assets\www\js\cordova-1.7.0.js最后添加对应的js操作代码

var targetActivityAPI = function() {
};
targetActivityAPI.prototype.startActivity = function(success, error, testData1) {
return PhoneGap.exec(success, error, 'TargetActivity', // java类名,plugins.xml中注册的名字
'startActivity', // action,Java方法中用来匹配的字段
[ testData1 ] // params 传递的参数,Array形式
);
};
PhoneGap.addConstructor(function() {
PhoneGap.addPlugin('targetActivityAPI', new targetActivityAPI());
});





第四、在MiLaucher\assets\www\index.html中就可以使用这个插件了

<script src="js/cordova-1.7.0.js"></script>
<script type="text/javascript">
var startActivity = function() {
window.plugins.targetActivityAPI.startActivity(null,null, "com.ljp.laucher.MiLaucherActivity");
}

$("#back").live("click",  null, startActivity);

</script>





第五、最后就是要在activity中载入html界面
package com.ljp.laucher;


import org.apache.cordova.DroidGap;


import android.os.Bundle;


public class MainActivity extends DroidGap {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
//        setContentView(R.layout.main);
        super.loadUrl("file:///android_asset/www/index.html");
    }
}





这样就可以使用这个插件了,
在开发这个插件的时候,自己也遇到了很多意想不到的问题,比如说js执行顺序问题,jquery绑定函数的问题,还有phonegap版本问题,等等!


简单记录一下吧:呵呵
jquery进行绑定函数的方式常用的有这两种:


$(".cell").bind("click", function() {
location.href = "testinfo.html";
});


var startActivity = function() {
window.plugins.targetActivityAPI.startActivity(null,null, "com.ljp.laucher.MiLaucherActivity");
}

$("#back").live("click",  null, startActivity);



记录结束!


                
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值