上节学习如何创建了一个Service,并且用Activity启动一个Service。这节我们在上节的基础上更加完善
这节学习用StartServer启动服务,并且在服务中每隔一秒打印出当前时间
举例说明:
MyActivity代码:
public class MyActivity extends Activity { private Button btn_start; private Button btn_end; @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.activity_myservice); btn_start = (Button)findViewById(R.id.button1); btn_end = (Button)findViewById(R.id.button2); btn_start.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { // 启动服务 Intent intent = new Intent(MyActivity.this, MyService.class); startService(intent); } }); btn_end.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { // 销毁服务 Intent intent = new Intent(MyActivity.this, MyService.class); stopService(intent); } }); } }
public class MyService extends Service { @Override//必须实现的方法 public IBinder onBind(Intent arg0) { // TODO Auto-generated method stub Log.i("MyService", "onBind------------"); return null; } @Override//被创建时的调用 public void onCreate() { // TODO Auto-generated method stub super.onCreate(); Log.i("MyService", "onCreate------------"); } @Override @Deprecated//OnStart方法现在被OnStartCommd已经取代,其实在OnStartCommand也调用了OnStart public void onStart(Intent intent, int startId) { // TODO Auto-generated method stub super.onStart(intent, startId); Log.i("MyService", "onStart------------"); } @Override//启动时会调用 public int onStartCommand(Intent intent, int flags, int startId) { // TODO Auto-generated method stub Log.i("MyService", "onStartCommand------------"); for(int i=0; i<100; i++) { SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); String time = format.format(new Date()); Log.i("MyService", time); try { Thread.sleep(1000); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } return super.onStartCommand(intent, flags, startId); } @Override public void onDestroy() { // TODO Auto-generated method stub super.onDestroy(); Log.i("MyService", "onDestroy------------"); } @Override public boolean onUnbind(Intent intent) { // TODO Auto-generated method stub Log.i("MyService", "onUnbind------------"); return super.onUnbind(intent); } }
虽然说服务启动了,但是我们的界面却被服务的打印任务卡死了。所以对这么耗时的任务,我们必须放入一个线程中去,防止卡死GUI显示
所以我对MyService的代码做了修改:
public class MyService extends Service { private MyThread thread; private boolean stopFlag = false; @Override//必须实现的方法 public IBinder onBind(Intent arg0) { // TODO Auto-generated method stub Log.i("MyService", "onBind------------"); return null; } @Override//被创建时的调用 public void onCreate() { // TODO Auto-generated method stub super.onCreate(); //服务创建时,实例化MyThread thread = new MyThread(); Log.i("MyService", "onCreate------------"); } @Override @Deprecated//OnStart方法现在被OnStartCommd已经取代,其实在OnStartCommand也调用了OnStart public void onStart(Intent intent, int startId) { // TODO Auto-generated method stub super.onStart(intent, startId); Log.i("MyService", "onStart------------"); } @Override//启动时会调用 public int onStartCommand(Intent intent, int flags, int startId) { // TODO Auto-generated method stub Log.i("MyService", "onStartCommand------------"); //服务运行后,启动线程 if(!stopFlag) { thread.start(); thread.setFlag(true); } return super.onStartCommand(intent, flags, startId); } @Override public void onDestroy() { // TODO Auto-generated method stub super.onDestroy(); //服务销毁时,停止线程任务 thread.setFlag(false); Log.i("MyService", "onDestroy------------"); } @Override public boolean onUnbind(Intent intent) { // TODO Auto-generated method stub Log.i("MyService", "onUnbind------------"); return super.onUnbind(intent); } class MyThread extends Thread { //设置flag public void setFlag(Boolean flag) { stopFlag = flag; } @Override public void run() { super.run(); while(stopFlag) { //设置时间的输出方式 SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); String time = format.format(new Date()); //显示时间 Log.i("MyService", time); try { //延迟一秒 Thread.sleep(1000); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } } }
当我点击启动服务的button后
可以看到当点击启动服务的按钮后,先是create, 然后是onStartCommand(Onstart)
当我接着按启动服务时:
可以看到当服务再次启动后,再次启动服务,会只调用OnStartCommand函数的
接着销毁服务:
可以看到服务销毁了,同时我们的线程任务也停止了。
ok。 今天用StartServer启动服务就说到这里。
版权声明:本文为博主原创文章,未经博主允许不得转载。
原文地址:http://blog.csdn.net/longwang155069/article/details/46986803