android Bound Service使用:继续Binder类绑定服务
2014-11-10 23:12
495 查看
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 Bound Service使用:继续Binder类绑定服务
- android Bound Service使用:使用Message类绑定服务
- android Bound Service使用:使用Message类绑定服务
- Android Service详解(六)---绑定服务BoundService详解之AIDL的自定义属性使用
- Android Service详解(四)---绑定服务BoundService详解之扩展Binder类
- Android 03 Bound Service--之被绑定的服务
- Android服务Service详解(作用,生命周期,AIDL)系列文章--服务绑定和开启混合使用
- Android应用程序绑定服务(bindService)的过程源代码分析
- Android Service---创建绑定类型服务
- Android中获取正在运行的服务-------ActivityManager.RunningServiceInfo的使用
- Android bound service 详解三:使用Messenger
- Android Service---创建绑定类型服务
- Android 绑定类型服务---使用信使(Messenger)
- Android使用binder访问service的方式(一)
- android AIDL绑定服务使用
- Android中获取正在运行的服务-------ActivityManager.RunningServiceInfo的使用
- Android中获取正在运行的服务-------ActivityManager.RunningServiceInfo的使用
- Android中获取正在运行的服务-------ActivityManager.RunningServiceInfo的使用
- Android 绑定类型服务---继承Binder类
- Android之使用bindService启动服务