我正在尝试在现有应用中实现新的 Android 导航组件。我有 2 个相同的片段,除了它们的名字。当我将 startDestination 设置为 fragment2 时,片段似乎显示正确。当 startDestination 设置为 fragment1 时,我看不到膨胀的视图,但我确实看到了“创建的片段 1”吐司。
我做错了什么?
class Fragment1 : Fragment() {
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?): View? {
Toast.makeText(context, "Fragment 1 created", Toast.LENGTH_LONG).show()
return inflater.inflate(R.layout.fragment1, container, false)
}
}
class Fragment2 : Fragment() {
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?): View? {
Toast.makeText(context, "Fragment 2 created", Toast.LENGTH_LONG).show()
return inflater.inflate(R.layout.fragment2, container, false)
}
}
Run Code Online (Sandbox Code Playgroud)
导航图.xml
<?xml version="1.0" encoding="utf-8"?>
<navigation xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/nav_graph"
app:startDestination="@id/fragment1">
<fragment
android:id="@+id/fragment1"
android:name="com.package.anotherpackage.ui.Fragment1" …Run Code Online (Sandbox Code Playgroud) android android-fragments kotlin android-jetpack android-architecture-navigation
我正在编写一个应用程序来检测蓝牙设备是否已连接.在做了一些研究后,我发现最好的方法是使用带有蓝牙相关意图过滤器的广播接收器.
<receiver android:name=".BTReceiver" >
<intent-filter>
<action android:name="android.bluetooth.BluetoothDevice.ACTION_ACL_CONNECTED" />
<action android:name="android.bluetooth.BluetoothDevice.ACTION_ACL_DISCONNECTED" />
<action android:name="android.bluetooth.BluetoothDevice.ACTION_ACL_DISCONNECT_REQUESTED" />
<action android:name="android.bluetooth.a2dp.profile.action.CONNECTION_STATE_CHANGED" />
</intent-filter>
</receiver>
Run Code Online (Sandbox Code Playgroud)
这是我的蓝牙接收器类.
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if(action.equals("android.bluetooth.BluetoothDevice.ACTION_ACL_CONNECTED") || action.equals("android.bluetooth.a2dp.profile.action.CONNECTION_STATE_CHANGED") ){
Log.d("Z","Received: Bluetooth Connected");
}
if(action.equals("android.bluetooth.BluetoothDevice.ACTION_ACL_DISCONNECTED") ||action.equals("android.bluetooth.BluetoothDevice.ACTION_ACL_DISCONNECTED_REQUESTED")){
Log.d("Z","Received: Bluetooth Disconnected");
}
Log.d("Z",action);
}
Run Code Online (Sandbox Code Playgroud)
当我打开我的蓝牙耳机并连接到我的手机时,我收到两次"android.bluetooth.a2dp.profile.action.CONNECTION_STATE_CHANGED".当我断开蓝牙耳机时,广播接收器不会运行或接收任何东西.所以我从未收到蓝牙断开连接日志消息.我正在使用我认为必要的两个蓝牙权限才能使其工作.
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
Run Code Online (Sandbox Code Playgroud)
我真的可以在这里使用任何有关错误的信息.谢谢.