我正在从事聊天项目
我在这里所做的是与连接人员的服务,我设置了一个广播接收器以从服务获取数据
这是接收者代码
mMessageReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
try {
final String code = intent.getExtras().get("code").toString();
final JSONObject Message = new JSONObject(intent.getExtras().get("msg").toString());
runOnUiThread(new Runnable() {
@Override
public void run() {
Log.d("test","test");
}
});
} catch (JSONException e) {
e.printStackTrace();
}
}
};
LocalBroadcastManager.getInstance(this).registerReceiver(mMessageReceiver,new IntentFilter("data"));
Run Code Online (Sandbox Code Playgroud)
问题是当我关闭活动时 runOnUiThread 内的代码调用多次
如果我关闭活动并打开它,它会调用 2 次,如果我再次执行它,它会调用 3 次,依此类推
android broadcast broadcastreceiver android-runonuithread android-broadcastreceiver
我的清单中有 200 多个项目。RecyclerView 定期更新(每 10 秒)。RecyclerView 在更新期间阻塞 ui 线程几秒钟。我正在使用 notifyDataSetChanged刷新 recyclerview 的方法。有没有其他方法可以防止冻结?顺便说一句,我不想使用分页。
此方法每 10 秒运行一次:
public void refreshAssetList(List<Asset> newAssetList){
recyclerViewAdapter.setAssetList(newAssetList);
recyclerViewAdapter.notifyDataSetChanged();
}
Run Code Online (Sandbox Code Playgroud)
RecyclerViewAdapter 类:
public class AssetListRecyclerViewAdapter extends RecyclerView.Adapter<AssetListRecyclerViewAdapter.BaseViewHolder> {
private List<Asset> assetList;
Context context;
public AssetListRecyclerViewAdapter(List<Asset> assetList, Context context) {
this.assetList = assetList;
this.context = context;
}
public void setAssetList(List<Asset> assetList) {
this.assetList = assetList;
}
@Override
public BaseViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemLayoutView = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item_asset, null);
return new DetailViewHolder(itemLayoutView);
}
@Override
public void onBindViewHolder(BaseViewHolder holder, …Run Code Online (Sandbox Code Playgroud) android ui-thread android-runonuithread android-recyclerview
我在android studio中创建了一个Java类,并且想在此类中使用runOnUiThread()。我可以在Android中没有Activity.xml的情况下运行runOnUiThread()线程吗?如果答案是肯定的?比怎么?
代码 :
private void startTimer() {
final ScheduledExecutorService scheduleTaskExecutor = Executors.newScheduledThreadPool(1);
scheduleTaskExecutor.scheduleAtFixedRate(new Runnable() {
@Override
public void run() {
runOnUiThread(new Runnable() {
public void run() {
int count = 60;
time.setText(count - 1 + "");
count--;
}
});
}
}, 0 , 1000, TimeUnit.MILLISECONDS);
}
Run Code Online (Sandbox Code Playgroud)
我想每 1 秒更新一次 TextView 中的文本,但这似乎只适用于第一次,以后的文本不会更新。
有人知道是什么问题吗??
android textview scheduledexecutorservice android-runonuithread
public class ViewPagerTask extends TimerTask {
@Override
public void run() {
getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
if (viewPager.getCurrentItem() == 0){
viewPager.setCurrentItem(1);
} else if (viewPager.getCurrentItem() == 1){
viewPager.setCurrentItem(2);
} else viewPager.setCurrentItem(0);
}
});
}
}
Run Code Online (Sandbox Code Playgroud)
我正在使用此代码更改视图中的图像。但问题是我在片段中使用它,当我更改片段和应用程序运行几秒钟,然后突然弹出的空指针错误。现在我理解的原因是它试图更改图像但没有找到视图并创建此错误我不知道该怎么做。请帮忙 :)
错误
E/AndroidRuntime: FATAL EXCEPTION: Timer-0
Process: com.example.android.indianmetro, PID: 5150
java.lang.NullPointerException: Attempt to invoke virtual method 'void
android.support.v4.app.FragmentActivity.runOnUiThread(java.lang.Runnable)'
on a null object reference
at com.example.android.indianmetro.HomeFragment$ViewPagerTask.run(HomeFragment.java:258)
at java.util.TimerThread.mainLoop(Timer.java:555)
at java.util.TimerThread.run(Timer.java:505)
E/AbstractTracker: Can't create handler inside thread that has not called
Looper.prepare()
Run Code Online (Sandbox Code Playgroud) 我有一个单独的线程来处理一些信息,如果在执行期间出现问题,需要让用户知道错误,所以现在我调用runOnUIThread()来创建一个Dialog并让用户知道.我的问题是什么时候会发生这种情况?runOnUIThread()中的指令是在调用后立即发生的,还是等待UI线程完成它当前正在执行的操作?
我问,因为我将使用图形,为了定期更新屏幕,UI线程将被循环占用,以便我知道更新和重绘屏幕.
我目前正在从蓝牙传感器读取数据,因此数据会实时变化并且不断变化。我已将数据存储在变量中:liveData:ByteArray
现在我尝试将 liveData 从 MainActivity 发送到 Sensordisplayfragment。
更新
根据@CTD的评论,这就是我尝试过的,不幸的是我对viewModel没有太多了解,而且在线研究很混乱,因为似乎有很多方法来实现viewModel。
在我存储变量 liveData 的 MainActivity 类中:
val model:MyViewModel by viewModels()
private fun processLiveData(liveData : ByteArray){
livedata = liveData
model.uploadData(livedata)
}
Run Code Online (Sandbox Code Playgroud)
在 MyViewModel.class 中,viewModel 位于:
class MyViewModel: ViewModel() {
private val realtimedata = MutableLiveData<ByteArray>()
fun uploadData(data:ByteArray){
realtimedata.value = data
}
fun loadData():LiveData<ByteArray>{
return realtimedata
}
}
Run Code Online (Sandbox Code Playgroud)
最后,在我获取数据的 Sensordisplay 片段中:
val 模型:MyViewModel by viewModels()
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
model.loadData().observe(viewLifecycleOwner,Observer<ByteArray>{
passandprocessLiveData(it)
})
return inflater.inflate(R.layout.sensordisplay, container, false)
} …Run Code Online (Sandbox Code Playgroud) 我不知道CoroutineScope(Dispatchers.Main).launch和runOnUiThread之间有什么区别,我认为两者都会在主线程上运行。
但仍然很混乱,有什么区别吗?
谢谢。
multithreading android kotlin android-runonuithread kotlin-coroutines