2
Android Service本地服务详解
source link: https://www.jishudog.com/32523/html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
释放双眼,带上耳机,听听看~!
1. Service分类
1.1 Service的类型分类
1.2 特点
2.本地Service
2.1具体使用步骤
- 步骤1:新建子类继承Service类
需重写父类的onCreate()、onStartCommand()、onDestroy()和onBind()方法
- 步骤2:构建用于启动Service的Intent对象
- 步骤3:调用startService()启动Service、调用stopService()停止服务
- 步骤4:在AndroidManifest.xml里注册Service
2.2实例DOME
- 步骤1:新建子类继承Service类
需重写父类的onCreate()、onStartCommand()、onDestroy()和onBind()
Myservice
public class MyService extends Service { //启动Service之后,就可以在onCreate()或onStartCommand()方法里去执行一些具体的逻辑 //由于这里作Demo用,所以只打印一些语句 @Override public void onCreate() { super.onCreate(); System.out.println("执行了onCreat()"); } @Override public int onStartCommand(Intent intent, int flags, int startId) { System.out.println("执行了onStartCommand()"); return super.onStartCommand(intent, flags, startId); } @Override public void onDestroy() { super.onDestroy(); System.out.println("执行了onDestory()"); } @Nullable @Override public IBinder onBind(Intent intent) { return null; } }
- 步骤2:在主布局文件设置两个Button分别用于启动和停止Service
activity_main.xml(代码简单所以过滤不写) - 步骤3:构建Intent对象,并调用startService()启动Service、stopService停止服务
MainActivity.java
public class MainActivity extends Activity { private MyConn conn; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } //点击按钮 开启服务 通过startservice public void click1(View v) { Intent intent = new Intent(this,DemoService.class); startService(intent); //开启服务 } public void click2(View v) { Intent intent = new Intent(this,DemoService.class); stopService(intent); } //点击按钮 绑定服务 开启服务的第二种方式 public void click3(View v){ Intent intent = new Intent(this,DemoService.class); //连接到DemoService 这个服务 conn = new MyConn(); bindService(intent,conn , BIND_AUTO_CREATE); } //点击按钮手动解绑服务 public void click4(View v){ unbindService(conn); } @Override protected void onDestroy() { //当Activity销毁的时候 要解绑服务 unbindService(conn); super.onDestroy(); } //定义一个类 用来监视服务的状态 private class MyConn implements ServiceConnection{ //当服务连接成功调用 @Override public void onServiceConnected(ComponentName name, IBinder service) { System.out.println("onServiceConnected"); } //失去连接调用 @Override public void onServiceDisconnected(ComponentName name) { } } }
-
步骤4:在AndroidManifest.xml里注册Service
AndroidManifest.xml
//注册Service服务 <service android:name=".MyService"> </service>
测试结果
Paste_Image.png
Androidmanifest里Service的常见属性说明
属性 说明 备注 android:name Service的类名 android:label Service的名字 若不设置,默认为Service类名 android:icon Service的图标 android:permission 申明此Service的权限 有提供了该权限的应用才能控制或连接此服务 android:process 表示该服务是否在另一个进程中运行(远程服务) 不设置默认为本地服务;remote则设置成远程服务 android:enabled 系统默认启动 true:Service 将会默认被系统启动;不设置则默认为false android:exported 该服务是否能够被其他应用程序所控制或连接 不设置默认此项为 false3. 常见的生命周期使用
Paste_Image.png
3.1 只使用startService启动服务的生命周期
startService启动服务的生命周期
3.2 只使用BindService绑定服务的生命周期
BindService绑定服务的生命周期
3.3 同时使用startService()启动服务、BindService()绑定服务的生命周期
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK