首页 文章 精选 留言 我的

精选列表

搜索[android],共10000篇文章
优秀的个人博客,低调大师

[Android]实现类似微信的延迟加载的Fragment——LazyFragment

以下内容为原创,转载请注明: 来自天天博客:http://www.cnblogs.com/tiantianbyconan/p/4303910.html 参考微信,使用ViewPager来显示不同的tab,每个tab是一个Fragment, 假设有3个tab,对应的fragment是FragmentA、FragmentB、FragmentC 需要实现的效果是进入后,默认先只加载FragmentA,具体滑动到了哪个Fragment,再去加载该Fragment的数据。 可以参考如下BaseLazyFragment代码: 1 /** 2 * Author: wangjie 3 * Email: tiantian.china.2@gmail.com 4 * Date: 1/23/15. 5 */ 6 public class BaseLazyFragment extends BaseFragment { 7 private static final String TAG = BaseLazyFragment.class.getSimpleName(); 8 private boolean isPrepared; 9 10 @Override 11 public void onActivityCreated(Bundle savedInstanceState) { 12 super.onActivityCreated(savedInstanceState); 13 initPrepare(); 14 } 15 16 17 /** 18 * 第一次onResume中的调用onUserVisible避免操作与onFirstUserVisible操作重复 19 */ 20 private boolean isFirstResume = true; 21 22 @Override 23 public void onResume() { 24 super.onResume(); 25 if (isFirstResume) { 26 isFirstResume = false; 27 return; 28 } 29 if (getUserVisibleHint()) { 30 onUserVisible(); 31 } 32 } 33 34 @Override 35 public void onPause() { 36 super.onPause(); 37 if (getUserVisibleHint()) { 38 onUserInvisible(); 39 } 40 } 41 42 private boolean isFirstVisible = true; 43 private boolean isFirstInvisible = true; 44 45 @Override 46 public void setUserVisibleHint(boolean isVisibleToUser) { 47 super.setUserVisibleHint(isVisibleToUser); 48 if (isVisibleToUser) { 49 if (isFirstVisible) { 50 isFirstVisible = false; 51 initPrepare(); 52 } else { 53 onUserVisible(); 54 } 55 } else { 56 if (isFirstInvisible) { 57 isFirstInvisible = false; 58 onFirstUserInvisible(); 59 } else { 60 onUserInvisible(); 61 } 62 } 63 } 64 65 public synchronized void initPrepare() { 66 if (isPrepared) { 67 onFirstUserVisible(); 68 } else { 69 isPrepared = true; 70 } 71 } 72 73 /** 74 * 第一次fragment可见(进行初始化工作) 75 */ 76 public void onFirstUserVisible() { 77 78 } 79 80 /** 81 * fragment可见(切换回来或者onResume) 82 */ 83 public void onUserVisible() { 84 85 } 86 87 /** 88 * 第一次fragment不可见(不建议在此处理事件) 89 */ 90 public void onFirstUserInvisible() { 91 92 } 93 94 /** 95 * fragment不可见(切换掉或者onPause) 96 */ 97 public void onUserInvisible() { 98 99 } 100 101 } 如上代码,使用setUserVisibleHint方法作为回调的依据, 暴露出来让子类使用的新的生命周期方法为: - onFirstUserVisible(); 第一次fragment可见(进行初始化工作) - onUserVisible(); fragment可见(切换回来或者onResume) - onFirstUserInvisible(); 第一次fragment不可见(不建议在此处理事件) - onUserInvisible(); fragment不可见(切换掉或者onPause) 具体的效果是: 1. 首先加载ViewPager,回调FragmentA(第一个默认呈现的Fragment)的onFirstUserVisible(),可以在这里进行FragmentA的初始化工作,其他Fragment保持不变。 2. 用户从FragmentA滑动到FragmentB,回调FragmentA的onUserInvisible()、FragmentB的onFirstUserVisible()(因为第一次切换到FragmentB,可以在这里进行初始化工作)。 3. 用户从FragmentB滑动到FragmentC,回调FragmentB的onUserInvisible()、FragmentC的onFirstUserVisible()(因为第一次切换到FragmentC,可以在这里进行初始化工作)。 4. 用户从FragmentC滑动到FragmentB,回调FragmentC的onUserInvisible()、FragmentB的onUserVisible()(因为FragmentB之前已经被加载过)。 5. 因为到此为止,suoyou的Fragment都已经被加载过了,所以以后这3个Fragment互相任意切换,只会回调原来Fragment的onUserInvisible()和切换后的Fragment的onUserVisible()。 6. 用户处于FragmentB,关闭手机屏幕,回调FragmentB的onUserInvisible()。 7.用户处于FragmentB,手机屏幕处关闭状态,然后开启手机屏幕解锁,只回调FragmentB的onUserVisible()。 本文转自天天_byconan博客园博客,原文链接:http://www.cnblogs.com/tiantianbyconan/p/4303910.html ,如需转载请自行联系原作者

