问题描述
我在Android应用中使用SignalR接收实时数据。当我的“ hubConnection.on()”方法启动时,它成功接收了数据,但是在此方法内部,它不允许我使用该数据。就像我写这样的代码时一样:
hubConnection = HubConnectionBuilder.create("http://www.example.com").build();
hubConnection.start();
hubConnection.on("ReceiveLocation",(lat,lng) -> {
Log.i("dataCheck","-> "+Double.parseDouble(lat));
Log.i("dataCheck","-> "+Double.parseDouble(lng));
if(hubConnection.getConnectionState() == HubConnectionState.disCONNECTED)
hubConnection.start();
},String.class,String.class);
上述案例数据已成功接收,并在日志中打印。
但是,当我尝试使用此“ lat”,“ lng”变量时,就像我尝试显示具有“ lat”值的吐司一样,数据无法接收此方法,并且吐司也不会显示在屏幕上。就像我使用这样的代码:
hubConnection = HubConnectionBuilder.create("http://www.example.com").build();
hubConnection.start();
hubConnection.on("ReceiveLocation",lng) -> {
Toast.makeText(OrderTrackingActivity.this,"->>>"+Double.parseDouble(lat),Toast.LENGTH_SHORT).show();
Log.i("dataCheck",String.class);
在第二个代码中,未接收到数据并且不显示吐司。请为我建议解决此问题的可能解决方案。我需要使用这些即将来临的数据。
按照一个建议,我尝试将“ lat”,“ lng”(。on()函数的变量)的值存储到我自己的定义变量中,然后将这些变量传递给我的函数名“ myFuntion”,如下所示:
hubConnection.on("ReceiveLocation",lng) -> {
varbl1 = lat;
varbl2 = lng;
Log.i("dataCheck","-> "+Double.parseDouble(lat));
Log.i("dataCheck","-> "+Double.parseDouble(lng));
myFuntion(varbl1,varbl2);
if(hubConnection.getConnectionState() == HubConnectionState.disCONNECTED)
hubConnection.start();
},String.class);
然后在“ myFuntion”中记录值并成功打印值,但仍然使用Toast中显示的值或执行其他操作时,signalR“ hubConnection.on()”函数停止工作。这是我的“ myFuntion”方法代码:
public void myFuntion(String value,String value2){
Toast.makeText(this,""+value,Toast.LENGTH_SHORT).show();
}
解决方法
这个问题是因为<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".ui.MainActivity">
<ImageView
android:id="@+id/imgBackground"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scaleType="centerInside"
android:src="@drawable/ic_launcher_track"
tools:ignore="MissingConstraints" />
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/bottomNavigationView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:itemRippleColor="#AD931E30"
android:background="#028C8C8C"
android:outlineAmbientShadowColor="#00931E30"
android:elevation="10dp"
android:layout_alignParentBottom= "true"
android:visibility="visible"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintVertical_bias="1.0"
app:menu="@menu/bottom_menu"
tools:visibility="visible"
>
<com.google.android.material.tabs.TabItem
android:background="#00424242"
android:id="@+id/go_to_home"
android:layout_width="100dp"
android:layout_height="70dp"
android:onClick="goToHome"
android:text="schmutz">
</com.google.android.material.tabs.TabItem>
<com.google.android.material.tabs.TabItem
android:id="@+id/go_to_dash"
android:layout_width="100dp"
android:layout_height="70dp"
android:layout_marginLeft="100dp"
android:onClick="goToDash"
android:text="schmutz" />
<com.google.android.material.tabs.TabItem
android:id="@+id/go_to_not"
android:layout_width="100dp"
android:layout_height="70dp"
android:layout_marginLeft="180dp"
android:onClick="goToNotification"
android:text="schmutz" />
<com.google.android.material.tabs.TabItem
android:id="@+id/go_to_profile"
android:layout_width="100dp"
android:layout_height="70dp"
android:layout_marginLeft="260dp"
android:text="schmutz"
android:onClick="goToProfile"/>
</com.google.android.material.bottomnavigation.BottomNavigationView>
<com.google.android.material.tabs.TabLayout
android:id="@+id/tab_tablayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:tabIndicatorColor="#00B8B8B8"
app:tabIndicatorHeight="3dp"
app:tabMode="fixed"
tools:ignore="MissingConstraints" />
<androidx.viewpager.widget.ViewPager
android:id="@+id/view_pager"
android:layout_width="match_parent"
android:layout_height="675dp"
android:layout_alignParentBottom="true"
android:layout_marginTop="?attr/actionBarSize"
android:layout_marginBottom="?attr/actionBarSize"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintVertical_bias="1.0" />
<fragment
android:id="@+id/fragment"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="1000dp"
android:layout_marginBottom="56dp"
app:defaultNavHost="true"
app:flow_horizontalAlign="center"
app:flow_verticalAlign="center"
app:navGraph="@navigation/my_nav"
tools:layout_editor_absoluteX="-27dp"
tools:layout_editor_absoluteY="278dp">
</fragment>
</RelativeLayout>
而不是UI Thread
,你可以试试这个代码
Signalr
如果您在 hubConnection.on("ReceiveTxt",(txt)-> {
Activity activity = (Activity) context;
activity.runOnUiThread(new Runnable() {
public void run() {
Toast.makeText(context,"->>> :" + txt,Toast.LENGTH_SHORT).show();
}
});
},String.class);
中,请使用此
Activity