0%

uniapp安卓开发杂记

自定义基座

下载源码

先去下载自定义基座的基础代码文件相关地址

使用其中的HBuilder-Integrate-AS文件包

增加自定义配置

  1. ~\HBuilder-Integrate-AS\simpleDemo\build.gradle增加如下的相关配置
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
dependencies {
implementation fileTree(dir: 'libs', include: ['*.aar', '*.jar'], exclude: [])
implementation 'androidx.appcompat:appcompat:1.1.0'
implementation 'androidx.localbroadcastmanager:localbroadcastmanager:1.0.0'
implementation 'androidx.core:core:1.1.0'
implementation "androidx.fragment:fragment:1.1.0"
implementation 'androidx.recyclerview:recyclerview:1.1.0'
implementation 'com.facebook.fresco:fresco:2.5.0'
implementation "com.facebook.fresco:animated-gif:2.5.0"
implementation 'com.github.bumptech.glide:glide:4.9.0'
implementation 'androidx.webkit:webkit:1.5.0'
implementation 'com.tencent.mm.opensdk:wechat-sdk-android:+'

// 自定义基座配置
implementation "com.alibaba:fastjson:1.2.83"
implementation "com.squareup.okhttp3:okhttp:3.12.12"
implementation("net.lingala.zip4j:zip4j:2.11.5")
implementation 'com.squareup.okio:okio:1.15.0'

debugImplementation fileTree(includes: ['.jar', '.aar'], dir: 'debug/libs')
}
  1. Androidmanifest.xml的application元素增加dcloud的appkey,这个要去网站申请,并且和证书匹配。
1
2
3
<meta-data
android:name="dcloud_appkey"
android:value="填写你的appkey" />
  1. (可选)接入微信功能,同上位置,加入以下代码
1
2
3
4
5
6
7
8
9
10
11
12
<meta-data android:value="填写你的APPID" android:name="WX_APPID"/>
<activity android:name="这里填写WXEntryActivity在项目中的路径"
android:label="@string/app_name"
android:exported="true"
android:launchMode="singleTop"
android:theme="@android:style/Theme.Translucent.NoTitleBar">
<intent-filter>
<action android:name="android.intent.action.VIEW"/>
<category android:name="android.intent.category.DEFAULT"/>
<data android:scheme="填写你的APPID"/>
</intent-filter>
</activity>

我是在\src\main\java\cn.com.wxapi创建的WXEntryActivity文件,内容如下

1
2
3
4
5
6
7
8
9
10
11
package cn.com.wxapi; // 确保这行代码的包名和您的文件路径一致

import io.dcloud.feature.oauth.weixin.AbsWXCallbackActivity;

public class WXEntryActivity extends AbsWXCallbackActivity {

// 这里是空的,不需要写任何代码。
// 所有微信的回调逻辑都由它的父类 AbsWXCallbackActivity 自动处理了。
// 这个父类会负责接收微信返回的结果,并将其传递给 uni-app 的前端JS。

}
  1. 导入data文件,位置在SDK/assets/data/,导入到项目的main/assets/data/中,修改dcloud_control.xml中的appid

运行安卓直接把hbuild生成的代码移动到项目的main/assets/apps/中

一些代码问题

流式传输

流式需要使用fetch-event-source自定义,通过renderjs + fetch实现

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
h5PostStream(url, data) {
let controller = null;
let onChunkCallback = null;
let onErrorCallback = null;
let onCompleteCallback = null;

const streamTask = {
// 设置数据块接收回调
onChunkReceived(callback) {
onChunkCallback = callback;
},

// 设置错误回调
onError(callback) {
onErrorCallback = callback;
},

// 设置完成回调
onComplete(callback) {
onCompleteCallback = callback;
},

// 中止请求
abort() {
if (controller) {
controller.abort();
}
},
};

// 启动 SSE 连接
setTimeout(() => {
controller = new AbortController();

fetchEventSource(baseurl + url, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
Authorization: getToken(),
Accept: 'text/event-stream',
},
body: JSON.stringify(data),
signal: controller.signal,

// 当连接打开时
onopen(response) {
if (response.ok) {
console.log('SSE 连接已建立');
return;
}

// 处理错误状态码
if (response.status === 401 || response.status === 403) {
throw new Error('认证失败');
}

throw new Error('连接失败: ' + response.status);
},

// 当收到消息时
onmessage(event) {
if (onChunkCallback && event.data) {
// 将字符串转换为 ArrayBuffer (模拟 uni.request 的返回格式)
const encoder = new TextEncoder();
const arrayBuffer = encoder.encode(event.data).buffer;

onChunkCallback({
data: arrayBuffer,
});
}
},

// 当发生错误时
onerror(err) {
console.error('SSE 错误:', err);

// 检查是否是认证错误
if (err.message && err.message.includes('认证失败')) {
useDialog('登录过期,请前往登录', { title: '提示', showCancelButton: true })
.then(() => {
uni.navigateTo({
url: '/pages/login/phone/index',
});
})
.catch(err => {});
}

if (onErrorCallback) {
onErrorCallback(err);
}

// 抛出错误以停止重连
throw err;
},

// 当连接关闭时
onclose() {
console.log('SSE 连接已关闭');
if (onCompleteCallback) {
onCompleteCallback();
}
},

// 不自动重连
openWhenHidden: false,
});
}, 0);

return streamTask;
},

echarts不同于小程序写法