使用service例程

使用service例程

使用service,有以下的步骤:

  1. 继承android.app.Service。

  2. 覆盖onStart,onCreate,onBind,onDestroy,onUnbind等方法。

  3. 在AndroidManifest.xml 添加<service> 节点来注册自已的service。

  4. 使用startService,stopService,bindService,unbindService来控制service。



service的生命周期很简单:

  1. 当调用了startService的时候,service的onCreate方法首先执行,接着执行onStart方法。

  2. 当调用了bindService方法时,service的onBind方法被调用。

  3. 当调用了unbindService方法时,service的onUnbind方法被调用。

  4. 当调用了stopService方法时,service的onDestroy方法被调用。

service的例子:
下面的例子是实现一个不继从网站上获取信息的功能,如果信息有更新,则发通一个信息到通知栏,打开通知栏后,就可以查看信息内容。网站上的数据格式在android工程目录的assets里面,把它放到自已的网站上,就可以通过该程序来获取了。

  • 继承Service

Java代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
package  com.android.test;
import  java.text.SimpleDateFormat;
import  java.util.ArrayList;
import  android.app.Notification;
import  android.app.NotificationManager;
import  android.app.PendingIntent;
import  android.app.Service;
import  android.content.Context;
import  android.content.Intent;
import  android.os.Binder;
import  android.os.IBinder;
import  android.text.format.Time;
import  android.util.Log;
 
/**
  *
  * @author hhuang
  *
  */
public  class  MyService  extends  Service {
  private  static  final  String TAG =  "MyService" ;
  private  MyBinder mBinder =  new  MyBinder();
  private  FetchDataThread mFetcher;
  //在activity调用bindService的时候,返回service的实例给它。
  @Override
  public  IBinder onBind(Intent intent) {
   Log.d(TAG,  "IBinder....." );
   return  mBinder;
  }
  @Override
  public  void  onCreate() {
   Log.d(TAG,  "onCreate....." );
   super .onCreate();
  }
  @Override
  public  void  onStart(Intent intent,  int  startId) {
   Log.d(TAG,  "onStart...." );
   super .onStart(intent, startId);
   if (mFetcher ==  null ){
    mFetcher =  new  FetchDataThread( this );
   } else {
    mFetcher.setStartTag( true );
   }
   new  Thread(mFetcher).start();
  }
  
  /**
   * 该线程不停地从网站上获取数据,如果数据有更新,则发送一个提示信息
   * @author hhuang
   *
   */
  public  class  FetchDataThread  implements  Runnable {
   private  boolean  tag =  true ;
   private  Context context;
   private  Sms mSms;
   
   public  void  setStartTag( boolean  tag){
    this .tag = tag;
   }
   public  void  stop(){
    tag =  false ;
   }
   public  FetchDataThread(Context c) {
    context = c;
   }
   @Override
   public  void  run() {
    while  (tag) {
     ArrayList<Sms> list = NetWork
       .reflushData( "[url=http://10.0.2.2/WebDemo/data.js]http://10.0.2.2/WebDemo/data.js[/url]" );
     if  (list !=  null  && list.size() >  0 ) {
      Sms currentSms = list.get( 0 );
      Log.d( "MyService:" , currentSms.content +  " : "  + currentSms.from);
      
      if  (currentSms !=  null  && !currentSms.equals(mSms)) {
       Intent i =  new  Intent(context,
         NotificationActivity. class );
       i.removeExtra( "content" );
       i.removeExtra( "from" );
       i.putExtra( "content" , currentSms.content);
       i.putExtra( "from" , currentSms.from);
       
       PendingIntent pending = PendingIntent.getActivity(
         context,  0 , i, PendingIntent.FLAG_UPDATE_CURRENT);
       Notification noticed =  new  Notification();
       noticed.icon = R.drawable.icon;
       noticed.tickerText =  "New Message" ;
       
       noticed.defaults = Notification.DEFAULT_SOUND;
       noticed.setLatestEventInfo(context, currentSms.content,  "from:"  + currentSms.from,
         pending);
       
       //在打开信息提示时,自动把notification清空。
       noticed.flags = Notification.FLAG_AUTO_CANCEL;
       NotificationManager noticedManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
       noticedManager.notify( 0 , noticed);
       mSms = currentSms;
      }
     }
     try  {
      Thread.sleep( 1000 );
     catch  (InterruptedException e) {
      e.printStackTrace();
     }
    }
   }
  }
  @Override
  public  void  onDestroy() {
   mFetcher.stop();
   Log.d(TAG,  "onDestroy..." );
   super .onDestroy();
  }
  @Override
  public  boolean  onUnbind(Intent intent) {
   Log.d(TAG,  "onUnbind..." );
   return  super .onUnbind(intent);
  }
  public  String getSystemTime() {
   Time t =  new  Time();
   t.setToNow();
   //SimpleDateFormat sf = new SimpleDateFormat("yyyy-dd-MM hh:mm:ss");
   return  t.format( "yyyy-dd-MM hh:mm:ss" );
  }
  public  class  MyBinder  extends  Binder {
   MyService getService() {
    return  MyService. this ;
   }
  }
}


在AndroidManifest.xml中注册service Xml代码 

1
2
3
4
5
6
7
8
....
  </ activity >
         < activity  android:name = ".NotificationActivity" />
         < activity  android:name = ".SmsViewActivity" />
   < service  android:name = ".MyService"  android:exported = "true" />
   
     </ application >
......


操作service Java代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
@Override
  public  void  onClick(View v) {
   if (v == startServiceButton) {
    Intent i =  new  Intent();
    i.setClass( this , MyService. class );
    this .startService(i);
    
    this .finish();
   } else  if (v == stopServiceButton){
    Intent i =  new  Intent();
    i.setClass( this , MyService. class );
    this .stopService(i);
   } else  if (v == bindServiceButton){
    Intent i =  new  Intent();
    i.setClass( this , MyService. class );
    this .bindService(i, mServiceConnection, BIND_AUTO_CREATE);
   } else {
    this .unbindService(mServiceConnection);
    
   }
  }

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值