四大组件 之 Service

service

开启service:第一次开启时会调用onCreate
  参数:intent = new Intent(this, MyService.class);serviceConnection = new ServiceConnection() {}
  方法:

    startService(intent);stopService(intent);//没stopService就一直存在
    bindService(intent, serviceConnection, Context.BIND_AUTO_CREATE);//当创建者的类回收时,会自动调用unbindService
结束service:
  如果是startService开启则需要调用stopService(intent);
  如果是bindService开启则需要调用unbindService(serviceConnection);
  如果startService和bindService开启,则需要先调用unbindService,在调用stopService,如果stopService在unbindService前调用的,则onDestroy会在  unbindService后调用。

MyService类:   

public class MyService extends Service {
    private MyBinder mBinder = new MyBinder();

    @Override
    public void onCreate() {
        super.onCreate();
        LogUtils.d("MyService---------onCreate");

        Notification notification = new Notification(R.drawable.ic_launcher,
                "有通知到来", System.currentTimeMillis());
        Intent notificationIntent = new Intent(this, TestService.class);
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0,
                notificationIntent, 0);
        notification.setLatestEventInfo(this, "这是通知的标题", "这是通知的内容",
                pendingIntent);
        startForeground(1, notification);//设置为前台 Service
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        LogUtils.d("MyService---------onStartCommand");
        return super.onStartCommand(intent, flags, startId);
    }

    @Override
    public IBinder onBind(Intent intent) {
        LogUtils.d("MyService---------onBind");
        return mBinder;
    }

    @Override
    public boolean onUnbind(Intent intent) {
        LogUtils.d("MyService---------onUnbind");
        return super.onUnbind(intent);
    }

    @Override
    public boolean stopService(Intent name) {
        LogUtils.d("MyService---------stopService");
        return super.stopService(name);
    }

    @Override
    public void onDestroy() {
        LogUtils.d("MyService---------onDestroy");
        super.onDestroy();
    }

    class MyBinder extends Binder {
        public void onStartDownload() {
            LogUtils.d("MyBinder--------onStartDownload");
        }
    }

}
View Code

  调用Service类:

public class TestService extends Activity{
    
    private Intent intent;
    private ServiceConnection serviceConnection;
    private MyService.MyBinder myBinder;
    private boolean bindService;  

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.test_service);
        
        initData();
    }


    private void initData() {
        intent = new Intent(this, MyService.class);
        serviceConnection = new ServiceConnection() {
            
            @Override
            public void onServiceDisconnected(ComponentName name) {
                LogUtils.d("TestService-------onServiceDisconnected");
            }
            
            @Override
            public void onServiceConnected(ComponentName name, IBinder service) {
                LogUtils.d("TestService-------onServiceConnected");
                myBinder = (MyService.MyBinder)service;
                myBinder.onStartDownload();
            }
        };
        
    }

    
    public void Start_Service(View view) {
        startService(intent);
        
    }
    
    public void stop_Service(View view) {
        stopService(intent);
    }
    
    public void bind_Service(View view) {
        bindService = bindService(intent, serviceConnection, Context.BIND_AUTO_CREATE);
        
    }
    
    public void unbind_Service(View view) {
        if (bindService) {
            unbindService(serviceConnection);
            bindService = false;
        }
        
    }
    

    @Override
    public void onBackPressed() {
        finish();
        super.onBackPressed();
    }
    
    

}
View Code

 

分类:

  前台service:Service几乎都是在后台运行的,但Service的系统优先级还是比较低,因此可以设为前台service,在service调用startForeground

  远程Service:注册Service的时候将它的android:process属性指定成:remote。相当于在另外一个进程中,避免anr

  注意:service是运行在主线程中的。远程Service不能使用传统的bindService,这需要进行ipc。

aidl:共用远程Service。
  创建步骤:
     1.新建远程Service的aidl文件

转载于:https://www.cnblogs.com/cjcblogs/p/4704129.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值