Android用tabhost实现 界面切换,每个界面为一个独立的activity操作
作者:AI蜗牛车 时间:2021-11-09 11:22:26
我就废话不多说了,大家还是直接看代码吧~
// 要extends TabActivity
public class Main_activity extends TabActivity {
private TabHost tabHost;// 建立Tabhost控件
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
tabHost = getTabHost();
addTab("act1", "界面1", blue_tooth_Activity.class);//添加
addTab("act2", "界面2", map_Activity.class);
addTab("act3", "界面3", plane_parameter_activity.class);
setContentView(tabHost);// 显示
}
/**
* 添加Activity标签
* @param tag 标识
* @param title 标签标题
* @param clazz 激活的界面
*/
private void addTab(String tag, String title, Class clazz) {
TabHost.TabSpec tabSpec = tabHost.newTabSpec(tag);
tabSpec.setIndicator(title);
Intent intent = new Intent(getApplicationContext(),clazz);
tabSpec.setContent(intent);
tabHost.addTab(tabSpec);
}
@Override
protected void onStop() {
super.onStop();
}
}
补充知识:Android BLE中传输数据的最大长度怎么破
好多小伙伴们都被一个事儿困扰过:
想在gatt client上(一般是手机上)传输长一点的数据给gatt server(一般是一个Bluetooth smart设备,即只有BLE功能的设备),但通过
writeCharacteristic(BluetoothGattCharacteristic)
来写的时候发现最多只能写入20个byte的数据。
这篇文章会回答下面几个问题:
1)为什么会是20?
2)如何突破20?
3)如何更优雅的来实现?
第一个问题,为什么为限制成20个字节?
core spec里面定义了ATT的默认MTU为23个bytes,除去ATT的opcode一个字节以及ATT的handle2个字节之后,剩下的20个字节便是留给GATT的了。
考虑到有些Bluetooth smart设备功能弱小,不敢太奢侈的使用内存空间,因此core spec规定每一个设备都必须支持MTU为23。
在两个设备连接初期,大家都像新交的朋友一样,不知对方底细,因此严格的按照套路来走,即最多一次发20个字节,是最保险的。
由于ATT的最大长度为512byte,
因此一般认为MTU的最大长度为512个byte就够了,再大也没什么意义,你不可能发一个超过512的ATT的数据,就像是孙猴子跑不过五行山一样。
所以ATT的MTU的最大长度可视为512个bytes。
第二个问题,如何突破20?
很简单嘛,改变传输的ATT的MTU就行了,大家经过友好的协商,得到双方都想要的结果,是最好的。在Android上(API 21),改变ATT MTU的接口为:
public boolean requestMtu (int mtu) Added in API level 21 Request an MTU size used for a given connection. When performing a write request operation (write without response), the data sent is truncated to the MTU size. This function may be used to request a larger MTU size to be able to send more data at once. A onMtuChanged(BluetoothGatt, int, int) callback will indicate whether this operation was successful. Requires BLUETOOTH permission. Returns true, if the new MTU value has been requested successfully
大声的说出来你想要一下子传多少,调用上面的接口就可以了,然后在下面的函数中看最终结果(当然了,如果你的peripheral申请改变MTU并且成功的话,那这个回调也会被调用):
@Override
public void onMtuChanged(BluetoothGatt gatt, int mtu, int status) {
super.onMtuChanged(gatt, mtu, status);
if (status == BluetoothGatt.GATT_SUCCESS) {
this.supportedMTU = mtu;//local var to record MTU size
}
}
之后你就可以快乐的发送supportedMTU-3的长度的数据了。
第三个问题,如何优雅的来实现?
俗话说得好,郎有情就怕妾无意。万一对方设备不同意你的请求怎么办?
对于app来说,一般是知道自己要最大发送多少数据的,例如一次要发100个bytes,那么就首先试试申请一下103,失败的话,则申请一下53,即二分法,剩下的只能自己分段拆着发了。
一般来讲,app的开发者和对端设备的开发者都是同一伙儿人,这是好事儿,他们可以根据自己设备的硬件情况,来商量MTU应该是多少。
总之,把事儿摆到桌面上,提前做好,会使得你的程序更加professional。
来源:https://blog.csdn.net/qq_33431368/article/details/70799727
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
http协议进阶之Transfer-Encoding和HttpCore实现详解
![](https://img.aspxhome.com/file/2023/2/87212_0s.png)
SpringBoot整合Redis正确的实现分布式锁的示例代码
![](https://img.aspxhome.com/file/2023/7/81297_0s.png)
Java 8 Stream操作类型及peek示例解析
Android使用Intent.ACTION_SEND分享图片和文字内容的示例代码
Java实现的并发任务处理实例
Android Studio 通过登录功能介绍SQLite数据库的使用流程
解决JavaWeb读取本地json文件以及乱码的问题
详解springmvc控制登录用户session失效后跳转登录页面
Flutter 分页功能表格控件详细解析
![](https://img.aspxhome.com/file/2023/2/104692_0s.gif)
Android scrollToTop实现点击回到顶部(兼容PullTorefreshScrollview)
![](https://img.aspxhome.com/file/2023/2/122102_0s.png)
SpringBoot中使用JeecgBoot的Autopoi导出Excel的方法步骤
![](https://img.aspxhome.com/file/2023/8/99748_0s.png)
如何只返回实体类中的部分字段问题
![](https://img.aspxhome.com/file/2023/2/61282_0s.jpg)
mybatis-plus update更新操作的三种方式(小结)
![](https://img.aspxhome.com/file/2023/9/62639_0s.png)
Java协议字节操作工具类详情
利用Postman和Chrome的开发者功能探究项目(毕业设计项目)
![](https://img.aspxhome.com/file/2023/9/117519_0s.png)
SpringBoot之Json的序列化和反序列化问题
Struts2学习笔记(8)-Result常用类型
C# yield在WCF中的错误用法(一)
![](https://img.aspxhome.com/file/2023/1/86951_0s.png)
VS2022+unity3D开发环境搭建的实现步骤
![](https://img.aspxhome.com/file/2023/4/113894_0s.jpg)