使用处理程序从服务更新活动的ui不调用oncreate()和start()服务命令

时间:2015-03-31 12:17:42

标签: android service handler

我使用处理程序来更新ui或从服务中每15秒显示一次活动类的toast,所以我在activity中定义了handler对象,并使用处理器方法MainActivity.handler.sendemptymessage()从服务类更新ui或show toast每15秒,但是当我开始服务时点击按钮oncreate()服务类没有显示我像tovice一样的toast创建onStartCommand toast也没有显示,只有一次活动类的toast显示没有重复显示所以,我的错误是什么下面的代码是正确的方式,我在活动类中定义了处理程序对象,我不能弄清楚,请给我适当的解决方案。我知道其他组件将ui从服务更新为活动但我只想通过处理程序和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();
    }
}

1 个答案:

答案 0 :(得分:0)

在处理程序内部或您采取的任何操作,当您需要更新Ui而不是使用时,

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