Android开发学习:[23]IntentService使用

Android开发学习:[23]IntentService使用,因为多数启动服务不必同时处理多个请求(在多线程情景下会很危险),所以使用ItetService类实现服务是很好的选择。本经验将通过继承ItetService输出当前时间教大家如何使用ItetService。
因为多数启动服务不必同时处理多个请求(在多线程情景下会很危险),所以使用IntentService类实现服务是很好的选择。本经验将通过继承IntentService输出当前时间教大家如何使用IntentService。工具/原料AndroidStudio方法/步骤1首先我们打开安装好的AndroidStudio,然后新建一个项目步骤阅读2然后我们在界面布局中添加一个按钮用了打印时间的触发器  步骤阅读3然后我们在编写一个CurrentTimeService类,继承IntentServicepackagecom.basillee.asus.demo;importandroid.app.IntentService;importandroid.content.Intent;importandroid.text.format.Time;importandroid.util.Log;publicclassCurrentTimeServiceextendsIntentService{  publicCurrentTimeService(){    super(\"CurrentTimeService\");  }  @Override  protectedvoidonHandleIntent(Intentintent){    Timetime=newTime();    time.setToNow();    StringcurrentTime=time.format(\"%Y-%m-%d%H:%M:%S\");    Log.i(\"CurrentTimeService\",currentTime);  }}步骤阅读4然后我们在Oncreate方法里面编写如下代码为button增加监听事件packagecom.basillee.asus.demo;importandroid.app.Activity;importandroid.content.Intent;importandroid.os.Bundle;importandroid.view.View;importandroid.widget.Button;publicclassMainActivity5extendsActivity{  @Override  protectedvoidonCreate(BundlesavedInstanceState){    super.onCreate(savedInstanceState);    setContentView(R.layout.activity_main_activity5);    Buttonbutton=(Button)findViewById(R.id.button_current_time);    button.setOnClickListener(newView.OnClickListener(){      @Override      publicvoidonClick(Viewv){        startService(newIntent(MainActivity5.this,CurrentTimeService.class));      }    });  }}步骤阅读5然后我们点击运行此程序步骤阅读6点击按钮我们在logcat里面可以看到打印的时间步骤阅读END注意事项喜欢的同学可以继续关注我的android系列经验,里面都是本人学习android过程的经验Android开发学习(共41篇)上一篇:获取网络图片|下一篇:Android加载网页

本文来自投稿,不代表长河网立场,转载请注明出处: http://www.changhe99.com/a/Z4wDaa9dGL.html

(0)

相关推荐