Service的简单介绍
发布日期:2021-06-30 21:07:25
浏览次数:4
分类:技术文章
本文共 3496 字,大约阅读时间需要 11 分钟。
protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); stop = (Button) findViewById(R.id.stop); start = (Button) findViewById(R.id.start); start.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(MainActivity.this , MyService.class); startService(intent); //启动服务 } }); stop.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent stopIntent = new Intent(MainActivity.this , MyService.class); stopService(stopIntent); //停止服务 } }); }
public class MyService extends Service {//需要在manifest文件中注册 //自身调用stopself()也可以停止服务运行 public MyService() { } @Override public void onCreate() { //第一次服务创建时调用,已经创建了就直接onstartcommand() super.onCreate(); Log.i("mydate" , "oncreate"); } @Override public int onStartCommand(Intent intent, int flags, int startId) { //服务启动时调用 Log.i("mydate" , "onstartcommand"); return super.onStartCommand(intent, flags, startId); } @Override public void onDestroy() { //服务销毁时调用 Log.i("mydate" , "ondestroy"); super.onDestroy(); } @Override public IBinder onBind(Intent intent) { /*// TODO: Return the communication channel to the service. throw new UnsupportedOperationException("Not yet implemented");*/ return (new MyBinder()); //具体干嘛,做什么 ,如果没有指定干嘛的话就为空 } class MyBinder extends Binder { //活动指挥服务干嘛,做什么什么,需要这个东西决定具体做什么 public void startDownload(){ Log.i("mydate" , "开始下载"); } //... }}
使用onBind()方法
@Override public IBinder onBind(Intent intent) { /*// TODO: Return the communication channel to the service. throw new UnsupportedOperationException("Not yet implemented");*/ return (new MyBinder()); //具体干嘛,做什么 ,如果没有指定干嘛的话就为空 }
public class MainActivity extends AppCompatActivity { private Button stop; private Button start; private MyService.MyBinder myBinder; //指挥服务具体干嘛的 private ServiceConnection connection = new ServiceConnection() { //对开启服务进行包装,使其具体干吗,做什么,什么 @Override public void onServiceConnected(ComponentName name, IBinder service) { myBinder = (MyService.MyBinder) service; myBinder.startDownload(); //具体调用这个方法 //... } @Override public void onServiceDisconnected(ComponentName name) { } }; protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); stop = (Button) findViewById(R.id.stop); start = (Button) findViewById(R.id.start); start.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(MainActivity.this , MyService.class); bindService(intent , connection , BIND_AUTO_CREATE); //绑定服务 指定binder 已经开启了服务,则不在执行这个语句 } }); stop.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent stopIntent = new Intent(MainActivity.this , MyService.class); unbindService(connection); //解绑服务 } }); }}
转载地址:https://liuyan.blog.csdn.net/article/details/79945848 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!
发表评论
最新留言
留言是一种美德,欢迎回访!
[***.207.175.100]2024年04月10日 17时43分28秒
关于作者
喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
redis cli
2021-07-03
redis api
2021-07-03
flink physical partition
2021-07-03
java 解析json
2021-07-03
java http请求
2021-07-03
tensorflow 数据格式
2021-07-03
tf rnn layer
2021-07-03
tf input layer
2021-07-03
tf model create
2021-07-03
tf dense layer两种创建方式的对比和numpy实现
2021-07-03
tf initializer
2021-07-03
tf 从RNN到BERT
2021-07-03
tf keras SimpleRNN源码解析
2021-07-03
tf keras Dense源码解析
2021-07-03
tf rnn输入输出的维度和权重的维度
2021-07-03
检验是否服从同一分布
2021-07-03
tf callbacks
2021-07-03
keras、tf、numpy实现logloss对比
2021-07-03
Ubuntu20.04安装微信
2021-07-03
Restful风格的使用
2021-07-03