您的位置:首页 > 移动开发 > Android开发

android四大组件之Service详解

2015-09-17 11:36 507 查看
今天学习了android四大组件中的Service,Service使用步骤基本上就是注册、启动、停止这三个步骤,今天我就拿自己做的一个demo来回顾一下学习的心得体会

1、首先,看一下demo运行的界面

布局文件的代码如下

<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" >

<Button
android:id="@+id/startService"
android:onClick="myclick"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:text="startService" />

<Button
android:id="@+id/stopService"
android:onClick="myclick"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_below="@+id/startService"
android:text="stopService" />

<Button
android:id="@+id/bindService"
android:onClick="myclick"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_below="@+id/stopService"
android:text="bindService" />

<Button
android:id="@+id/unbindService"
android:onClick="myclick"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_below="@+id/bindService"
android:text="unbindService" />

<Button
android:id="@+id/play"
android:onClick="myclick"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_below="@+id/unbindService"
android:text="play" />

<Button
android:id="@+id/pause"
android:onClick="myclick"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_below="@+id/play"
android:text="pause" />

<Button
android:id="@+id/next"
android:onClick="myclick"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_below="@+id/pause"
android:text="next" />

<Button
android:id="@+id/pervious"
android:onClick="myclick"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_below="@+id/next"
android:text="pervious" />

</RelativeLayout>


关于Service的启动方式,主要有两种,一种是直接使用startService();另一种是使用bindService()

我们先来看一下使用startService()方法来启动一个服务

想要启动一个服务,首先要先创建一个服务,写一个类然后继承Service,实现里面几个必要的方法,代码如下

package com.mytest.servicetest;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;

public class Mystartservice extends Service {

@Override
public IBinder onBind(Intent intent) {
System.out.println("Mystartservice------onBind");
return null;
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
System.out.println("Mystartservice------onStartCommand");
return super.onStartCommand(intent, flags, startId);
}
@Override
public void onCreate() {
System.out.println("Mystartservice------onCreate");
super.onCreate();
}

@Override
public void onDestroy() {
System.out.println("Mystartservice------onDestroy");
super.onDestroy();
}

}


写完服务之后我们需要在AndroidManifest.xml中通过service 标签注册我们的服务
<service android:name="com.mytest.servicetest.Mystartservice"></service>然后我们就通过
<pre name="code" class="java">Intent <span style="font-family: Arial, Helvetica, sans-serif;">intent1=new Intent(MainActivity.this,Mystartservice.class);</span>
startService(intent1);

每次启动一个服务,
onCreate()方法只会执行一次,之后再启动服务,都会只调用onStartCommand();可以使用<span style="font-family: Arial, Helvetica, sans-serif;">stopService()来销毁一个服务</span>


如果采用的是bindService()方法开启一个服务,可能方法会比较复杂一点, 但是bindService()可以支持数据的回传

使用bindService()方法, 需要使用bindService(intent2, connection,Service.BIND_AUTO_CREATE);
这里的三个参数,第一个代表了你要开的服务,第二个代表了ServiceConnection如果没有的话可以传null,第三个代表了自动创建服务

如果要销毁一个bindService()开启的服务,需要取消绑定,使用unbindService(connection);这里的参数和上面的参数一样的,这样就可以开启一个服务

这里是Mainacivity.java的代码

package com.mytest.servicetest;

import com.mytest.servicetest.MyBindService.mybind;

import android.app.Service;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.support.v7.app.ActionBarActivity;
import android.view.View;

public class MainActivity extends ActionBarActivity {

Intent intent1;
Intent intent2;
MyBindService myBindService;
ServiceConnection connection=new ServiceConnection() {

@Override
public void onServiceDisconnected(ComponentName name) {
// TODO Auto-generated method stub

}

@Override
public void onServiceConnected(ComponentName name, IBinder service) {
// TODO Auto-generated method stub
myBindService=((mybind)service).getservice();
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}

public void myclick(View v) {
switch (v.getId()) {
case R.id.startService:
intent1=new Intent(MainActivity.this,Mystartservice.class);
startService(intent1);
break;
case R.id.stopService:
stopService(intent1);
break;
case R.id.bindService:
intent2=new Intent(MainActivity.this,MyBindService.class);
bindService(intent2, connection,Service.BIND_AUTO_CREATE);
break;
case R.id.unbindService:
unbindService(connection);
break;
case R.id.play:
myBindService.play();
break;
case R.id.pause:
myBindService.pause();
break;
case R.id.next:
myBindService.next();
break;
case R.id.pervious:
myBindService.pervious();
break;
}
}
}

注:

当一个服务开启之后,需要我们手动去关闭,如果不关闭,服务将会一直运行在后台
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  android Service 服务