新建一个用于service的class,Superclass属性设置为Service-android.app, 命名为Pservice在其中复写方法,代码如下:
@Override public void onCreate() { // TODO Auto-generated method stub Log.i(TAG, "EXampleService--->onCreate"); super.onCreate(); } @Override public void onDestroy() { // TODO Auto-generated method stub Log.i(TAG, "EXampleService--->onDestroy"); super.onDestroy(); } @Override public int onStartCommand(Intent intent, int flags, int startId) { // TODO Auto-generated method stub Log.i(TAG, "EXampleService--->onStartCommand"); return super.onStartCommand(intent, flags, startId); }
在MainActivity.java中通过write如下监听器来测试service:
private OnClickListener listener = new OnClickListener() { public void onClick(View v) { Intent intent = new Intent(MainActivity.this,Pservice.class); switch(v.getId()) { case R.id.btnStartService: startService(intent); break; case R.id.btnStopService: stopService(intent); break; default: break; } } };
但需要在Manifest中加入:<service android:name=".Pservice"></service> 这行代码与Activity同级。
Binder service:
Binder service:允许其他组件(如Activity)绑定到这个service上,可以发送请求,也可以接受请求,甚至可以进行进程间的通话。Binder service仅仅在服务于其他组件时存在,不能独自无限期的在后台运行。
创建一个servide类在里面复写onBind,代码如下:
public class BoundService extends Service { private static final String TAG = "BoundService"; private MyBinder binder = new MyBinder(); private class MyBinder extends Binder { public BoundService getService() { return BoundService.this; } } @Override public IBinder onBind(Intent arg0) { // TODO Auto-generated method stub return binder; } public void MyMethod() { Log.i(TAG, "MyMethod()"); } }
创建一个BinderActivity扩展自Activity,代码如下:
package com.example.pmusicplayer; import android.os.Bundle; import android.os.IBinder; import android.app.Activity; import android.content.ComponentName; import android.content.Context; import android.content.Intent; import android.content.ServiceConnection; import android.view.Menu; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; public class BinderActivity extends Activity { private Button btnStartBinderService; private Button btnStopBinderService; private boolean isConnected = false; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.binder); btnStartBinderService = (Button)findViewById(R.id.btnStartBinderService); btnStopBinderService = (Button)findViewById(R.id.btnStopBinderService); btnStartBinderService.setOnClickListener(listener); btnStopBinderService.setOnClickListener(listener); } private OnClickListener listener = new OnClickListener() { public void onClick(View v) { switch(v.getId()) { case R.id.btnStartBinderService: bindService(); break; case R.id.btnStopBinderService: unBind(); break; default: break; } } }; private void unBind() { if(isConnected) { unbindService(conn); } } private void bindService() { Intent intent = new Intent(BinderActivity.this, BoundService.class); bindService(intent, conn, Context.BIND_AUTO_CREATE); } private ServiceConnection conn = new ServiceConnection() { @Override public void onServiceDisconnected(ComponentName name) { // TODO Auto-generated method stub isConnected = false; } @Override public void onServiceConnected(ComponentName name, IBinder binder) { // TODO Auto-generated method stub BoundService service=null; service.MyMethod(); isConnected = true; } }; @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.binder, menu); return true; } }