您的位置:首页 > 其它

安卓 魔窗SDK 快速接入

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

公司要求的只是集成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)

<?xml version="1.0" encoding="utf-8"?>

<manifest xmlns:android="http://schemas.android.com/apk/res/android"

      package="com.powerfox.qp.klhnmj"

      android:versionCode="1"

      android:versionName="1.0"

      android:installLocation="auto">

    <uses-sdk android:minSdkVersion="14"/>

    <uses-feature android:glEsVersion="0x00020000" />

    <application android:label="@string/app_name"

                 android:icon="@drawable/icon">
 

        <!-- Tell Cocos2dxActivity the name of our .so -->

        <meta-data android:name="android.app.lib_name"
             android:value="cocos2dcpp" />

        <!-- 启动页面 --> 

        <activity android:name="org.cocos2dx.cpp.AppActivity"

                  android:label="@string/app_name"

                  android:screenOrientation="landscape"

                  android:theme="@android:style/Theme.NoTitleBar.Fullscreen"

                  android:configChanges="orientation|keyboardHidden|screenSize">

            <intent-filter>

                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />

            </intent-filter>

            <intent-filter>

        <data android:scheme="kuailaihngame"/>

        <action android:name="android.intent.action.VIEW" />

        <category android:name="android.intent.category.DEFAULT" />

        <category android:name="android.intent.category.BROWSABLE" />

    </intent-filter>

        </activity>

        

        <!-- 微信回调  -->

        <activity

            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"

        />

        

        

        

        <!--总的activity,必须注册!!! -->

<activity android:name="com.zxinsight.common.base.MWActivity" android:exported="true"

        android:configChanges="orientation|keyboardHidden|screenSize|navigation"/>

<activity android:name=".SplashActivity">

           

        </activity>

<!--MW sdk ID 此处跟activity同级,需要放在Application内,MW_APPID(也就是后台的” 魔窗AppKey”)不能更改 -->

<meta-data android:name="MW_APPID" android:value="RP35KW646Q7JBEL4FOKUHUTU55IRNX9E" />

<!--渠道名称MW_CHANNEL不能更改 -->

<meta-data android:name="MW_CHANNEL" android:value="应用宝" />

    </application>

    <supports-screens android:anyDensity="true"

                      android:smallScreens="true"

                      android:normalScreens="true"

                      android:largeScreens="true"

                      android:xlargeScreens="true"/>

    <uses-permission android:name="android.permission.INTERNET"/> 

    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/> 

    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/> 

    <uses-permission android:name="android.permission.READ_PHONE_STATE"/> 

    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />  

    <uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/>  

    <uses-permission android:name="android.permission.RECORD_AUDIO" />

    

    

    <!-- 连接互联网Internet权限 -->

<uses-permission android:name="android.permission.INTERNET" />

<!-- 允许应用程序联网,以便向我们的服务器端发送数据。 -->

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />

<!--判断程序是否在前台运行,必须 -->

<uses-permission android:name="android.permission.GET_TASKS" />

<!-- 检测手机基本状态 -->

<uses-permission android:name="android.permission.READ_PHONE_STATE" />

<!-- 缓存资源优先存入SDcard -->

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" />

    

</manifest> 

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<String, String> 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群  

我的三板斧,配合使用,威力无穷,搞懂了很多东西
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: