绑定服务-----------binderService TimerTask的使用

绑定服务 服务中通过定义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 }

MainActivity.java

 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 }

TimerService.java

 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>

activity_main.xml

时间: 2024-08-06 07:58:56

绑定服务-----------binderService TimerTask的使用的相关文章

androi之Service+Broadcast+Timer+ui【通过绑定服务、自定义回调接口判断

最近项目要定时从服务器获取某些信息,通过研究来总结一下下[我以定时判断网络状态为例来阐述] 原理: 我们定义一个Service,在该Service中设置一个定时器Timer,通过TimerTask的策略来检查当前应用的网络连接状态,关键是在该Service需要自定义一个回调接口用于向我们的Activity来回调发送网络状态,然后通过Bind来绑定当前的Service,在绑定成功之后得到回调信息 代码: Service类 package com.example.androidtimerdemo;

绑定服务学习

绑定服务主要是其他组件绑定服务(比如活动),然后发送请求,接收返回.这个服务主要是作为其他组件的佣人,不会再后台无限 地运行.个人认为关键要学习的是如何绑定以及服务和组件之间的通信. 如何绑定到服务 一个绑定的服务是Service类的实现,允许其他组件绑定和他通信.要为服务提供绑定,必须实现onBind回调方法.这个方法返回IBinder, 定义了客户端可以和服务通信的程序接口. 客户端可以调用bindService绑定到service上,要这么做必须提供ServiceConnection的实现

绑定服务调用本地服务中的方法

如果想调用服务中的方法, 通过startService()是做不到的, 这时需要用bindService来解决. 下面的demo是在Activity中调用Service中的自定义方法---methodInService 这个demo可以解决在项目开发中调用service里的数据. 这里在service中使用到了代理模式.这是为了,给service组件和activity组件中间添加一个中间人. 通过代理来传递数据.也就是binder对象.这个代理就是接口IService Service中的代码如下

绑定服务调用远程服务中的方法

在Andorid平台中,各个组件运行在自己的进程中,他们之间是不能相互访问的,但是在程序之间是不可避免的要传递一些对象,在进程之间相互通信.为了实现进程之间的相互通信,Andorid采用了一种轻量级的实现方式RPC(Remote Procedure Call 远程进程调用)来完成进程之间的通信,并且Android通过接口定义语言(Andorid Interface Definition Language ,AIDL)来生成两个进程之间相互访问的代码,例如,你在Activity里的代码需要访问Se

使用绑定服务实现一个简单的音乐播放器

效果 布局文件 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal" android:layout_width="fill_parent" android:

绑定服务

绑定服务 右边部分就是绑定服务的运行过程 这样绑定的目的就是服务绑定者调用服务的方法,在我的样例里就是体现为服务访问者调用服务的show()方法 来张效果图吧 分析:  1.第一步还是继承服务类 1 package fry; 2 3 import java.io.FileDescriptor; 4 5 import android.app.Service; 6 import android.content.Intent; 7 import android.os.Binder; 8 import

绑定服务抽取接口

示例代码 简易的播放器: A. 写一个接口,在接口中写一个要暴露出去的方法 B. 让服务里的内部类实现这个接口 C. 在activity的连接成功后,强转为接口类型 D. 调用接口中的方法,实际上调用的时接口的实现类服务里的内部类里的方法 import android.app.Service; import android.content.Intent; import android.media.MediaPlayer; import android.os.Binder; import andr

adnroid四大组件之Service(5) 绑定服务,数据通信-IBinder

绑定服务: 第一步:创建一个服务类,实现onBind() 方法, 返回一个IBinder对象, 这个对象定义了与服务通信的接口,客户端接收到这个IBinder,与服务进行 通信.  IBinder的实现类通过一个 方法用来获取服务,和onTransact方法 与客户端进行通信. 第二步:在清单文件注册服务<service>  <service android:name="com.example.bindservicedemo.BindService"></

android之绑定服务调用服务的方法

public class MainActivity extends Activity { private music.MyBinder mm;//在activity里面得到服务ibinder对象的引用 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } public v