android Bound Service使用:使用Message类绑定服务
来源:互联网 发布:linux中国开源社区 编辑:程序博客网 时间:2024/04/30 17:32
activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".MainActivity" > <Button android:id="@+id/current_time" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="当前时间" android:textColor="@android:color/white" android:textSize="25dp"/></RelativeLayout>
CurrentTimeService1.java
package com.example.demoboundservice;import android.app.Service;import android.content.Intent;import android.os.Handler;import android.os.IBinder;import android.os.Message;import android.os.Messenger;import android.text.format.Time;import android.widget.Toast;public class CurrentTimeService1 extends Service{public static final int CURRENT_TIME=0;private class IncomingHandler extends Handler{public void handleMessage(Message msg){if(msg.what==CURRENT_TIME){Time time = new Time();time.setToNow();String currentTime = time.format("%Y-%m-%d %H:%M:%S");Toast.makeText(CurrentTimeService1.this,currentTime,Toast.LENGTH_LONG).show();}else{super.handleMessage(msg);}}}public IBinder onBind(Intent intent){Messenger messenger = new Messenger(new IncomingHandler());return messenger.getBinder();}}
CurrentTimeActivity.java:
package com.example.demoboundservice;import android.app.Activity;import android.content.ComponentName;import android.content.Intent;import android.content.ServiceConnection;import android.os.Bundle;import android.os.IBinder;import android.os.Message;import android.os.Messenger;import android.os.RemoteException;import android.view.View;import android.widget.Button;public class CurrentTimeActivity extends Activity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);}Messenger messenger;boolean bound;protected void onStart(){super.onStart();Button button = (Button)findViewById(R.id.current_time);button.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View view) {Intent intent = new Intent(CurrentTimeActivity.this,CurrentTimeService.class);bindService(intent,connection,BIND_AUTO_CREATE);if(bound){Message message = Message.obtain(null,CurrentTimeService1.CURRENT_TIME,0,0);try{messenger.send(message);}catch(RemoteException e){e.printStackTrace();}}}});}protected void onStop(){super.onStop();if(bound){bound = false;unbindService(connection);}}private ServiceConnection connection = new ServiceConnection(){public void onServiceDisconnected(ComponentName name){messenger = null;bound = false;}public void onServiceConnected(ComponentName name,IBinder service){messenger = new Messenger(service);bound = true;}};}
AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.demoboundservice" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="19" /> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name="com.example.demoboundservice.CurrentTimeActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <service android:name="com.example.demoboundservice.CurrentTimeService1"/> </application></manifest>
0 0
- android Bound Service使用:使用Message类绑定服务
- android Bound Service使用:继续Binder类绑定服务
- Android 03 Bound Service--之被绑定的服务
- Android 开发指南(二) 服务绑定 Bound Service
- Android bound service 详解三:使用Messenger
- Android bound service 详解三:使用Messenger
- Android bound service 详解三:使用Messenger
- Android bound service 详解三:使用Messenger
- Android bound service 详解三:使用Messenger
- android - Bound Services 绑定服务
- 绑定服务——Bound Service
- Service的使用(三)绑定服务
- android - 绑定服务(bound services)
- Android bound service 详解四:service绑定及生命期
- Android bound service 详解四:service绑定及生命期
- Android bound service 详解四:service绑定及生命期
- Android bound service 详解四:service绑定及生命期
- Android服务Service详解(作用,生命周期,AIDL)系列文章--服务绑定和开启混合使用
- 小马哥-----高仿华为荣耀3c 6572芯片详细拆机 主板图 开 真假机鉴别
- Diverse Permutation - CodeForces 482 A 水题
- 树的最近公共祖先——LCA poj1330 hdu2586
- ubuntu下编译java程序
- 就从这里出发
- android Bound Service使用:使用Message类绑定服务
- 我为何停止使用Spring
- Android递归删除文件和文件夹(转载)
- ADF备忘:EL表达式调用带参数的方法(expression language invoke method with parameter)
- 找出2N+1个两两配对数中落单那个
- Leetcode: Min Stack
- 小马哥---深度解析mtk刷机平台报错解决 4032 8038等
- 【MongoDB】在windows平台下mongodb的分片集群(五)
- andriod课程第一天