我使用处理程序从服务中更新UI或每15秒显示活动类,所以我在活动中定义了处理程序对象,并使用服务程序中的处理程序方法MainActivity.handler.sendemptymessage()从服务类中更新UI或每15秒显示敬酒中,但是什么当我启动服务时,单击服务类的oncreate()按钮时没有发生像创建服务那样向我显示吐司的情况,也没有显示onStartCommand吐司,并且仅一次没有显示活动类的吐司,所以,在我的以下代码中出了什么问题我在活动类中定义处理程序对象的正确方法,我不知道请给我适当的解决方案。我知道其他组件可以将ui从服务更新为活动,但是我只想通过handler和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();
}
}
在处理程序中或您需要执行的任何操作中,当您需要更新Ui而不是Use时,
runOnUiThread(new Runnable()
{
public void run()
{
//Code for ui update
}
});
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句