使用Messenger类绑定服务显示时间
2016-01-17 21:24
459 查看
1、布局文件
2、创建CurrentTimeService类,继承Service类,内部类IncomingHandler类继承Handler类
3、MainActivity
4AndroidManifest.xml
<service android:name=".CurrentTimeService"/>
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <Button android:id="@+id/current_time" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="当前时间" android:textColor="@android:color/black" android:textSize="25dp" /> </LinearLayout>
2、创建CurrentTimeService类,继承Service类,内部类IncomingHandler类继承Handler类
public class CurrentTimeService extends Service { public static final int CURRENT_TIME = 0; private class IncomingHandler extends Handler{ @Override public void handleMessage(Message msg) { if(msg.what==CURRENT_TIME){ Time time = new Time();//创建Time对象 time.setToNow();//设置时间为当前时间 String currentTime = time.format("%Y-%m-%d %H:%M:%S");//设置时间格式 Toast.makeText(CurrentTimeService.this, currentTime, Toast.LENGTH_SHORT).show(); }else{ super.handleMessage(msg); } } } @Override public IBinder onBind(Intent intent) { Messenger messenger = new Messenger(new IncomingHandler()); return messenger.getBinder(); } }
3、MainActivity
public class MainActivity extends Activity { Messenger messenger; boolean bound; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } @Override protected void onStart() { super.onStart(); Button button = (Button)findViewById(R.id.current_time); button.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(MainActivity.this,CurrentTimeService.class); bindService(intent,connection,BIND_AUTO_CREATE);//绑定服务 if(bound){ Message message = Message.obtain(null, CurrentTimeService.CURRENT_TIME, 0, 0); try { messenger.send(message); } catch (RemoteException e) { e.printStackTrace(); } } } }); } @Override protected void onStop() { super.onStop(); if(bound){ bound = false; unbindService(connection);//解绑定 } } private ServiceConnection connection = new ServiceConnection(){ public void onServiceConnected(android.content.ComponentName name, android.os.IBinder service) { messenger = new Messenger(service);
<span style="white-space:pre"> </span>bound = true; }; public void onServiceDisconnected(android.content.ComponentName name) { messenger = null; bound = false; } }; }
4AndroidManifest.xml
<service android:name=".CurrentTimeService"/>
相关文章推荐
- 你有想过现今的电车如此准时是因为大数据的原因吗?
- swift-var/let定义变量和常量
- 测试用例的编写-重在平衡
- 学习新的东东,是不断的看一本书,直到看懂,还是不断看新书,而每本书都要自己想要了解的东东呢?
- 1-2 Ping和Tracert
- 初识Lucene
- Android-----DialogFragment的使用
- iOS开发 UIImage图片拼接
- 利用BBRSACryptor实现iOS端的RSA加解密
- oozie action配置retry-max失效
- 利用BBRSACryptor实现iOS端的RSA加解密
- Havel-Hakimi定理
- linux中关于DNS的配置
- Tomcat运行时一闪而过
- ubuntu 安装完成后的工作
- 【追寻】
- C#下设置TextBox默认显示文字,点击后消失
- Linux虚拟机启动级别和网络设置
- C++学习笔记44——动态绑定
- mysql基础知识点