在前台创建Service

  关于Service,我们经常使用它来进行一些不需要进行界面展示的后台任务,比如下载之类的。今天我们来一看一种Service,在前台进行创建。前台服务是很难被应用杀死的,像通知栏里面的音乐播放器,计步器等等,如果不使用360等清理软件,一般情况下是不会被杀死的,一直存在于通知栏中。简而言之,前台服务就是运行在通知栏中的一种服务。下面直接来Service类的代码。

public class ForeGroundService extends Service {

    private static final int NOTIFICATION_ID = 1;//如果id为0,将导致不能设置为前台service

    private NotificationManager manager;

    private MyBinder myBinder=new MyBinder();//与外界交互

    @Override
    public void onCreate() {
        super.onCreate();

        manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        Notification.Builder builder = new Notification.Builder(this);
        PendingIntent contentIntent = PendingIntent.getActivity(this, 0, new Intent(this, MyActivity.class), 0);
        builder.setSmallIcon(R.drawable.ic_launcher);
        builder.setContentIntent(contentIntent);
        builder.setContentText("前台服务已启动");
        Notification notification = builder.build();

        startForeground(NOTIFICATION_ID, notification);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        stopForeground(true);
    }

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

    //在Activity获取Service实例
     public class MyBinder extends Binder{
        public ForeGroundService getService(){
            return ForeGroundService.this;
        }
    }
}

  看看这里的核心方法startForeground(int id, Notification notification)。需要注意的是,在API5之前,使用的是setForeground()方法,考虑到现在应该很少会有Android2.0的设备了,所以我这里并没有考虑这个问题。看看startForeground()方法,需要传入两个参数,一个标识通知的int型整数和给状态栏的通知,源码注释中告诉我们这个id不可以为0,通知可以根据需求在onCreate()方法中自由定制。这里也提供了继承Binder的MyBinder对象来与外界进行交互,并提供了返回服务实例的方法供Activity调用。既然在onCreate()方法中启动了前台服务,同样对应的在onDestory()方法中也要停止前台服务,这里调用的是stopForeground(true)方法,停止服务的前台状态,在内存不足的时候将会被杀死。
  看完前台服务的创建,我们来看一下如何在Activity中启动前台服务。过程很简单,onCreate方法中bindService,onDestroy方法中unbindService。
  

public class MyActivity extends BaseActivity {

    private ForeGroundService foreGroundService;

    private ServiceConnection conn;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        conn=new ServiceConnection() {
            @Override
            public void onServiceConnected(ComponentName name, IBinder service) {
                ForeGroundService.MyBinder myBinder= (ForeGroundService.MyBinder) service;
                foreGroundService=myBinder.getService();
            }

            @Override
            public void onServiceDisconnected(ComponentName name) {

            }
        };
        Intent intent=new Intent(MyActivity.this,ForeGroundService.class);
        bindService(intent,conn, Context.BIND_AUTO_CREATE);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unbindService(conn);
    }

  最后不要忘记在清单文件中注册Service。

  <service android:name=".service.ForeGroundService">
        </service>

  这样一个前台服务就可以使用了,效果图如下:
  这里写图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值