android service的使用(一)
来源:互联网 发布:怎么把数据导入matlab 编辑:程序博客网 时间:2024/04/29 18:04
service是android的一个组件,相当于一个没有界面的activity,每个service都要在mainfest中进行注册,service有两种启动方式:Context.startService()和Context.bindService().
在详述这两种启动方式之前需要强调:
1.service不是一个单独的进程(可以看我的上一篇博客),如果在mainfest中不设置process属性,service会在默认进程里运行,
2.service和其他组件一样,运行在宿主进程的主线程里,service不是新的线程!!!!如果在service有耗时的工作,请另开thread,防止阻塞main thread。
有了上述的两个知识点,就可以进行servie的启动方式的学习了,由于篇幅较长,本文先讲第一种启动方式,第二种启动方式在下一篇博客中讲述。
service的第一种启动方式:
就是前面提到的Context.startService(),我们的activity,service都是一个Context(虚类),我们这里的例子以在activity中其中为例。
1 Service类的声明
和Activity一样,只不过service继承的是Service类,有几个函数可以override,我在这里声明的Servie如下:
public class MyService extends Service{String TAG = "MyService";@Override public void onCreate() { super.onCreate(); Log.d(TAG, "onCreate() executed"); } @Override public int onStartCommand(Intent intent, int flags, int startId) { Log.d(TAG, "onStartCommand() executed"); return super.onStartCommand(intent, flags, startId); } @Override public void onDestroy() { super.onDestroy(); Log.d(TAG, "onDestroy() executed"); } @Overridepublic IBinder onBind(Intent arg0) {// TODO Auto-generated method stubreturn null;}}
2 在mainfest.xml中注册
<service android:name="com.example.servicetest.MyService"> </service>
3 启动
本文将用一个activity对service进行启动,需要声明一个Intent,在activity中有两个按钮,按下start后启动service,activity的代码如下:
public class MainActivity extends Activity {Intent serviceIntent;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);serviceIntent = new Intent(this , MyService.class);Button startButton = (Button)findViewById(R.id.startbutton);startButton.setOnClickListener(new OnClickListener(){@Overridepublic void onClick(View arg0) {// TODO Auto-generated method stub//启动servicestartService(serviceIntent);}});Button stopButton = (Button)findViewById(R.id.stopbutton);stopButton.setOnClickListener(new OnClickListener(){@Overridepublic void onClick(View arg0) {// TODO Auto-generated method stub//停止servicestopService(serviceIntent);}});}@Overridepublic boolean onCreateOptionsMenu(Menu menu) {// Inflate the menu; this adds items to the action bar if it is present.getMenuInflater().inflate(R.menu.main, menu);return true;}}
布局文件如下:
<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/startbutton" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="startService"/> <Button android:id="@+id/stopbutton" android:layout_below="@id/startbutton" android:layout_marginTop="10dp" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="stopService"/></RelativeLayout>
4 启动流程
在activity中,点击startbutton后输出如下:
在activity中,点击startbutton后输出如下:
点击stopbutton后输出如下:
这其实就是对service的一个完美诠释:
在activity里调用startService()之后在service里onCreate()->onStartCommand()
而在activity里调用stopService()之后在service里onDestroy(),即service终止
后记:用这种方法启动的service和actiivty没联系,但是在默认情况下是在同一进程的同一线程中的,如果此时终止activity,service还会继续运行,但是由于service在系统中的优先级较低,在内存不足时可能会被系统收回。
0 0
- android service的使用(一)
- Android—Service的使用(一)
- Service的使用(一)
- Service的使用(一)
- Service的使用(一)
- Android使用binder访问service的方式(一)
- -----------------------------Android使用binder访问service的方式(一)
- Android使用binder访问service的方式(一)
- Android使用binder访问service的方式(一)
- Android中的Service的使用详解(一)
- Android:Service(一)——Service的基本使用
- Service的使用一
- android Service(一)
- Android Service(一)
- android Service(一)
- Android Service 的使用(二)----Service的绑定使用
- Android Service的使用
- Android Service 的使用
- 变量在编译时为其分配相应存储单元
- leetcode: Path Sum II
- linux下su与su - 切换到root权限命令的区别
- 暗示法
- 结构化P2P网络——DHT网络原理
- android service的使用(一)
- eclipse在maven下的断点debug
- HttpClient解决ntlm验证
- Java 多线程断点下载
- 虚拟网卡配置
- 正则表达式
- Visual Studio+VAssistX自动添加注释
- C语言变长数组data[0]【总结】
- 初识Struts,快速熟悉小练习