安卓 魔窗SDK 快速接入


首先要清楚目标是什么?魔窗官网有成果视频展示,接入之前最好看一下,方向不对努力白费,不要急着对着文档操作 ,  理清思路,磨刀不误砍柴功。

公司要求的只是集成mlin功能,所以其他略去

名词解释:

1.mlink服务的配置(说白了就是指定页面的特殊url,形式上不同与超链接,但类似)
2.短链的生成是在魔窗系统后台生成的,生成的此短链用户点击后可以直接跳转至一个特定的浏览器页面,上面有一个按钮,点击后回调相关函数(此函数就是在eclipse中实现的,这个过程应该是走魔窗后台),唤醒app或是页面走到下载该app的指定位置

应该清楚    cocos、unity等集成的单 activity   官网给的文档是用安卓写的两个页面 两个activity 所以很坑      

其实单个activity 的集成还简单了呢,首先要导入jar包,这步是必须的,选中工程buidpath-》cofigerBuidPath     然后选中 library这个tab  addjar   引入jar  (事先要拷贝到libs下,找不到就刷新项目)

配置AndroidMainTest.xml文件

添加必要权限、设置魔窗appid、添加回调url    

我的粘贴到这里  有注释   (含微信sdk)


      package=”com.powerfox.qp.klhnmj”
      android:versionCode=”1″
      android:versionName=”1.0″
      android:installLocation=”auto”>

   
   

                     android:icon=”@drawable/icon”>
 
       
                     android:value=”cocos2dcpp” />

         
                          android:label=”@string/app_name”
                  android:screenOrientation=”landscape”
                  android:theme=”@android:style/Theme.NoTitleBar.Fullscreen”
                  android:configChanges=”orientation|keyboardHidden|screenSize”>

           
               
               
           

           
       
       
       
       
   


       
        
       
                    android:name=”com.powerfox.qp.klhnmj.wxapi.WXEntryActivity”
            android:label=”@string/app_name”
            android:exported=”true”
    android:launchMode=”singleTop”
    android:theme=”@android:style/Theme.Translucent”
        />
        
        
        
       
        android:configChanges=”orientation|keyboardHidden|screenSize|navigation”/>


           
       





   

                          android:smallScreens=”true”
                      android:normalScreens=”true”
                      android:largeScreens=”true”
                      android:xlargeScreens=”true”/>

     
     
     
     
   
     
     
   
    
    
   











    
 

2. 在activity类中实现一下函数即可    我的appActivity粘贴到这里(看官们有筛选的看)

/****************************************************************************
Copyright (c) 2008-2010 Ricardo Quesada
Copyright (c) 2010-2012 cocos2d-x.org
Copyright (c) 2011      Zynga Inc.
Copyright (c) 2013-2014 Chukong Technologies Inc.
 
http://www.cocos2d-x.org

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the “Software”), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
****************************************************************************/
package org.cocos2dx.cpp;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
import java.util.Map;

import org.cocos2dx.lib.Cocos2dxActivity;
import org.cocos2dx.lib.Cocos2dxHelper;

import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.*;

import com.powerfox.qp.klhnmj.wxapi.WXEntryActivity;
import com.zxinsight.MLink;
import com.zxinsight.MWConfiguration;
import com.zxinsight.MagicWindowSDK;
import com.zxinsight.Session;
import com.zxinsight.mlink.MLinkCallback;
import com.zxinsight.mlink.MLinkIntentBuilder;
import com.zxinsight.mlink.annotation.MLinkDefaultRouter;

import android.util.Log;
import android.widget.Toast;

//@MLinkDefaultRouter
public class AppActivity extends Cocos2dxActivity {

ProgressDialog mProgressDlg = null;
private int downLoadFileSize;

//******************添加代码*******集成Session环境*************

@Override
protected void onPause() {
       Session.onPause(this);
       super.onPause();
   }

@Override
protected void onResume() {
       Session.onResume(this);
       super.onResume();
}
@Override
 protected void onStart() {
   super.onStart();
   Uri mLink = getIntent().getData();
   if (mLink != null) {
       MagicWindowSDK.getMLink().router(mLink);
   } else {
   
    Log.d(“请您安装软件”, “请您安装软件”);
       //MLink.getInstance(this).checkYYB();
   }
 }

@Override
    public void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
initMW();
    registerForMLinkCallback();
    Uri mLink = getIntent().getData();
    MLink.getInstance(AppActivity.this).deferredRouter();

