草庐IT

runnables

全部标签

java - handler.post(runnable) 是否启动一个新线程?

如果处理程序是在主UI线程中实例化的,带有Runnable的帖子是否会创建一个添加到消息队列的子线程,或者它只是在UI线程中运行?handler.post(newRunnable(){publicvoidrun(){//dostuff}}); 最佳答案 不,它不会创建新线程。它只是在你的处理程序附加到的线程上执行你的可运行对象,在这种情况下意味着你​​的UI线程 关于java-handler.post(runnable)是否启动一个新线程?,我们在StackOverflow上找到一个类似

java - Android - 检查 Runnable 是否正在运行

我想检查Runnable在我的服务中是否存在。我有办法做到这一点吗?privateRunnablesendData=newRunnable(){publicvoidrun(){try{superToast.show();screenOn();vibrate();mHandler.postDelayed(sendData,time);}catch(Exceptione){e.printStackTrace();}}}; 最佳答案 您可以创建一个boolean变量并检查其值是否正在运行booleanisRunning=false;pri

android - 在实现 Runnable 的类中获取上下文

我在android中有一个应用程序,它位于与UI(实现Runnable)不同的线程中我收到一些数据(纬度和经度形式的GPS数据)并从这些数据中我想通过将地址传递给Geocoder来找出正确的地址......之后Geocoder返回的地址我将其存储在数据库中:这是我做这些事情的方法:publicclassClientimplemetsRunnable{publicvoidrun(){GeocodermyLocation=newGeocoder(getApllicationContext,Locale.getDefault());}}但我在这里遇到错误:GeocodermyLocation

android - 如何在 onClick 中重置 postDelayed runnable 的时间?

我有一个从服务器填充的listView。在ListItem的onClick中,我显示一个按钮x秒,然后再次使其不可见。每次调用onClick时如何重置时间?这是我的listItemonClick:privatevoiddisplayInCallButton(){mButton.setEnabled(true);if(canDisplayInCallControlls){canDisplayInCallControlls=false;fadeInAnimation(mButton);mButton.setEnabled(true);mFrontView.postDelayed(newRu

android - 如何启动/停止Runnable/Handler?

我正在尝试维护Web服务和Android应用程序之间的数据库同步。下面的代码可以正常工作,但我遇到了一些问题:每次我进入App的主页时,都会启动一个新的无限进程。过程永无止境任何人都可以按照我的意愿解释如何开始和停止这个过程吗?我希望此过程每5分钟运行一次,但只在应用程序打开时运行一次。publicvoidonCreate(BundlesavedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.main);finalHandlerhandler=newHandler();finalRunn

android - 在 Handler 中执行所有预定的(postDelayed)runnables

我使用一个处理程序,它通过postDelayed(r,DELAY_TIME)发布一些Runnable,但我需要在发布之前执行所有Runnables通过postDelayed新建。有什么好的想法可以尽可能简单地实现这一目标吗?编辑:我想要的基本上是这样的:Runnabler=newRunnable(){//Dosomefancyaction};if(mHandler.runnablesScheduled){mHandler.flushAllScheduledRunnables();}mHandler.postDelayed(r,DELAY); 最佳答案

android - runOnUiThread(new Runnable() { 标点符号(token)问题

不知何故它不起作用,根据我的说法应该是这样的:publicvoidSplash(){Timertimer=newTimer();timer.schedule(newTimerTask(){MexGame.this.runOnUiThread(newRunnable(){publicvoidrun(){SplashImage.setImageDrawable(aktieknop);}//Closesrun()});//ClosesrunOnUiThread((){})},SplashTime);//ClosestheTimeratask((){})}//closesSplash()有人知

android - 如何在 Android 中单击按钮时停止 Runnable?

我必须在单击开始按钮时启动runnable,并在单击暂停按钮时停止它。单击开始按钮时启动可运行的代码是//TODOAuto-generatedmethodstub//while(running){mUpdateTime=newRunnable(){publicvoidrun(){sec+=1;if(sec>=60){sec=0;min+=1;if(min>=60){min=0;hour+=1;}}Min_txtvw.setText(String.format(mTimeFormat,hour,min,sec));mHandler.postDelayed(mUpdateTime,1000

android - Runnable 中的上下文

我尝试播放来自R.raw的声音。在Thread/Runnable中但我无法让它工作。newRunnable(){publicvoidrun(){//thisisgivingmeaNullPointerException,becausegetBaseContextisnullMediaPlayermp=MediaPlayer.create(getBaseContext(),R.raw.soundfile);while(true){if(something)playsomething}}如何在run方法中获取真正的Context?无论我尝试什么,它都是空的。或者有更好的方法吗?

android - 为什么 notifyItemChanged 只能在 setAdapter 之后的 Post Runnable 中起作用?

publicclassMainActivityextendsAppCompatActivity{@OverrideprotectedvoidonCreate(BundlesavedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.main);RecyclerViewlist=(RecyclerView)findViewById(R.id.list);list.setLayoutManager(newLinearLayoutManager(this));finalRecyclerView.A