直接上代码
类1
package com.iceskysl.TestServiceHolder;import android.app.Notification;import android.app.NotificationManager;import android.app.PendingIntent;import android.app.Service;import android.content.Intent;import android.os.Binder;import android.os.IBinder;import android.util.Log;public class TestService extends Service {private static final String TAG = "TestService";private NotificationManager _nm;@Overridepublic IBinder onBind(Intent i) {Log.e(TAG, "============> TestService.onBind");return null;}public class LocalBinder extends Binder {TestService getService() {return TestService.this;}}@Overridepublic boolean onUnbind(Intent i) {Log.e(TAG, "============> TestService.onUnbind");return false;}@Overridepublic void onRebind(Intent i) {Log.e(TAG, "============> TestService.onRebind");}@Overridepublic void onCreate() {Log.e(TAG, "============> TestService.onCreate");_nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);showNotification();}@Overridepublic void onStart(Intent intent, int startId) {Log.e(TAG, "============> TestService.onStart");}@Overridepublic void onDestroy() {_nm.cancel(R.string.service_started);Log.e(TAG, "============> TestService.onDestroy");}private void showNotification() {Notification notification = new Notification(R.drawable.face_1,"Service started", System.currentTimeMillis());PendingIntent contentIntent = PendingIntent.getActivity(this, 0,new Intent(this, TestServiceHolder.class), 0);// must set this for content view, or will throw a exceptionnotification.setLatestEventInfo(this, "Test Service","Service started", contentIntent);_nm.notify(R.string.service_started, notification);}}
类2
package com.iceskysl.TestServiceHolder;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.Toast;public class TestServiceHolder extends Activity {private boolean _isBound;private TestService _boundService;/** Called when the activity is first created. */@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.main);setTitle("Service Test");initButtons();}  private ServiceConnection _connection = new ServiceConnection() {     public void onServiceConnected(ComponentName className, IBinder service) {           _boundService = ((TestService.LocalBinder)service).getService();             Toast.makeText(TestServiceHolder.this, "Service connected",           Toast.LENGTH_SHORT).show();     }     public void onServiceDisconnected(ComponentName className) {       // unexpectedly disconnected,we should never see this happen.       _boundService = null;       Toast.makeText(TestServiceHolder.this, "Service connected",           Toast.LENGTH_SHORT).show();     }   };    private void initButtons() {     Button buttonStart = (Button) findViewById(R.id.start_service);     buttonStart.setOnClickListener(new OnClickListener() {       public void onClick(View arg0) {         startService();       }     });     Button buttonStop = (Button) findViewById(R.id.stop_service);     buttonStop.setOnClickListener(new OnClickListener() {       public void onClick(View arg0) {         stopService();       }     });     Button buttonBind = (Button) findViewById(R.id.bind_service);     buttonBind.setOnClickListener(new OnClickListener() {       public void onClick(View arg0) {         bindService();       }     });     Button buttonUnbind = (Button) findViewById(R.id.unbind_service);     buttonUnbind.setOnClickListener(new OnClickListener() {       public void onClick(View arg0) {         unbindService();       }     });   }   private void startService() {     Intent i = new Intent(this, TestService.class);     this.startService(i);      }    private void stopService() {     Intent i = new Intent(this, TestService.class);     this.stopService(i);   }   private void bindService() {     Intent i = new Intent(this, TestService.class);     bindService(i, _connection, Context.BIND_AUTO_CREATE);     _isBound = true;   }   private void unbindService() {     if (_isBound) {       unbindService(_connection);       _isBound = false;     }   } }

更多相关文章

  1. Android(安卓)补间动画
  2. android教你怎么轻松实现手机推送功能,一步一步教你Mob+MobPush实
  3. 横竖屏优化
  4. Android(安卓)7.0解析包时出现问题 的解决方案(应用内更新)
  5. android 自定义换件--数字图象时钟控件
  6. Android业务层代码结构扩展 - from MVP to MVPplus
  7. android:自定义圆环形百分比控件,代码简单,加载动画,代码详细说明
  8. 使用include和merge 标签-转自网络
  9. 浅谈android Toast五种样式 (让你的Toast更炫)

随机推荐

  1. android 权限permission 说明
  2. Android系统(168)---Android(安卓)开源项目
  3. android中ListView数据刷新时的同步方法
  4. Android(安卓)okhttp3 进行socket connec
  5. httpget请求在android4.4系统出现java.ne
  6. android支付宝问题2013-07-17
  7. iOS和Android对比系列(二):页面跳转和传参
  8. 签名keystore一系列
  9. AArch64 是什么
  10. Android设备指纹大全