Commit aff26ecf by 1256748979@qq.com

init

parent b1e36fde
package com.reyun.sdktestdemo;
import android.app.Application;
import android.util.Log;
import com.reyun.wandun.WdCallback;
import com.reyun.wandun.WdMain;
public class MyApplication extends Application {
public static String TAG = "reyunsdk_demo";
@Override
public void onCreate() {
super.onCreate();
//初始化sdk环境
WdMain wm = WdMain.getInstance();
wm.init(this, "test-a341fsfr3123ddadfs");
//同步获取唯一id
wm.getWdId(new WdCallback() {
@Override
public void onWdId(String wdId) {
Log.d(TAG, "onWdId: " + wdId);
}
/*
@Override
public void onError(String errorMsg) {
Log.d(TAG, "onError: " + errorMsg);
}*/
});
}
}
package com.reyun.wandun;
import android.os.Message;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
public class MdUtils {
public static String md5(String data) {
try {
MessageDigest md = MessageDigest.getInstance("md5");
byte[] result = md.digest(data.getBytes());
return bytes2HexString(result);
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
return null;
}
private static byte charToByte(char c) {
return (byte) "0123456789abcdef".indexOf(c);
}
public static byte[] hexString2Bytes(String hex) {
int len = (hex.length() / 2);
byte[] result = new byte[len];
char[] achar = hex.toCharArray();
for (int i = 0; i < len; i++) {
int pos = i * 2;
result[i] = (byte) (charToByte(achar[pos]) << 4 | charToByte(achar[pos + 1]));
}
return result;
}
public static String bytes2HexString(byte[] byteArray) {
if (byteArray == null) {
return null;
}
char[] hexArray = "0123456789ABCDEF".toCharArray();
char[] hexChars = new char[byteArray.length * 2];
for (int j = 0; j < byteArray.length; j++) {
int v = byteArray[j] & 0xFF;
hexChars[j * 2] = hexArray[v >>> 4];
hexChars[j * 2 + 1] = hexArray[v & 0x0F];
}
return new String(hexChars);
}
}
package com.reyun.wandun; package com.reyun.wandun;
public interface WdCallback { public interface WdCallback {
String onWdId(String wdId); void onWdId(String wdId);
// void onError(String errorMsg);
} }
package com.reyun.wandun; package com.reyun.wandun;
import android.content.Context; import android.content.Context;
import android.content.SharedPreferences;
public class WdMain { public class WdMain {
private static WdMain mInstance = new WdMain();
// 用户申请sdk后获得的认证码,调用sdk接口时需传入 // 用户申请sdk后获得的认证码,调用sdk接口时需传入
private String mApiKey; private String mApiKey;
...@@ -12,8 +15,15 @@ public class WdMain { ...@@ -12,8 +15,15 @@ public class WdMain {
//app context //app context
private Context mContext; private Context mContext;
public static WdMain getInstance(){
return mInstance;
}
//初始化方法, //初始化方法,
public static void init(Context context, String apiKey) { public void init(Context context, String apiKey) {
mContext = context;
mApiKey = apiKey;
//load native lib //load native lib
System.loadLibrary("wdun"); System.loadLibrary("wdun");
...@@ -22,7 +32,18 @@ public class WdMain { ...@@ -22,7 +32,18 @@ public class WdMain {
} }
//获取wdId //获取wdId
public static void getWdId(WdCallback cb) { public void getWdId(WdCallback cb) {
String wdId;
//文件中读取
SharedPreferences sp = mContext.getSharedPreferences("wd_dna", Context.MODE_PRIVATE);
wdId = sp.getString(MdUtils.md5("device_id"), null);
if(wdId == null) {
//重新请求服务器获取
wdId = getWdIdNative();
}
cb.onWdId(wdId);
} }
//native层获取wdId
public native String getWdIdNative();
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment