首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 移动开发 > Android >

android基础(二)

2012-07-27 
android基础(2)/**以下内容主要来源于android-sdk-windows/docs**/??services?Started ServiceBound Servi

android基础(2)

/**以下内容主要来源于android-sdk-windows/docs**/?

?

services?

  • Started Service
  • Bound Service<manifest ... > ... <application ... > <service android:name=".ExampleService" /> ... </application></manifest>?android基础(二)
    Started Service?
    可以从Service或者IntentService继承,IntentService从Service继承,内部使用了一个工作线程和队列串行处理请求,如果对并发请求要求不高的话,推荐使用IntentService?

    启动一个服务,当调用startService时,系统会调用服务的onStartCommand
    Intent intent = new Intent(this, HelloService.class);startService(intent);
    如果希望获取结果,可使用PendingIntent(for a?broadcast)?
    继承IntentService实现服务:
    public class HelloIntentService extends IntentService {  /**    * required   */  public HelloIntentService() {      super("HelloIntentService");  }  /**   *当启动服务后,该方法在一个默认的工作线程中执行,当这个方法返回时,服务自动停止.    */  @Override  protected void onHandleIntent(Intent intent) {      // Normally we would do some work here, like download a file.      // For our sample, we just sleep for 5 seconds.      long endTime = System.currentTimeMillis() + 5*1000;      while (System.currentTimeMillis() < endTime) {          synchronized (this) {              try {                  wait(endTime - System.currentTimeMillis());              } catch (Exception e) {              }          }      }  }@Overridepublic int onStartCommand(Intent intent, int flags, int startId) {    //my    Toast.makeText(this, "service starting", Toast.LENGTH_SHORT).show();    //基类的一定要调用    return super.onStartCommand(intent,flags,startId);}}
    继承Service实现服务:?
    public class HelloService extends Service {  private Looper mServiceLooper;  private ServiceHandler mServiceHandler;  // Handler that receives messages from the thread  private final class ServiceHandler extends Handler {      public ServiceHandler(Looper looper) {          super(looper);      }      @Override      public void handleMessage(Message msg) {          // Normally we would do some work here, like download a file.          // For our sample, we just sleep for 5 seconds.          long endTime = System.currentTimeMillis() + 5*1000;          while (System.currentTimeMillis() < endTime) {              synchronized (this) {                  try {                      wait(endTime - System.currentTimeMillis());                  } catch (Exception e) {                  }              }          }          // Stop the service using the startId, so that we don't stop          // the service in the middle of handling another job          stopSelf(msg.arg1);      }  }  @Override  public void onCreate() {    // 创建一个工作线程    // Start up the thread running the service.  Note that we create a    // separate thread because the service normally runs in the process's    // main thread, which we don't want to block.  We also make it    // background priority so CPU-intensive work will not disrupt our UI.    HandlerThread thread = new HandlerThread("ServiceStartArguments",            Process.THREAD_PRIORITY_BACKGROUND);    thread.start();        // Get the HandlerThread's Looper and use it for our Handler     mServiceLooper = thread.getLooper();    mServiceHandler = new ServiceHandler(mServiceLooper);  }  @Override  public int onStartCommand(Intent intent, int flags, int startId) {      Toast.makeText(this, "service starting", Toast.LENGTH_SHORT).show();      // For each start request, send a message to start a job and deliver the      // start ID so we know which request we're stopping when we finish the job      Message msg = mServiceHandler.obtainMessage();      msg.arg1 = startId;      mServiceHandler.sendMessage(msg);            // If we get killed, after returning from here, restart      return START_STICKY;  }  @Override  public IBinder onBind(Intent intent) {      // We don't provide binding, so return null      return null;  }    @Override  public void onDestroy() {    Toast.makeText(this, "service done", Toast.LENGTH_SHORT).show();   }}
    ?注意onStartCommand的返回值,决定当服务被杀掉后,系统怎么处理请求START_NOT_STICKYSTART_STICKYSTART_REDELIVER_INTENT
    停止服务stopSelf(int startId)? 如果startId和最近一次onStartCommand(Intent, int, int?startId)的startId相同则停止服务。stopService()

    ?

    ?

热点排行