优秀的个人博客,低调大师

Android 扫描附近的蓝牙设备并连接蓝牙音响

写了一个可以扫描附近蓝牙设备的小Demo,可以查看蓝牙设备的设备名和Mac地址 代码量不多,很容易看懂 /** * 作者:叶应是叶 * 时间:2017/9/8 20:13 * 描述: */ public class ScanDeviceActivity extends AppCompatActivity { private LoadingDialog loadingDialog; private DeviceAdapter deviceAdapter; private BluetoothAdapter bluetoothAdapter; private Handler handler = new Handler(); private BroadcastReceiver discoveryReceiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { switch (intent.getAction()) { case BluetoothAdapter.ACTION_DISCOVERY_STARTED: showLoadingDialog("正在搜索附近的蓝牙设备"); break; case BluetoothAdapter.ACTION_DISCOVERY_FINISHED: Toast.makeText(ScanDeviceActivity.this, "搜索结束", Toast.LENGTH_SHORT).show(); hideLoadingDialog(); break; case BluetoothDevice.ACTION_FOUND: BluetoothDevice bluetoothDevice = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE); deviceAdapter.addDevice(bluetoothDevice); deviceAdapter.notifyDataSetChanged(); break; } } }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_scan_device); BluetoothManager bluetoothManager = (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE); bluetoothAdapter = bluetoothManager.getAdapter(); if (bluetoothAdapter == null) { Toast.makeText(this, "当前设备不支持蓝牙", Toast.LENGTH_SHORT).show(); finish(); } initView(); registerDiscoveryReceiver(); startScan(); } @Override protected void onDestroy() { super.onDestroy(); handler.removeCallbacksAndMessages(null); unregisterReceiver(discoveryReceiver); if (bluetoothAdapter.isDiscovering()) { bluetoothAdapter.cancelDiscovery(); } } private void initView() { ListView lv_deviceList = (ListView) findViewById(R.id.lv_deviceList); deviceAdapter = new DeviceAdapter(this); lv_deviceList.setAdapter(deviceAdapter); } private void registerDiscoveryReceiver() { IntentFilter intentFilter = new IntentFilter(); intentFilter.addAction(BluetoothAdapter.ACTION_DISCOVERY_STARTED); intentFilter.addAction(BluetoothAdapter.ACTION_DISCOVERY_FINISHED); intentFilter.addAction(BluetoothDevice.ACTION_FOUND); intentFilter.addAction(BluetoothDevice.ACTION_BOND_STATE_CHANGED); registerReceiver(discoveryReceiver, intentFilter); } private void startScan() { if (!bluetoothAdapter.isEnabled()) { if (bluetoothAdapter.enable()) { handler.postDelayed(new Runnable() { @Override public void run() { scanDevice(); } }, 1500); } else { Toast.makeText(this, "请求蓝牙权限被拒绝,请授权", Toast.LENGTH_SHORT).show(); } } else { scanDevice(); } } private void scanDevice() { if (bluetoothAdapter.isDiscovering()) { bluetoothAdapter.cancelDiscovery(); } bluetoothAdapter.startDiscovery(); } private void showLoadingDialog(String message) { if (loadingDialog == null) { loadingDialog = new LoadingDialog(this); } loadingDialog.show(message, true, false); } private void hideLoadingDialog() { if (loadingDialog != null) { loadingDialog.dismiss(); } } } 此外,还可以通过利用反射来调用系统API,从而与支持蓝牙A2DP协议的蓝牙音响连接上,不过因为我只有一部不算严格意义上的蓝牙音响来做测试,所以这个功能并不确定是否适用于大多数蓝牙设备 /** * 作者:叶应是叶 * 时间:2017/9/8 20:02 * 描述: */ public class ConnectA2dpActivity extends AppCompatActivity { private DeviceAdapter deviceAdapter; private BluetoothAdapter bluetoothAdapter; private Handler handler = new Handler(); private BluetoothA2dp bluetoothA2dp; private LoadingDialog loadingDialog; private final String TAG = "ConnectA2dpActivity"; private BroadcastReceiver a2dpReceiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { switch (intent.getAction()) { case BluetoothA2dp.ACTION_CONNECTION_STATE_CHANGED: int connectState = intent.getIntExtra(BluetoothA2dp.EXTRA_STATE, BluetoothA2dp.STATE_DISCONNECTED); if (connectState == BluetoothA2dp.STATE_DISCONNECTED) { Toast.makeText(ConnectA2dpActivity.this, "已断开连接", Toast.LENGTH_SHORT).show(); } else if (connectState == BluetoothA2dp.STATE_CONNECTED) { Toast.makeText(ConnectA2dpActivity.this, "已连接", Toast.LENGTH_SHORT).show(); } break; case BluetoothA2dp.ACTION_PLAYING_STATE_CHANGED: int playState = intent.getIntExtra(BluetoothA2dp.EXTRA_STATE, BluetoothA2dp.STATE_NOT_PLAYING); if (playState == BluetoothA2dp.STATE_PLAYING) { Toast.makeText(ConnectA2dpActivity.this, "处于播放状态", Toast.LENGTH_SHORT).show(); } else if (playState == BluetoothA2dp.STATE_NOT_PLAYING) { Toast.makeText(ConnectA2dpActivity.this, "未在播放", Toast.LENGTH_SHORT).show(); } break; } } }; private BroadcastReceiver discoveryReceiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { switch (intent.getAction()) { case BluetoothAdapter.ACTION_DISCOVERY_STARTED: showLoadingDialog("正在搜索蓝牙设备,搜索时间大约一分钟"); break; case BluetoothAdapter.ACTION_DISCOVERY_FINISHED: Toast.makeText(ConnectA2dpActivity.this, "搜索蓝牙设备结束", Toast.LENGTH_SHORT).show(); hideLoadingDialog(); break; case BluetoothDevice.ACTION_FOUND: BluetoothDevice bluetoothDevice = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE); deviceAdapter.addDevice(bluetoothDevice); deviceAdapter.notifyDataSetChanged(); break; case BluetoothDevice.ACTION_BOND_STATE_CHANGED: int status = intent.getIntExtra(BluetoothDevice.EXTRA_BOND_STATE, BluetoothDevice.BOND_NONE); if (status == BluetoothDevice.BOND_BONDED) { Toast.makeText(ConnectA2dpActivity.this, "已连接", Toast.LENGTH_SHORT).show(); } else if (status == BluetoothDevice.BOND_NONE) { Toast.makeText(ConnectA2dpActivity.this, "未连接", Toast.LENGTH_SHORT).show(); } hideLoadingDialog(); break; } } }; private BluetoothProfile.ServiceListener profileServiceListener = new BluetoothProfile.ServiceListener() { @Override public void onServiceDisconnected(int profile) { if (profile == BluetoothProfile.A2DP) { Toast.makeText(ConnectA2dpActivity.this, "onServiceDisconnected", Toast.LENGTH_SHORT).show(); bluetoothA2dp = null; } } @Override public void onServiceConnected(int profile, final BluetoothProfile proxy) { if (profile == BluetoothProfile.A2DP) { Toast.makeText(ConnectA2dpActivity.this, "onServiceConnected", Toast.LENGTH_SHORT).show(); bluetoothA2dp = (BluetoothA2dp) proxy; } } }; private AdapterView.OnItemClickListener itemClickListener = new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { BluetoothDevice device = deviceAdapter.getDevice(position); if (device.getBondState() == BluetoothDevice.BOND_BONDED) { Toast.makeText(ConnectA2dpActivity.this, "已连接该设备", Toast.LENGTH_SHORT).show(); return; } showLoadingDialog("正在连接"); connectA2dp(device); } }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_connect_a2dp); BluetoothManager bluetoothManager = (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE); bluetoothAdapter = bluetoothManager.getAdapter(); if (bluetoothAdapter == null || !getPackageManager().hasSystemFeature(PackageManager.FEATURE_BLUETOOTH_LE)) { Toast.makeText(ConnectA2dpActivity.this, "当前设备不支持蓝牙", Toast.LENGTH_SHORT).show(); finish(); } bluetoothAdapter.getProfileProxy(this, profileServiceListener, BluetoothProfile.A2DP); initView(); registerDiscoveryReceiver(); registerA2dpReceiver(); startScan(); } @Override protected void onDestroy() { super.onDestroy(); handler.removeCallbacksAndMessages(null); unregisterReceiver(a2dpReceiver); unregisterReceiver(discoveryReceiver); if (bluetoothAdapter.isDiscovering()) { bluetoothAdapter.cancelDiscovery(); } } private void initView() { ListView lv_deviceList = (ListView) findViewById(R.id.lv_deviceList); deviceAdapter = new DeviceAdapter(this); lv_deviceList.setAdapter(deviceAdapter); lv_deviceList.setOnItemClickListener(itemClickListener); } private void registerDiscoveryReceiver() { IntentFilter intentFilter = new IntentFilter(); intentFilter.addAction(BluetoothAdapter.ACTION_DISCOVERY_STARTED); intentFilter.addAction(BluetoothAdapter.ACTION_DISCOVERY_FINISHED); intentFilter.addAction(BluetoothDevice.ACTION_FOUND); intentFilter.addAction(BluetoothDevice.ACTION_BOND_STATE_CHANGED); registerReceiver(discoveryReceiver, intentFilter); } private void registerA2dpReceiver() { IntentFilter intentFilter = new IntentFilter(); intentFilter.addAction(BluetoothA2dp.ACTION_CONNECTION_STATE_CHANGED); intentFilter.addAction(BluetoothA2dp.ACTION_PLAYING_STATE_CHANGED); registerReceiver(a2dpReceiver, intentFilter); } private void startScan() { if (!bluetoothAdapter.isEnabled()) { if (bluetoothAdapter.enable()) { handler.postDelayed(new Runnable() { @Override public void run() { scanDevice(); } }, 1500); } else { Toast.makeText(ConnectA2dpActivity.this, "请求蓝牙权限被拒绝", Toast.LENGTH_SHORT).show(); } } else { scanDevice(); } } private void scanDevice() { if (bluetoothAdapter.isDiscovering()) { bluetoothAdapter.cancelDiscovery(); } bluetoothAdapter.startDiscovery(); } public void setPriority(BluetoothDevice device, int priority) { try { Method connectMethod = BluetoothA2dp.class.getMethod("setPriority", BluetoothDevice.class, int.class); connectMethod.invoke(bluetoothA2dp, device, priority); } catch (Exception e) { e.printStackTrace(); } } private void connectA2dp(BluetoothDevice bluetoothDevice) { if (bluetoothA2dp == null || bluetoothDevice == null) { return; } setPriority(bluetoothDevice, 100); try { Method connectMethod = BluetoothA2dp.class.getMethod("connect", BluetoothDevice.class); connectMethod.invoke(bluetoothA2dp, bluetoothDevice); } catch (Exception e) { e.printStackTrace(); } } private void showLoadingDialog(String message) { if (loadingDialog == null) { loadingDialog = new LoadingDialog(this); } loadingDialog.show(message, true, false); } private void hideLoadingDialog() { if (loadingDialog != null) { loadingDialog.dismiss(); } } } 这里给出源代码供大家参考:BluetoothDemo

