android service学习与研究

什么是Service

Android引入了Service的概念是为了处理后台进程。Service不实现任何用户界面。最 常见的例子如:媒体播放器程序,它可以在转到后台运行的时候仍然能保持播放歌曲;或者如文件下载程序,它可以在后台执行文件的下载。

它跟Activity的级别差不多,但是他不能自己运行,需要通过某一个Activity或者其他Context对象来调用, Context.startService() Context.bindService()
<!--[if !supportLineBreakNewLine]-->
<!--[endif]-->


Service
的生命周期

Service的生命周期方法比Activity少一些,只有onCreate, onStart, onDestroy
我们有两种方式启动一个Service,他们对Service生命周期的影响是不一样的。两种启动Service的方式有所不同。这里要说明一下的是如果你在ServiceonCreate或者onStart做一些很耗时间的事情,最好在Service里启动一个线程来完成,因为Service是跑在主线程中,会影响到你的UI操作或者阻塞主线程中的其他事情。
<!--[if !supportLineBreakNewLine]-->
<!--[endif]-->

创建一个Service
Android
中已经定义了一个 ‘Service’类,所有其他的Service都继承于该类。Service类中定义了一系列的生命周期相关的方法,如: onCreate(), onStart(), onDestroy()

 

启动Service

有了 Service 类我们如何启动他呢,有两种方法:

  • Context.startService()
  • Context.bindService()

在同一个应用任何地方调用 startService() 方法就能启动 Service 了,然后系统会回调 Service 类的 onCreate() 以及 onStart() 方法。这样启动的 Service 会一直运行在后台,直到 Context.stopService() 或者 selfStop() 方法被调用。另外如果一个 Service 已经被启动,其他代码再试图调用 startService() 方法,是不会执行 onCreate() 的,但会重新执行一次 onStart()

另外一种 bindService() 方法的意思是,把这个 Service 和调用 Service 的客户类绑起来,如果调用这个客户类被销毁,Service 也会被销毁。用这个方法的一个好处是,bindService() 方法执行后 Service 会回调上边提到的 onBind() 方发,你可以从这里返回一个实现了 IBind 接口的类,在客户端操作这个类就能和这个服务通信了,比如得到 Service 运行的状态或其他操作。如果 Service 还没有运行,使用这个方法启动 Service 就会 onCreate() 方法而不会调用 onStart()

用其他方式启动 Service

其实不光能从 Activity 中启动 Service ,还有一个很有用的方法是接收系统的广播,这就要用到 Receiver 。在 Mainfest 文件中配置你得 Receiver 能接收什么样的广播消息,那么即使你得程序没有显示给用户,你的 Service 也能启动。你要做的就是继承 android.content.BroadcastReceiver ,然后实现 onReceive(Context context, Intent intent) 方法,就可以启动你得 Service 了。这里不能 bindService 因为一个 Receiver 是一个短暂存在的对象,所以 bind 是没有什么意义的。

这种方法启动service,你可以参考我以前的一篇文章android activity/service开机后自动运行

 

接下来用一个例子来演示一下service的创建以及调用。

首先是创建service这一部分,代码如下:

 


import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Binder;
import android.os.IBinder;
import android.util.Log;
import android.widget.Toast;

/**
* This is an example of implementing an application service that runs locally
* in the same process as the application. The {@link LocalServiceController}
* and {@link LocalServiceBinding} classes show how to interact with the
* service.
*
* <p>Notice the use of the {@link NotificationManager} when interesting things
* happen in the service. This is generally how background services should
* interact with the user, rather than doing something more disruptive such as
* calling startActivity().
*/

public class LocalService extends Service {
    
    private NotificationManager mNM;
    private MediaPlayer mp;

    
/**
      * Class for clients to access. Because we know this service always
      * runs in the same process as its clients, we don't need to deal with
      * IPC.
      */

    public class LocalBinder extends Binder {
         LocalService getService() {
            return LocalService.this;
        }
    }
    
     @Override
    public void onCreate() {
         mNM = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
        
        Log.i("Zx", "Duration");

        
// Display a notification about us starting. We put an icon in the status bar.

         showNotification();
    }

     @Override
    public void onDestroy() {
        
// Cancel the persistent notification.

         mNM.cancel(R.string.local_service_started);

        
// Tell the user we stopped.

         Toast.makeText(this, R.string.local_service_stopped, Toast.LENGTH_SHORT).show();
        
         mp.stop();
    }

     @Override
    public IBinder onBind(Intent intent) {
        return mBinder;
    }

    
// This is the object that receives interactions from clients. See

    
// RemoteService for a more complete example.

    private final IBinder mBinder = new LocalBinder();

    
/**
      * Show a notification while this service is running.
      */

    private void showNotification() {
        
// In this sample, we'll use the same text for the ticker and the expanded notification

        CharSequence text = getText(R.string.local_service_started);

        
// Set the icon, scrolling text and timestamp

        Notification notification = new Notification(R.drawable.stat_sample, text,
                System.currentTimeMillis());

        
// The PendingIntent to launch our activity if the user selects this notification

         PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
                new Intent(this, Demo.class), 0);

        
// Set the info for the views that show in the notification panel.

        notification.setLatestEventInfo(this, getText(R.string.local_service_label),
                       text, contentIntent);

        
// Send the notification.

        
// We use a layout id because it is a unique number. We use it later to cancel.

         mNM.notify(R.string.local_service_started, notification);
        
        
//add music player here

         mp = MediaPlayer.create(this, R.raw.test_cbr);
         mp.start();
    }

}

 

至于如何调用就很简单了,你可以在需要启动 service的地方:

 

 

      Intent i = new Intent();
      i.setClass(Demo.this, LocalService.class);
      startService(i);

from:http://hi.baidu.com/nubone/blog/item/b07d282181f98affd6cae253.html

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值