android Bound Service使用:继续Binder类绑定服务
2015-12-14 00:00
766 查看
activity_main.xml:
CurrentTimeService.java:
MainActivity.java:
AndroidManifest.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>
CurrentTimeService.java:
package com.example.demoboundservice; import android.app.Service; import android.content.Intent; import android.os.Binder; import android.os.IBinder; import android.text.format.Time; public class CurrentTimeService extends Service{ private final IBinder binder = new LocalBinder(); public class LocalBinder extends Binder{ CurrentTimeService getService(){ return CurrentTimeService.this; } } public IBinder onBind(Intent intent){ return binder; } public String getCurrentTime(){ Time time = new Time(); time.setToNow(); String currentTime = time.format("%Y-%m-%d %H:%M:%S"); return currentTime; } }
MainActivity.java:
package com.example.demoboundservice; import com.example.demoboundservice.CurrentTimeService.LocalBinder; 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.view.Menu; import android.view.View; import android.widget.Button; import android.widget.Toast; public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } CurrentTimeService cts ; boolean bound; protected void onStart(){ super.onStart(); Button button = (Button)findViewById(R.id.current_time); button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Intent intent = new Intent(MainActivity.this,CurrentTimeService.class); bindService(intent,sc,BIND_AUTO_CREATE); if(bound){ Toast.makeText(MainActivity.this, cts.getCurrentTime(), Toast.LENGTH_LONG).show(); } } }); } protected void onStop(){ super.onStop(); if(bound){ bound = false; unbindService(sc); } } private ServiceConnection sc = new ServiceConnection(){ public void onServiceDisconnected(ComponentName name){ bound = false; } public void onServiceConnected(ComponentName name,IBinder service){ LocalBinder binder =(LocalBinder)service; cts = binder.getService(); 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.MainActivity" 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.CurrentTimeService"/> </application> </manifest>
相关文章推荐
- android GridView视图的案例
- android 开发LayoutInflater之我见
- android Menu 选项菜单示例
- android ContentProvider 使用实例
- android Notification 状态栏通知使用示例
- android 备忘录之简单实现
- android Toast 消息提示 例子
- android gallery 画廊控件示例
- android 发送e-mail实例
- android Dialog对话框使用示例
- listItem.measure(0, 0) 空指针问题
- android百度地图入门中碰到的2个难题
- AndroidStudio快捷键
- Android 自定义listview滑到顶部标题固定 轻松实现360软件详情页效果
- Android中的进程与线程
- 喝酒不骑马的Android自学日记(5)
- Android--Binder驱动浅析
- Android开源项目之音乐播放器:HideMusicPlayer
- 智能监控平台Android客户端
- Android消息机制源码解析(一)——消息的载体Message