资源下载

更多资源
Mario

Mario

马里奥是站在游戏界顶峰的超人气多面角色。马里奥靠吃蘑菇成长,特征是大鼻子、头戴帽子、身穿背带裤,还留着胡子。与他的双胞胎兄弟路易基一起,长年担任任天堂的招牌角色。

腾讯云软件源

腾讯云软件源

为解决软件依赖安装时官方源访问速度慢的问题,腾讯云为一些软件搭建了缓存服务。您可以通过使用腾讯云软件源站来提升依赖包的安装速度。为了方便用户自由搭建服务架构,目前腾讯云软件源站支持公网访问和内网访问。

Nacos

Nacos

Nacos /nɑ:kəʊs/ 是 Dynamic Naming and Configuration Service 的首字母简称,一个易于构建 AI Agent 应用的动态服务发现、配置管理和AI智能体管理平台。Nacos 致力于帮助您发现、配置和管理微服务及AI智能体应用。Nacos 提供了一组简单易用的特性集,帮助您快速实现动态服务发现、服务配置、服务元数据、流量管理。Nacos 帮助您更敏捷和容易地构建、交付和管理微服务平台。

Rocky Linux

Rocky Linux

Rocky Linux(中文名:洛基)是由Gregory Kurtzer于2020年12月发起的企业级Linux发行版,作为CentOS稳定版停止维护后与RHEL(Red Hat Enterprise Linux)完全兼容的开源替代方案,由社区拥有并管理,支持x86_64、aarch64等架构。其通过重新编译RHEL源代码提供长期稳定性,采用模块化包装和SELinux安全架构,默认包含GNOME桌面环境及XFS文件系统,支持十年生命周期更新。

用户登录
用户注册
修改密码
忘记密码