I want change the state of a ToggleButton
in my Activity when an event occurs in a service. Can anybody please help me to implement this using Handler?
I mean where should I write the code to create the handler and how to trigger it from a service? I read similar posts but, they were not really helpful in achieving this.
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
回答1:
In your Activity
public class MyActivity extends Activity {
public static Runnable runnable;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
runnable=new Runnable(){
@Override
public void run() {
//Change status here
}
};
From the service
Handler handler=new Handler();
handler.post(MyActivity.runnable);