Service的简单介绍
发布日期:2021-06-30 21:07:25
浏览次数:3
分类:技术文章
本文共 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 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!
发表评论
最新留言
初次前来,多多关照!
[***.217.46.12]2024年04月21日 17时31分29秒
关于作者
喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
Python手动读取MNIST数据集
2019-04-30
Python手动读取CIFAR-10数据集
2019-04-30
Pytorch(十一) —— 分布式(多GPU)训练
2019-04-30
Deeplab v3
2019-04-30
tensor/矩阵/图片等更换通道,调整size
2019-04-30
本地和colab 中 改变tensorflow的版本
2019-04-30
Camera-ready ddl
2019-04-30
CUB-200鸟类数据集
2019-04-30
Python反射机制
2019-04-30
YAPF —— Python代码格式化工具
2019-04-30
UGC 用户产生内容
2019-04-30
ranger
2019-04-30
slurm
2019-04-30
xfce4
2019-04-30
xrdp
2019-04-30
Raft算法
2019-04-30
Python计算文本BLEU分数
2019-04-30
swap内存(linux)
2019-04-30
torch.distributed 分布式
2019-04-30