四大组件之Service 前台服务

  • 前台服务和通知十分相似,典型的前台服务,比如天气软件通常会在系统状态栏显示当前的天气信息,这就是应用了前台服务。
  • 前台服务的实现代码十分简单
  • 主活动代码如下:
public class MainActivity extends AppCompatActivity implements View.OnClickListener{

    private Button button_start,button_stop;
    private MyReceiver myReceiver;
    private IntentFilter intentFilter;
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        init();
        intentFilter.addAction("START");
        registerReceiver(myReceiver,intentFilter);
    }

    private void init() {
        button_start= (Button) findViewById(R.id.button_start);
        button_stop= (Button) findViewById(R.id.button_stop);
        button_start.setOnClickListener(this);
        button_stop.setOnClickListener(this);
        myReceiver=new MyReceiver();
        intentFilter=new IntentFilter();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.button_start:
                Intent intent=new Intent(this,MyService.class);
                startService(intent);
                break;
            case R.id.button_stop:
                Intent intent1=new Intent(this,MyService.class);
                stopService(intent1);
                break;
        }
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        unregisterReceiver(myReceiver);
    }
}
  • 这里仅仅是实现了服务的开启和关闭
  • 同时注册了一个自定义的广播接收器,并无其他的复杂代码

  • 下面是我们的Service代码:

public class MyService extends Service {
    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        return super.onStartCommand(intent, flags, startId);
    }

    @Override
    public void onCreate() {
        super.onCreate();
        Notification notification=new Notification();
        notification.icon=R.mipmap.ic_launcher;
        RemoteViews rv=new RemoteViews(getPackageName(),R.layout.layout_notification);
        rv.setTextViewText(R.id.textView_title,"我是标题");
        rv.setTextViewText(R.id.textView_content,"我是内容");
        PendingIntent intent=PendingIntent.getBroadcast(MyApplication.getContent(),0,new Intent("START"),0);
        notification.contentView=rv;
        rv.setOnClickPendingIntent(R.id.textView_title,intent);
        startForeground(1,notification);

    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }
}
  • 在onCreate()方法中我们实现了前台服务的逻辑编码
  • 仔细观察,你就会发现,这些代码和通知的写法完全一样,只有这一句代码和通知不同
startForeground(1,notification);
  • 这句代码的意思就是将自定义的MyService编程前台服务,并且在状态栏显示出来,在下拉菜单栏中就可以看到前台服务的详细信息。
  • 这里我设置了前台服务的文本的点击事件,当被点击的时候发出广播,在相应的广播接收器中处理逻辑

  • 我的广播接收器如下:

public class MyReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        String action=intent.getAction();
        switch (action){
            case "START":
                Toast.makeText(context, "我被点击了", Toast.LENGTH_SHORT).show();
                break;
        }
    }
}
  • 这里仅仅是进行了简单的吐司提示

这里写图片描述

  • 不知道大家注意到没有,在我的服务中,相应notification的发出广播的动作,我就使用了全局获取广播的技巧

  • 通过上面这些知识,我们完全可以自定义实现一个播放器的前台服务的控制栏。

  • 这篇博客和我的这篇博客 实践–Notification实现Music状态栏一起浏览,

  • 我不是特别清楚现在手机上那些音乐软件用哪一种方式实现的手机音乐控制栏,但我感觉用前台服务实现的可能性跟大一些吧,因为服务是不依赖活动运行的,而用Notification实现必须一直依赖活动才能实现一直播放,当活动被清除之后,音乐也就停止了,使用前台服务的话,就不会有这种问题的出现。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值