android services

来源:互联网 发布:阿里云栖大会2016 编辑:程序博客网 时间:2024/06/07 04:45

关于service首先要明白如下两个方面

service和activity类似,但是有些不同点。service是不可见的。并且优先级高于activity,也就是说当内存不够用等情况出现的时候先杀死activity,随后是service。

service主要分为两类:1.不绑定数据源 2.绑定数据源的方式

类似activity要明白其生命周期,见图


下面写一个小程序来演示两种service的生命周期,并对其的使用简单说明。

首先是mainactivity类

package com.example.myservice;import com.example.myservice.MyBindService.MyBinder;import android.app.Activity;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.view.Menu;import android.view.MenuItem;import android.view.View;import android.view.View.OnClickListener;public class MainActivity extends Activity {Intent intent1;Intent intent2;MyBindService service;ServiceConnection conn = new ServiceConnection() {@Override//当服务跟启动源断开的时候 会自动回调public void onServiceDisconnected(ComponentName name) {// TODO Auto-generated method stub}@Override//当服务跟启动源连接的时候 会自动回调public void onServiceConnected(ComponentName name, IBinder binder) {// TODO Auto-generated method stubservice = ((MyBinder)binder).getService();}};@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);}public void doClick(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);startService(intent2);bindService(intent2, conn, Service.BIND_AUTO_CREATE);break;case R.id.unbindService:unbindService(conn);break;default:break;}}protected void onDestroy() {// TODO Auto-generated method stubstopService(intent2);unbindService(conn);super.onDestroy();}@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;}@Overridepublic boolean onOptionsItemSelected(MenuItem item) {// Handle action bar item clicks here. The action bar will// automatically handle clicks on the Home/Up button, so long// as you specify a parent activity in AndroidManifest.xml.int id = item.getItemId();if (id == R.id.action_settings) {return true;}return super.onOptionsItemSelected(item);}}

其次是MyBindeService类

package com.example.myservice;import android.app.Service;import android.content.Intent;import android.content.ServiceConnection;import android.os.Binder;import android.os.IBinder;import android.util.Log;public class MyBindService extends Service {@Overridepublic boolean bindService(Intent service, ServiceConnection conn, int flags) {// TODO Auto-generated method stubLog.i("info", "Bind--Service");return super.bindService(service, conn, flags);}public class MyBinder extends Binder{public MyBindService getService(){return MyBindService.this;}}@Overridepublic void unbindService(ServiceConnection conn) {// TODO Auto-generated method stubLog.i("info", "Bind--unBindService");super.unbindService(conn);}@Overridepublic void onCreate() {// TODO Auto-generated method stubLog.i("info", "Bind--onCreate");super.onCreate();}@Overridepublic IBinder onBind(Intent intent) {// TODO Auto-generated method stubreturn new MyBinder();}public void Play(){Log.i("info", "播放");}public void Pause(){Log.i("info", "暂停");}public void Pervious(){Log.i("info", "上一首");}public void next(){Log.i("info", "下一首");}}

然后是MystartService类

package com.example.myservice;import android.app.Service;import android.content.Intent;import android.os.IBinder;import android.util.Log;public class MyStartService extends Service{@Overridepublic void onCreate() {// TODO Auto-generated method stubLog.i("info", "start--onCreate");super.onCreate();}@Overridepublic void onDestroy() {// TODO Auto-generated method stubLog.i("info", "start--onDestroy");super.onDestroy();}@Overridepublic int onStartCommand(Intent intent, int flags, int startId) {// TODO Auto-generated method stubLog.i("info", "onStartCommand");System.out.println("123456");return super.onStartCommand(intent, flags, startId);}@Overridepublic IBinder onBind(Intent intent) {// TODO Auto-generated method stubLog.i("info", "start--onBind");return null;}}

之后是xml布局文件

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:id="@+id/LinearLayout1"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical"    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="com.example.myservice.MainActivity" >    <TextView        android:id="@+id/textView1"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:text="StartService" />    <Button        android:id="@+id/startService"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:onClick="doClick"        android:text="StartService" />    <Button        android:id="@+id/stopService"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:onClick="doClick"        android:text="StopService" />    <TextView        android:id="@+id/textView2"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:text="BindService" />    <Button        android:id="@+id/bindService"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:onClick="doClick"        android:text="bindService" />    <Button        android:id="@+id/unbindService"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:onClick="doClick"        android:text="unbindService" />    <Button        android:id="@+id/previous"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:onClick="doClick"        android:text="上一首" />    <Button        android:id="@+id/next"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:onClick="doClick"        android:text="下一首" />    <Button        android:id="@+id/play"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:onClick="doClick"        android:text="播放" />    <Button        android:id="@+id/stop"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:onClick="doClick"        android:text="停止" /></LinearLayout>
AndroidMainfest文件需要添加的内容

<service android:name="com.example.myservice.MyBindService"></service><service android:name="com.example.myservice.MyStartService"></service>


0 0