Service使用
来源:互联网 发布:java源代码流程图 编辑:程序博客网 时间:2024/06/07 04:16
**Service的使用**
Service和其他3大组件一样,自定义一个MyService继承Service.其实Service就是一个看不见界面的Activity!
一 ,自定义一个MyService继承Service
package com.zhang.myservice;import android.app.Service;import android.content.Intent;import android.os.IBinder;public class DemoService extends Service { @Override public IBinder onBind(Intent intent) { System.out.println("onBind"); return null; } @Override public void onCreate() { System.out.println("onCreate"); super.onCreate(); } @Override public int onStartCommand(Intent intent, int flags, int startId) { System.out.println("onStartCommand"); return super.onStartCommand(intent, flags, startId); } @Override public void onDestroy() { System.out.println("onDestroy"); super.onDestroy(); }}
1.public void onCreate() 当服务第一次创建的时候调用
2.public int onStartCommand() onStart()方法被onStartCommand()取代了 .service再次重启,onStartCommand会被调用
3.public void onDestroy() 当服务销毁的是调用
二,AndroidManifest.xml注册自定义的Service
<!--配置服务 --> <service android:name="com.zhang.myservice.DemoService"> </service>
三,MainActivity.java中开启,关闭Service
package com.zhang.myservice;import android.app.Activity;import android.content.ComponentName;import android.content.Intent;import android.content.ServiceConnection;import android.os.Bundle;import android.os.IBinder;import android.view.View;import android.widget.Toast;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); //开启服务 Toast.makeText(this,"startservice-开启服务",Toast.LENGTH_SHORT).show(); } public void click2(View v) { Intent intent = new Intent(this,DemoService.class); stopService(intent); Toast.makeText(this,"stopService-关闭服务",Toast.LENGTH_SHORT).show(); } //点击按钮 绑定服务 开启服务的第二种方式 public void click3(View v){ Intent intent = new Intent(this,DemoService.class); //连接到DemoService 这个服务 conn = new MyConn(); bindService(intent,conn , BIND_AUTO_CREATE); Toast.makeText(MainActivity.this,"bindService-绑定服务",Toast.LENGTH_SHORT).show(); } //点击按钮手动解绑服务 public void click4(View v){ unbindService(conn); Toast.makeText(this,"unbindService-手动解绑服务",Toast.LENGTH_SHORT).show(); } @Override protected void onDestroy() { //当Activity销毁的时候 要解绑服务 unbindService(conn); super.onDestroy(); } //定义一个类 用来监视服务的状态 private class MyConn implements ServiceConnection{ //当服务连接成功调用 @Override public void onServiceConnected(ComponentName name, IBinder service) { } //失去连接调用 @Override public void onServiceDisconnected(ComponentName name) { } }}
阅读全文
0 0
- 使用service
- Service使用
- service使用
- 第一次使用 web service
- 学会使用Web Service
- service 里面使用toast
- Service的使用
- 使用AIDL写Service
- 使用Service播放音乐
- Android Service的使用
- Android Service 的使用
- 使用service播放音乐
- Android service的使用
- service生命周期及使用
- Java Service Wrapper 使用
- Service 类 使用
- 使用Service的方式
- 关于service的使用
- 读取解析xml文件
- Word2013中合并多个文档
- Spring Boot系列四 Spring @Value 属性注入使用总结一
- Oracle初步认识存储过程
- 前端的知识结构图
- Service使用
- 性能优化——Android群英传
- java基础第一章_原码反码补码
- Spring4集成Swagger:真的只需要四步,五分钟速成
- [LeetCode]21. Merge Two Sorted Lists
- mac sublime text 3 列操作
- 考研英语
- 【java】枚举类的使用
- MNIST机器学习入门