标签:
效果图
MainActivity.java
1 package com.wangzhen.servicedemo; 2 3 import com.lidroid.xutils.ViewUtils; 4 import com.lidroid.xutils.view.annotation.ContentView; 5 import com.lidroid.xutils.view.annotation.ViewInject; 6 import com.lidroid.xutils.view.annotation.event.OnClick; 7 import com.wangzhen.service.MyBindService; 8 import com.wangzhen.service.MyBindService.MyBinder; 9 import com.wangzhen.service.MyService; 10 import com.wangzhen.servicedemo.R; 11 12 import android.support.v7.app.ActionBarActivity; 13 import android.view.View; 14 import android.widget.Button; 15 import android.widget.Toast; 16 import android.content.ComponentName; 17 import android.content.Context; 18 import android.content.Intent; 19 import android.content.ServiceConnection; 20 import android.os.Bundle; 21 import android.os.IBinder; 22 23 @ContentView(R.layout.activity_main) 24 public class MainActivity extends ActionBarActivity { 25 26 Context mContext; 27 28 @ViewInject(R.id.Button_StartService) 29 private Button Button_StartService; 30 @ViewInject(R.id.Button_StopService) 31 private Button Button_StopService; 32 @ViewInject(R.id.Button_BindService) 33 private Button Button_BindService; 34 @ViewInject(R.id.Button_unBindService) 35 private Button Button_unBindService; 36 37 private MyBindService myBindService; 38 39 @Override 40 protected void onCreate(Bundle savedInstanceState) { 41 super.onCreate(savedInstanceState); 42 mContext = this; 43 ViewUtils.inject(this); 44 } 45 46 /** 47 * 创建ServiceConnection对象 48 */ 49 private ServiceConnection connection = new ServiceConnection() { 50 51 @Override 52 public void onServiceDisconnected(ComponentName name) { 53 myBindService = null; 54 Toast.makeText(mContext, "服务已解除绑定", 2000).show(); 55 } 56 57 @Override 58 public void onServiceConnected(ComponentName name, IBinder service) { 59 myBindService = ((MyBinder) service).getService(); 60 Toast.makeText(mContext, "服务绑定成功,返回值:" + myBindService.getName(), 61 2000).show(); 62 } 63 }; 64 65 @OnClick({ R.id.Button_StartService, R.id.Button_StopService, 66 R.id.Button_BindService, R.id.Button_unBindService }) 67 private void OnButtonClick(View view) { 68 Intent intent = new Intent(MainActivity.this, MyService.class); 69 Intent intent_bind = new Intent(mContext, MyBindService.class); 70 switch (view.getId()) { 71 case R.id.Button_StartService: 72 startService(intent); 73 break; 74 case R.id.Button_StopService: 75 stopService(intent); 76 break; 77 case R.id.Button_BindService: 78 bindService(intent_bind, connection, Context.BIND_AUTO_CREATE); 79 break; 80 case R.id.Button_unBindService: 81 unbindService(connection); 82 break; 83 84 default: 85 break; 86 } 87 } 88 }
MyService.java
1 package com.wangzhen.service; 2 3 import java.util.Timer; 4 import java.util.TimerTask; 5 6 import com.wangzhen.servicedemo.MainActivity; 7 import com.wangzhen.servicedemo.R; 8 9 import android.annotation.SuppressLint; 10 import android.app.Notification; 11 import android.app.NotificationManager; 12 import android.app.PendingIntent; 13 import android.app.Service; 14 import android.content.Context; 15 import android.content.Intent; 16 import android.os.Handler; 17 import android.os.IBinder; 18 import android.os.Message; 19 import android.util.Log; 20 21 /** 22 * 23 * @author XX 24 * @since 2015年7月10日 10:25:56 25 */ 26 public class MyService extends Service { 27 private Context mContext; 28 private static final int FLAG_SUCCESS = 0; 29 private static final int FLAG_NUMBER = 1; 30 private String TAG = "SERVICE"; 31 Timer timer; 32 private int Count = 0; 33 34 @Override 35 public void onCreate() { 36 super.onCreate(); 37 mContext = getApplicationContext(); 38 } 39 40 @Override 41 public int onStartCommand(Intent intent, int flags, int startId) { 42 flags = START_STICKY; 43 timer = new Timer(); 44 timer.schedule(new TimerTask() { 45 46 @Override 47 public void run() { 48 // TODO Auto-generated method stub 49 Message msg = Message.obtain(); 50 msg.what = FLAG_NUMBER; 51 msg.obj = Count; 52 mHandler.sendMessage(msg); 53 Count++; 54 } 55 }, 0, 1000); 56 57 new Thread() { 58 @Override 59 public void run() { 60 super.run(); 61 try { 62 Thread.sleep(1000); 63 Message message = Message.obtain(); 64 message.what = FLAG_SUCCESS; 65 mHandler.sendMessage(message); 66 } catch (InterruptedException e) { 67 e.printStackTrace(); 68 } 69 } 70 71 }.start(); 72 return super.onStartCommand(intent, flags, startId); 73 } 74 75 @SuppressLint("HandlerLeak") 76 Handler mHandler = new Handler() { 77 @SuppressLint("ShowToast") 78 public void handleMessage(Message msg) { 79 switch (msg.what) { 80 case FLAG_SUCCESS: 81 SendNotification("Service已启动"); 82 break; 83 case FLAG_NUMBER: 84 Log.i(TAG, msg.obj.toString()); 85 break; 86 87 default: 88 break; 89 } 90 } 91 }; 92 93 /** 94 * 发送通知 95 * 96 * @param message 97 */ 98 @SuppressWarnings("deprecation") 99 @SuppressLint("NewApi") 100 private void SendNotification(String message) { 101 // 点击之后执行的Intent 102 Intent intent = new Intent(mContext, MainActivity.class); 103 PendingIntent pendingIntent = PendingIntent.getActivity(mContext, 0, 104 intent, 0); 105 Notification notification = new Notification(); 106 notification.icon = R.drawable.ic_launcher; 107 notification.tickerText = "通知"; 108 notification.when = System.currentTimeMillis(); 109 notification.defaults = Notification.DEFAULT_SOUND 110 | Notification.DEFAULT_VIBRATE;// 设置默认为系统声音 111 notification.flags = Notification.FLAG_AUTO_CANCEL;// 点击后自动消失 112 notification.setLatestEventInfo(mContext, "通知", message, pendingIntent); 113 114 NotificationManager mManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); 115 mManager.notify(1, notification); 116 117 } 118 119 @Override 120 public void onDestroy() { 121 // TODO Auto-generated method stub 122 super.onDestroy(); 123 timer.cancel(); 124 } 125 126 @Override 127 public IBinder onBind(Intent arg0) { 128 // TODO Auto-generated method stub 129 return null; 130 } 131 132 }
MyBindService.java
1 package com.wangzhen.service; 2 3 import android.app.Service; 4 import android.content.Intent; 5 import android.os.Binder; 6 import android.os.IBinder; 7 8 public class MyBindService extends Service { 9 10 public class MyBinder extends Binder { 11 /** 12 * 返回当前Service的实例 13 * 14 * @return 当前Service的实例 15 */ 16 public MyBindService getService() { 17 return MyBindService.this; 18 } 19 } 20 21 public String getName() { 22 return "你好"; 23 } 24 25 @Override 26 public IBinder onBind(Intent arg0) { 27 // TODO Auto-generated method stub 28 return new MyBinder(); 29 } 30 31 @Override 32 public boolean onUnbind(Intent intent) { 33 // TODO Auto-generated method stub 34 return super.onUnbind(intent); 35 } 36 37 }
标签:
原文地址:http://www.cnblogs.com/lavalike/p/4637716.html