Android --- Service 应用

Service简介

    Service是android 系统中的四大组件之一(Activity、Service、BroadcastReceiver、ContentProvider),它跟Activity的级别差不多,但不能自己运行只能后台运行,并且可以和其他组件进行交互。service可以在很多场合的应用中使用,比如播放多媒体的时候用户启动了其他Activity这个时候程序要在后台继续播放,比如检测SD卡上文件的变化,再或者在后台记录你地理信息位置的改变等等,总之服务总是藏在后台的, 例如,一个service可能处理网络事物,播放音乐,执行文件I/O,或与一个内容提供者交互,所有这些都在后台进行

Service两种启动模式

Service的启动有两种方式:context.startService() 和 context.bindService()

1、定义服务

public class MusicService extends Service {
    
    @Override
    public IBinder onBind(Intent intent) {
        Log.e("m_tag", "onBind");
        // 当服务被其他组件绑定时触发
        return new MyBinder();
    }

    class MyBinder extends Binder {
        public void start() {
            Log.e("m_tag", "start");
        }

        public void pause() {
            Log.e("m_tag", "pause");
        }

        public MusicService getService() {
            return MusicService.this;
        }
    }

    @Override
    public boolean onUnbind(Intent intent) {
        Log.e("m_tag", "onUnbind");
        return true;
    }

    @Override
    public void onRebind(Intent intent) {
        super.onRebind(intent);
        Log.e("m_tag", "onRebind");
    }

    @Override
    public void onCreate() {
        super.onCreate();
        Log.e("m_tag", "===Service onCreate===");
    }

    // 其他组件跟当前服务的沟通入口
    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Log.e("m_tag", "===Service onStartCommand===");
        return START_STICKY;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        Log.e("m_tag", "===Service onDestroy===");
    }
}

2 、注册服务

<service    android:name=".MusicService"
            android:enabled="true"
            android:exported="true">
</service>

3 、使用服务

//startService :第一次启动会使服务 onCreate ,接着执行 onStartCommand ,在服务运行的情况下,每 startService 一次就会触发 onStartCommand 一次

//stopService :停止当前在后台运行 ( 未被绑定 ) 的服务
//    case R.id.btn_start:
//	  Intent it = new Intent(MainActivity.this,MusicService.class);
//	  startService(it);
//	  break;
//    case R.id.btn_stop:
//	  Intent it1 = new Intent(MainActivity.this,MusicService.class);
//	  stopService(it1);
//	  break;
// bindService :第一次 bindService 如果服务未运行则会触发 onCreate 一次,然后触发 onBind 方法, bindService 只能绑定一次

// unBindService :取消绑定

绑定服务过程

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

    @Override
    public void onServiceDisconnected(ComponentName name) {
	myBinder = null;
    }
};
基于 ServiceConnection 来绑定服务
Intent it2 = new Intent(MainActivity.this,MusicService.class);
//(Intent 对象, ServiceConnection 对象, Context.BIND_AUTO_CREATE 表示绑定时自动创建 Service 对象的 flag)
bindService(it2,conn, Context.BIND_AUTO_CREATE);

解除绑定
unbindService(conn);


注意:只要 bindService 那么在停止之前必须先 unBindService 才能停止 , 只要服务使用中途有 startService ,那么该服务都会进入独立运行状态


自定义通知

自定义通知布局中允许出现的视图: TextView ImageView Button ProgressBar, 通知设置自定义布局

notification = new Notification(R.drawable.filesystem_icon_music,name,System.currentTimeMillis());
// 加载自定义的布局 ( 远程的布局加载器 )
RemoteViews layout = new RemoteViews(getApplication().getPackageName(),R.layout.notification_layout);
// 设置自定义布局
notification.contentView = layout;

//更新布局中的内容
notification.contentView.setTextViewText(R.id.n_name,name);
//改变布局中的图标
notification.contentView.setImageViewResource(R.id.n_play_or_pause,R.drawable.music_notify_pause_normal);

自定义布局中视图的点击监听

Intent pIntent = new Intent(MusicService.this, MusicService.class);
pIntent.putExtra(OPT_KEY,OPT_PLAY);
PendingIntent playOrPauseIntent = PendingIntent.getService(MusicService.this, 1, pIntent, PendingIntent.FLAG_UPDATE_CURRENT);
// 设置通知布局中视图的点击行为
layout.setOnClickPendingIntent(R.id.n_play_or_pause, playOrPauseIntent);





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值