<div >
<div class="normal_container">
<div class="pic_title_container" onclick="settings">
<div style="flex-direction : row;">
<text style="text-align : center; width : 30%; font-size : 60px; color : brown;">{{ randomChar}}</text>
<div style="flex-direction : column; width : 40%; margin-top: 20px;">
<text style="text-align : center; width : 100%; font-size : 25px;">
得分
</text>
<text style="text-align : center; width : 100%; font-size : 25px;color: blue;">
{{ score }}
</text>
</div>
<text style="text-align : center; font-size:60px; width : 30%;color: darkmagenta;">{{countDown}}</text>
</div>
<div style="margin-right : 10px;">
<button onclick="start" type="capsule" style="opacity: 0.7; margin-right : 10px; text-align : center; width : 33%;">开始</button>
<button onclick="stop" type="capsule" style="opacity: 0.7; margin-right : 10px; text-align : center; width : 33%;">停止</button>
<button onclick="extend" type="capsule" style="opacity: 0.7;text-align : center; width : 33%;">扩展</button>
</div>
</div>
</div>
</div>
Thread gameThread = new Thread(new Runnable() {
// 延迟放到最后
public void run() {
// 刷新服务卡片
while (true) {
try {
Thread.sleep(50);
if (startFlag) {
... ...
// 刷新服务卡片,产生随机字符
updateForm(gameWidgetFormId, formBindingData);
}
Thread.sleep(2000); // 没2秒刷新一次
}
} catch (Exception e) {
}
}
}
});
gameThread.start();
// 用于保持服务卡片的相关信息
public static class GameWidgetData {
public String leftValue = "4";
public String rightValue = "6";
public String leftBackgroundColor = "#FF0000";
public String rightBackgroundColor = "#FF00FF";
public String leftColor = "#FF00FF";
public String rightColor = "#FF0000";
}
public static Map<Long, GameWidgetData> gameWidgetFormIds = new HashMap<>();
protected ProviderFormInfo onCreateForm(Intent intent) {
... ...
if (formName.equals("GameWidget")) {
GameWidgetData gameWidgetData = new GameWidgetData();
gameWidgetFormIds.put(formId, gameWidgetData);
}
... ...
return formController.bindFormData();
}
// formId是被删除的服务卡片的id
@Override
protected void onDeleteForm(long formId) {
if (gamePanelFormId == formId) {
gamePanelFormId = 0;
} else { // 移除多余的服务卡片
gameWidgetFormIds.remove(formId);
}
}
DeviceManager.getDeviceList(DeviceInfo.FLAG_GET_ALL_DEVICE);
public boolean onSaveData(IntentParams intentParams) {
intentParams.setParam("ip", Tools.getLocalIP(this));
return true;
}
// 获取本地IP
public static String getLocalIP(Context context) {
try {
int ip = WifiDevice.getInstance(context).getIpInfo().get().getIpAddress() ;
String ipStr =
String.format("%d.%d.%d.%d",
(ip & 0xff),
(ip >> 8 & 0xff),
(ip >> 16 & 0xff),
(ip >> 24 & 0xff));
return ipStr;
} catch (Exception e) {
System.out.println("socket error:" +e.getMessage());
}
return "";
}
// 在A端调用startServer方法启动Socket服务
public void startServer() {
ServerSocket serverSocket = new ServerSocket(8888);
if (thread == null) {
thread = new Thread(new Runnable() {
public void run() {
while (true) {
try {
final Socket socket = serverSocket.accept();
// 等待B的连接
}catch (Exception e) {
}
} catch (Exception e) {
}
}
}
});
thread.start();
}
}
public boolean onRestoreData(IntentParams intentParams) {
// 获取A的IP
final String ip = intentParams.getParam("ip").toString();
Thread thread = new Thread(new Runnable() {
public void run() {
try {
// 连接A
clientSocket = new Socket(ip, 8888);
//获取输入输出流,与A交互
InputStream is = clientSocket.getInputStream();
OutputStream os = clientSocket.getOutputStream();
} catch (Exception e) {
}
}
});
thread.start();
return true;
}
package com.unitymarvel.harmonyos.projects.findme.common;
import ohos.app.Context;
import ohos.data.DatabaseHelper;
import ohos.data.rdb.RdbOpenCallback;
import ohos.data.rdb.RdbStore;
import ohos.data.rdb.StoreConfig;
import ohos.data.resultset.ResultSet;
import java.util.ArrayList;
import java.util.List;
public class DataService {
private Context context;
private RdbStore store;
public DataService(Context context) {
this.context = context;
StoreConfig config = StoreConfig.newDefaultConfig("game.sqlite");
RdbOpenCallback callback = new RdbOpenCallback() {
// 创建表时调用
public void onCreate(RdbStore store) {
// 创建t_users表
store.executeSql("CREATE TABLE IF NOT EXISTS t_records (id INTEGER PRIMARY KEY autoincrement, user VARCHAR(30), score int, time datetime default (datetime('now', 'localtime')))");
Tools.print("成功创建t_records表");
}
// 升级表时调用
public void onUpgrade(RdbStore store, int oldVersion, int newVersion) {
}
};
DatabaseHelper helper = new DatabaseHelper(context);
store = helper.getRdbStore(config,
1,
callback,
null);
}
// 保存积分数据
public void writeGameRecord(String user, int score) {
String insertSQL = "insert into t_records(user, score) values(?,?);";
// 向t_users表中插入3条记录
store.executeSql(insertSQL, new Object[]{user, score});
}
// 获取积分数据
public List<GameRecord> getGameRecords() {
ArrayList<GameRecord> result = new ArrayList<>();
String selectSQL = "select user, score, time from t_records order by score desc, time, user";
ResultSet resultSet = store.querySql(selectSQL, null);
while(resultSet.goToNextRow()) {
GameRecord record = new GameRecord();
record.user = resultSet.getString(0);
record.score = resultSet.getInt(1);
record.time = resultSet.getString(2);
result.add(record);
}
return result;
}
}
- EOF -
鸿蒙(HarmonyOS)API的源代码去哪了?竟然全抛出异常,原来使用的是虚拟API!
万字长文,教你如何发布鸿蒙(HarmonyOS)组件到Maven中央仓库,全网最全教程!
关注「极客起源」公众号,加星标,不错过精彩技术干货