    if (mLink != null) {
        MLink.getInstance(this).router(mLink);
    } else {
        MLink.getInstance(this).checkYYB();
    }
}
private void initMW() {
    MWConfiguration config = new MWConfiguration(this);
    config.setDebugModel(true)
            .setPageTrackWithFragment(true)
            .setWebViewBroadcastOpen(true)
            .setSharePlatform(MWConfiguration.ORIGINAL);
    MagicWindowSDK.initSDK(config);
}
private static void registerForMLinkCallback() {
    MLink mLink = MagicWindowSDK.getMLink();
    mLink.registerDefault(new MLinkCallback() {
        @Override
        public void execute(Map paramMap, Uri uri, Context context) {
            //todo: 获取动态参数,用来处理
            String id = “”;
            if (paramMap != null) {
                id = paramMap.get(“homeid”);
            } else if(uri!=null) {
                id = uri.getQueryParameter(“homeid”);
            }
            //todo: 此处可以根据获取的动态参数id来做相应的处理

            Intent intent = new Intent(context, WXEntryActivity.class);
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
            context.startActivity(intent);
            Log.d(“*******homeid”, id);// ****已经获取到参数房间号****
            
        }
    });
}

@Override
public void onNewIntent(Intent intent) {
    super.onNewIntent(intent);
    Uri mLink = intent.getData();
    setIntent(intent);
    if (mLink != null) {
        MagicWindowSDK.getMLink().router(mLink);
    } else {
        MLink.getInstance(this).checkYYB();
    }
}

// 鎵撳紑涓嬭浇鏂囦欢瀹夎
private void openFile(File file) {
Intent intent = new Intent();
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setAction(android.content.Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(file),”application/vnd.android.package-archive”);
startActivity(intent);
this.finish();
}

// 涓嬭浇鏂囦欢
public void downFile(String url, String path) throws IOException {
String filename = url.substring(url.lastIndexOf(“/”) + 1);
// 鑾峰彇鏂囦欢鍚�
URL myURL = new URL(url);
URLConnection conn = myURL.openConnection();
conn.connect();
InputStream inputstream = conn.getInputStream();
final File file = new File(path + filename);
if (inputstream == null)
throw new RuntimeException(“stream is null”);
FileOutputStream fileOutputStream = new FileOutputStream(file);
// 鎶婃暟鎹瓨鍏ヨ矾寰�+鏂囦欢鍚�
byte buf[] = new byte[512];
downLoadFileSize = 0;
sendMsg(0);
do {
int numread = inputstream.read(buf);
if (numread == -1) {
break;
}
fileOutputStream.write(buf, 0, numread);
downLoadFileSize += numread;
sendMsg(1);// 鏇存柊杩涘害鏉�
} while (true);
sendMsg(2);// 閫氱煡涓嬭浇瀹屾垚
try {
fileOutputStream.close();
inputstream.close();
} catch (Exception ex) {
Log.e(“tag”, “error: ” + ex.getMessage(), ex);
}
}

private void sendMsg(int flag, String url, int size, String info,int isUpdate) 
{
Message msg = new Message();
msg.what = flag;
Bundle bundle = new Bundle();
bundle.putInt(“size”, size);
bundle.putString(“url”, url);
bundle.putInt(“isUpdate”, isUpdate);
bundle.putString(“info”, info);
msg.setData(bundle);
handler.sendMessage(msg);
}
private void sendMsg(int flag)
{
Message msg = new Message();
msg.what = flag;
handler.sendMessage(msg);
}

private Handler handler = new Handler() 
{
@Override
public void handleMessage(Message msg)
{
if (!Thread.currentThread().isInterrupted()) 
{
switch (msg.what) 
{
case 1:
if(mProgressDlg != null)
{
mProgressDlg.setProgress(downLoadFileSize / 1024);
}

break;
case 2:
Toast.makeText(AppActivity.this, “鏂囦欢涓嬭浇瀹屾垚”, 1).show();
String absPath = Environment.getExternalStorageDirectory()+ “/Android/data/”+
getApplicationInfo().packageName + “/files/”;
openFile(new File(absPath/* + filename*/));
break;
case 3:
Toast.makeText(AppActivity.this, “璇锋鏌ifi缃戠粶鏄惁寮�鍚�!”, 1).show();
break;
case -1:
Toast.makeText(AppActivity.this, “缃戠粶寮傚父锛屾枃浠朵笅杞藉け璐�!”, 1).show();
// Cocos2dxHelper.ExitUpdate(0);
break;
case 4:
break;
}
}

}
};
}

3。剩下的就是到磨窗后台配置东西了官网视频这方面讲的还真是详细    http://www.magicwindow.cn/doc/sdk-android.html

文档、社区 http://bbs.magicwindow.cn/  、QQ群  

我的三板斧,配合使用,威力无穷,搞懂了很多东西