- 浏览: 325838 次
- 性别:
- 来自: 上海
文章分类
最新评论
-
xuanyuanxiaoxue:
...
Android - LayoutInflater -
柴兴博:
不错 多谢
Android 悬浮Activity并可拖动(访悬浮歌词) -
di1984HIT:
写的很好,我收藏一下。
java之动态代理模式(JDK和cglib) -
chinacssnj:
待测试,明天测,测试的结果发给大家
网络开发上传文件到服务器 -
fx_199182:
...
Android之MediaPlayer
第一步:新建一个Android工程,我这里命名为ServiceDemo.
第二步:修改main.xml代码,我这里增加了四个按钮,代码如下:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <TextView android:id="@+id/text" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/hello" /> <Button android:id="@+id/startservice" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="startService" /> <Button android:id="@+id/stopservice" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="stopService" /> <Button android:id="@+id/bindservice" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="bindService" /> <Button android:id="@+id/unbindservice" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="unbindService" /> </LinearLayout>
第三步:新建一个Service,命名为MyService.java代码如下:
package com.tutor.servicedemo; import android.app.Service; import android.content.Intent; import android.os.Binder; import android.os.IBinder; import android.text.format.Time; import android.util.Log; public class MyService extends Service { //定义个一个Tag标签 private static final String TAG = "MyService"; //这里定义吧一个Binder类,用在onBind()有方法里,这样Activity那边可以获取到 private MyBinder mBinder = new MyBinder(); @Override public IBinder onBind(Intent intent) { Log.e(TAG, "start IBinder~~~"); return mBinder; } @Override public void onCreate() { Log.e(TAG, "start onCreate~~~"); super.onCreate(); } @Override public void onStart(Intent intent, int startId) { Log.e(TAG, "start onStart~~~"); super.onStart(intent, startId); } @Override public void onDestroy() { Log.e(TAG, "start onDestroy~~~"); super.onDestroy(); } @Override public boolean onUnbind(Intent intent) { Log.e(TAG, "start onUnbind~~~"); return super.onUnbind(intent); } //这里我写了一个获取当前时间的函数,不过没有格式化就先这么着吧 public String getSystemTime(){ Time t = new Time(); t.setToNow(); return t.toString(); } public class MyBinder extends Binder{ MyService getService() { return MyService.this; } } }
第四步:修改ServiceDemo.java,代码如下:
package com.tutor.servicedemo; import android.app.Activity; import android.content.ComponentName; import android.content.Context; import android.content.Intent; import android.content.ServiceConnection; import android.os.Bundle; import android.os.IBinder; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.TextView; public class ServiceDemo extends Activity implements OnClickListener{ private MyService mMyService; private TextView mTextView; private Button startServiceButton; private Button stopServiceButton; private Button bindServiceButton; private Button unbindServiceButton; private Context mContext; //这里需要用到ServiceConnection在Context.bindService和context.unBindService()里用到 private ServiceConnection mServiceConnection = new ServiceConnection() { //当我bindService时,让TextView显示MyService里getSystemTime()方法的返回值 public void onServiceConnected(ComponentName name, IBinder service) { // TODO Auto-generated method stub mMyService = ((MyService.MyBinder)service).getService(); mTextView.setText("I am frome Service :" + mMyService.getSystemTime()); } public void onServiceDisconnected(ComponentName name) { // TODO Auto-generated method stub } }; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); setupViews(); } public void setupViews(){ mContext = ServiceDemo.this; mTextView = (TextView)findViewById(R.id.text); startServiceButton = (Button)findViewById(R.id.startservice); stopServiceButton = (Button)findViewById(R.id.stopservice); bindServiceButton = (Button)findViewById(R.id.bindservice); unbindServiceButton = (Button)findViewById(R.id.unbindservice); startServiceButton.setOnClickListener(this); stopServiceButton.setOnClickListener(this); bindServiceButton.setOnClickListener(this); unbindServiceButton.setOnClickListener(this); } public void onClick(View v) { // TODO Auto-generated method stub if(v == startServiceButton){ Intent i = new Intent(); i.setClass(ServiceDemo.this, MyService.class); mContext.startService(i); }else if(v == stopServiceButton){ Intent i = new Intent(); i.setClass(ServiceDemo.this, MyService.class); mContext.stopService(i); }else if(v == bindServiceButton){ Intent i = new Intent(); i.setClass(ServiceDemo.this, MyService.class); mContext.bindService(i, mServiceConnection, BIND_AUTO_CREATE); }else{ mContext.unbindService(mServiceConnection); } } }
第五步:修改AndroidManifest.xml代码(将我们新建的MyService注册进去如下代码第14行:)
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.tutor.servicedemo" android:versionCode="1" android:versionName="1.0"> <application android:icon="@drawable/icon" android:label="@string/app_name"> <activity android:name=".ServiceDemo" 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=".MyService" android:exported="true"></service> </application> <uses-sdk android:minSdkVersion="7" /> </manifest>
第六步:执行上述工程,效果图如下:
点击startServie按钮时先后执行了Service中onCreate()->onStart()这两个方法,打开Logcat视窗效果如下图:
我们这时可以按HOME键进入Settings(设置)->Applications(应用)->Running Services(正在运行的服务)看一下我们新启动了一个服务,效果如下:
点击stopService按钮时,Service则执行了onDestroy()方法,效果图如下所示:
这时候我们再次点击startService按钮,然后点击bindService按钮(通常bindService都是bind已经启动的Service),我们看一下Service执行了IBinder()方法,以及TextView的值也有所变化了,如下两张图所示:
最后点击unbindService按钮,则Service执行了onUnbind()方法,如下图所示:
发表评论
-
Android中AsyncTask的简单用法
2012-01-13 16:00 1128在开发Android移动客户端的时候往往要使用多线程来进行操 ... -
Android应用的自动升级、更新模块的实现 .
2011-11-16 14:01 650http://www.eoeandroid.com/threa ... -
一个APK反编译利器Apktool
2011-11-16 13:54 1560一个APK反编译利器Apktool APK 本地化 ... -
自定义Android标题栏TitleBar布局
2011-11-14 14:13 1220很多网友发现自己Android程序的标题栏TitleBar区域 ... -
Android GPS获取地理位置 .
2011-11-14 14:11 830import android.app.Activity; i ... -
android ListView详解
2011-11-14 13:48 1026在android开发中ListView是比较常用的组件,它以列 ... -
Android之Content provider 详解
2011-11-14 13:35 2438Android是如何实现应用程序之间数据共享的?一个应用程序可 ... -
Android源码地址
2011-11-12 19:14 1028http://blog.csdn.net/ilittleone ... -
android之File
2011-11-11 22:39 19721:Fileservice package cn.itcas ... -
Android知识补漏
2011-11-09 22:33 01:AndroidManifiest.xml < ... -
深入剖析Android消息机制
2011-11-09 14:13 938在Android中,线程内部或者线程之间进行信息交互时经常会使 ... -
Android之Handler详解(四)
2011-11-09 14:00 1265d、自己创建新的线程,然后在新线程中创建Looper,主线程调 ... -
Android之Handler详解(三)
2011-11-09 13:58 1352c、将消息队列绑定到子线程上,主线程只管通过Handl ... -
Android之Handler详解(二)
2011-11-09 13:54 1638二:sendMessage版本的Handl ... -
Android之Handler详解(一)
2011-11-09 13:22 2270一个Handler允许你发送和处理消息(Message)以及 ... -
关于StartActivityForResult方法的使用
2011-10-31 17:11 1061根据方法名可知 这个方法是要得到启动后的Activity返回的 ... -
Android 悬浮Activity并可拖动(访悬浮歌词)
2011-10-24 16:23 2059天天动听, 这款Android手机上的音乐播放器,相信不少朋友 ... -
Android GWES
2011-10-24 16:13 1162第八章 Android GWES 8.1 View Syst ... -
Android系统服务-WindowManager
2011-10-24 16:10 1418WindowManager是Android中一个重要的服务 ... -
http通信
2011-10-15 17:31 1070HTTP(HyperText Transfer Proto ...
相关推荐
android intent service 实例android intent service 实例android intent service 实例
android service 简单实例代码 . binder继承一个接口,实现方法。
android service实例
关于Android Service的简单实例:属于start service类型,而不是bound service。
Android Service实例 Android Service 一般处理比较耗时的操作
Android service 开发实例。欢迎大家下载学习
实现了android的远程service调用哪个,使用了AIDL,并且提供了一个自定义类型的参数
NULL 博文链接:https://chaowen0668.iteye.com/blog/1090096
有四个小实例 分别是安卓 service的 有启动方式的 绑定方式的 线程方式的 aidl方式的 看了这四个应该更加容易的懂了
android service 实例
Android 的 Remote Service 开发实例RemoteService
Android创建服务实例图文实测,基于Eclipse, Android7, target=android-24 build-tools=25.0.2
android service的实例程序,用于博文http://blog.csdn.net/xianming01/article/details/7341877
Android 的 Remote Service 开发实例RemoteServiceClient~
public class ServiceList extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); TextView tv = new TextView(this); ActivityManager...
NULL 博文链接:https://javajeye.iteye.com/blog/803007
主要介绍了Android开发之Service用法,实例分析了Android中Service的功能及使用技巧,需要的朋友可以参考下
Android Service AIDL实例
博文android基础知识05:四大组件之service 02:远程调用中关于远程调用service的实例程序
android 开发实例 1、 图片浏览器(Picture Viewer) 2 、文件浏览器(File Browser) 3 、通讯录(Contact) 4 、任务管理器(Task Manager) 5 、音乐播放器(Rock Player) 6 、天气预报(Android Weather ...