使用处理程序从服务更新 ui of activity 不调用服务的 oncreate() 和 start()Command

updating ui of activity from service using handler not call oncreate() and start()Command of service

我正在使用处理程序更新 ui 或每 15 秒从服务中显示 activity class 的吐司,所以我在 activity 中定义了处理程序对象并使用处理程序方法MainActivity.handler.sendemptymessage() 从服务 class 更新 ui 或每 15 秒显示一次 toast 但是当我启动服务时发生了什么点击服务 class 的按钮 oncreate() 没有显示我还创建了类似服务的吐司 onStartCommand 吐司未显示,只有一次 activity class 的吐司显示未重复显示,所以我的以下代码有什么问题这是我在 [ 中定义处理程序对象的正确方法=29=] class 还是我不知道,请给我适当的解决方案。我知道其他组件要将 ui 从服务更新到 activity,但我只想通过处理程序和 sendemptymessage.thanks 提前实现此

MainActivity-->

public class MainActivity extends ActionBarActivity {
    static Activity thisActivity = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button b1 = (Button) findViewById(R.id.button1);
        Button b2 = (Button) findViewById(R.id.button2);
        thisActivity = this;


        b1.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                startService(new Intent(MainActivity.this, ServiceDemo.class));
            }
        });

        b2.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                stopService(new Intent(MainActivity.this, ServiceDemo.class));
            }

        });

    }

    static Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {

            // This is where main activity thread receives messages
            // Put here your handling of incoming messages posted by other
            // threads
            super.handleMessage(msg);

            if (msg.what == 0) {

                show();
            }
        }

    };

    public static void show() {

        Toast.makeText(thisActivity, "recive called", Toast.LENGTH_SHORT)
                .show();

    }

}

Service.class-->

public class ServiceDemo extends Service {
    int mStartMode;
    /** interface for clients that bind */
    IBinder mBinder;
    /** indicates whether onRebind should be used */
    boolean mAllowRebind;

    /** Called when the service is being created. */
    @Override
    public void onCreate() {
        Toast.makeText(getApplicationContext(), " service created",
                Toast.LENGTH_SHORT).show();
    }

    /** The service is starting, due to a call to startService() */
    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {

        Toast.makeText(getApplicationContext(), "service started",
                Toast.LENGTH_SHORT).show();
        while (true) {
            try {
                Thread.sleep(15000);
                MainActivity.handler.sendEmptyMessage(0);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            return mStartMode;
        }

    }

    /** A client is binding to the service with bindService() */
    @Override
    public IBinder onBind(Intent intent) {
        return mBinder;
    }

    /** Called when all clients have unbound with unbindService() */
    @Override
    public boolean onUnbind(Intent intent) {
        return mAllowRebind;
    }

    /** Called when a client is binding to the service with bindService() */
    @Override
    public void onRebind(Intent intent) {

    }

    /** Called when The service is no longer used and is being destroyed */
    @Override
    public void onDestroy() {
        Toast.makeText(getApplicationContext(), "service destroyed",
                Toast.LENGTH_SHORT).show();
    }
}

在 Handler 或任何你拿走的东西里面,当你需要更新时 Ui 而不是使用,

runOnUiThread(new  Runnable()
            {
                public void run()
                {
                    //Code for ui update
                }
            });