Service的简单用法

来源:互联网 发布:罗马2塞琉古兵种数据 编辑:程序博客网 时间:2024/05/21 11:37

首先Service是四大组建之一,Service的优先级比Activity等级高。Service没有用户界面,可以解决两个不同Android应用程序之间的调用和通信问题。

下面介绍一下Service的两种使用方法:

一种是启动方式使用的Service(startservice),另一种是绑定式使用Service(Bound Service)。

以启动式方式分为下面两种:

显示启动:Intent intent=new Intent(this,RandomService.class);

                   StartService(serviceIntent);

隐身启动:先注册需要隐身启动的Service

 <service  android:name=".service.Notification.Service">
            <intent-filter >
                <action android:name="startService"/>
            </intent-filter>
        </service>

隐身启动的代码 Intent Intent=new Intent();

serviceintent.setAction("startService");

以绑定方式使用service:

intent serviceIntent=new Intent(this,MathSevrvice.class);

bindService(serviceIntent,mConnection,Context.BIND_AUTO_CREATE)

0 0
原创粉丝点击