标签:
绑定服务 服务中通过定义Binder对象的子类让这个子类成为桥梁 在onBind()中返回子类对象
这样就可以在activity中调用这个子类的方法
在Activity中通过ServiceConnection获取这个对象并向下转型为该子类对象 y与Activity绑定的服务当Activity结束的时候服务也会跟着结束
timer.cancel()会结束timerTask中的所有任务
NotifyManager.cancel(2) 2是对应的通知的id 会结束对应的通知
1 import android.app.Activity; 2 import android.content.ComponentName; 3 import android.content.Intent; 4 import android.content.ServiceConnection; 5 import android.os.Bundle; 6 import android.os.IBinder; 7 import android.view.View; 8 9 import com.qf.service03_bindservice.TimerService.TimerBinder; 10 11 public class MainActivity extends Activity { 12 13 //澹版槑Binder瀛愮被瀵硅薄 14 TimerBinder timerBinder; 15 16 //3. 瀹炰緥鍖朣erviceConnection鎺ュ彛锛堢粦瀹氭湇鍔$粍浠舵椂浣跨敤鐨勫洖璋冩帴鍙o級 17 ServiceConnection conn=new ServiceConnection(){ 18 @Override 19 public void onServiceConnected(ComponentName name, IBinder service) { 20 // TODO 缁戝畾鎴愬姛鐨勬柟娉? 21 timerBinder=(TimerBinder) service; 22 } 23 24 @Override 25 public void onServiceDisconnected(ComponentName name) { 26 // TODO 涓庣粦瀹氭湇鍔$粍浠舵柇寮?繛鎺ワ紙鍙戠敓鐨勬椂鏈猴細鐢变簬绯荤粺鍘熷洜閫犳垚浜嗘湇鍔$粍浠跺仠姝㈡垨閿?瘉锛? timerBinder=null; 27 } 28 }; 29 30 @Override 31 protected void onCreate(Bundle savedInstanceState) { 32 super.onCreate(savedInstanceState); 33 setContentView(R.layout.activity_main); 34 } 35 36 public void bindService(View v) { 37 //缁戝畾鏈嶅姟缁勪欢 38 bindService(new Intent(getApplicationContext(),TimerService.class), 39 conn, BIND_AUTO_CREATE); 40 41 //BIND_AUTO_CREATE鏍囪瘑琛ㄧず锛氱粦瀹氱殑鏈嶅姟缁勪欢濡傛灉涓嶅瓨锛屽垯浼氳嚜鍔ㄥ垱寤猴紝 42 //娉細鐢眀indService鏂瑰紡鍚姩鐨凷ervice锛屽叾鐢熷懡鍛ㄦ湡浼氬彈鍒扮粦瀹氱粍浠剁殑褰卞搷锛屽嵆褰撶粦瀹氱粍浠禔ctivity閿?瘉鏃讹紝Service涔熶細鍋滄 43 } 44 45 public void unbindService(View v) { 46 unbindService(conn); //瑙i櫎缁戝畾 47 } 48 49 public void startTime(View v) { 50 if(timerBinder!=null){ 51 timerBinder.start(); 52 } 53 } 54 55 public void stopTime(View v) { 56 if(timerBinder!=null){ 57 timerBinder.stop(); 58 } 59 } 60 61 }
1 import java.util.Timer; 2 import java.util.TimerTask; 3 4 import android.app.Notification; 5 import android.app.NotificationManager; 6 import android.app.PendingIntent; 7 import android.app.Service; 8 import android.content.Intent; 9 import android.os.Binder; 10 import android.os.IBinder; 11 import android.support.v4.app.NotificationCompat; 12 import android.util.Log; 13 14 public class TimerService extends Service { 15 16 private Timer timer; //瀹氭椂鍣? 17 private NotificationManager notifyMgr; 18 @Override 19 public void onCreate() { 20 super.onCreate(); 21 Log.i("debug", "--onCreate--"); 22 23 timer=new Timer(); 24 25 notifyMgr=(NotificationManager) getSystemService(NOTIFICATION_SERVICE); 26 } 27 28 @Override 29 public IBinder onBind(Intent intent) { 30 Log.i("debug", "--onBind--"); 31 return new TimerBinder();//2. 瀹炰緥鍖朆inder鐨勫瓙绫伙紝骞惰繑鍥? 32 } 33 34 @Override 35 public boolean onUnbind(Intent intent) { 36 Log.i("debug", "--onUnbind--"); 37 return super.onUnbind(intent); 38 } 39 40 @Override 41 public void onDestroy() { 42 Log.i("debug", "--onDestroy--"); 43 super.onDestroy(); 44 } 45 46 //1. 澹版槑Binder绫荤殑瀛愮被锛岀敤浜嶴ervice涓庣粦瀹氱殑Activity鐨勭粍浠惰繘琛岄?淇? 47 public class TimerBinder extends Binder{ 48 public void start(){ 49 Log.i("debug", "----start---"); 50 51 //閫氳繃瀹氭椂鍣紝鏉ュ畨鎺掓椂闂磋鍒? 52 timer.schedule(new TimerTask(){ 53 @Override 54 public void run() { 55 // TODO 鍦ㄦ寚瀹氱殑鏃堕棿鎵ц鐨勪换鍔? 56 NotificationCompat.Builder builder= 57 new NotificationCompat.Builder(getApplicationContext()); 58 builder.setSmallIcon(android.R.drawable.ic_menu_today) 59 .setContentTitle("鎻愰啋") 60 .setContentText("鏃堕棿浜嗭紝璇ヨ捣搴婁簡....") 61 .setTicker("鏃堕棿浜嗭紝璇ヨ捣搴婁簡....") 62 .setDefaults(Notification.DEFAULT_SOUND) 63 .setOngoing(true); 64 65 notifyMgr.notify(2, builder.build()); 66 67 } 68 },10*1000, 5*1000); 69 70 } 71 72 public void stop(){ 73 Log.i("debug", "----stop---"); 74 //鍏抽棴鎵?湁鐨勫畾鏃朵换鍔? 75 timer.cancel(); 76 77 notifyMgr.cancel(2); 78 } 79 } 80 81 }
1 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 2 xmlns:tools="http://schemas.android.com/tools" 3 android:layout_width="match_parent" 4 android:layout_height="match_parent" 5 android:paddingBottom="@dimen/activity_vertical_margin" 6 android:paddingLeft="@dimen/activity_horizontal_margin" 7 android:paddingRight="@dimen/activity_horizontal_margin" 8 android:paddingTop="@dimen/activity_vertical_margin" 9 tools:context=".MainActivity" > 10 11 <Button 12 android:id="@+id/btn1Id" 13 android:layout_width="wrap_content" 14 android:layout_height="wrap_content" 15 android:onClick="bindService" 16 android:text="绑定服务" /> 17 18 <Button 19 android:id="@+id/btn2Id" 20 android:layout_width="wrap_content" 21 android:layout_height="wrap_content" 22 android:layout_below="@id/btn1Id" 23 android:onClick="unbindService" 24 android:text="解除绑定" /> 25 26 <Button 27 android:id="@+id/btn3Id" 28 android:layout_width="wrap_content" 29 android:layout_height="wrap_content" 30 android:layout_below="@id/btn2Id" 31 android:onClick="startTime" 32 android:text="开启定时" /> 33 34 <Button 35 android:id="@+id/btn4Id" 36 android:layout_width="wrap_content" 37 android:layout_height="wrap_content" 38 android:layout_below="@id/btn3Id" 39 android:onClick="stopTime" 40 android:text="关闭定时" /> 41 42 </RelativeLayout>
绑定服务-----------binderService TimerTask的使用
标签:
原文地址:http://www.cnblogs.com/bimingcong/p/4822155.html