IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> 移动开发 -> 安卓开发—自己创建一个服务,活动和服务进行通信 -> 正文阅读

[移动开发]安卓开发—自己创建一个服务,活动和服务进行通信

解释还是在注释之中

MainActivity

package com.example.servicetest;

import androidx.appcompat.app.AppCompatActivity;

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.Button;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private Button start_service;
    private Button stop_service;
    private Button start_bind;
    private Button stop_bind;
    private MyService.DownloadBinder downloadBinder;

    //创建了ServiceConnection匿名类,onServiceConnected与onServiceDisconnected会在服务绑定与服务断开
    //时调用
    private ServiceConnection connection=new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
            //通过向下转型得到downloadBinder的实例
            downloadBinder= (MyService.DownloadBinder) iBinder;
            downloadBinder.Download();
            downloadBinder.StopDownload();

        }

        @Override
        public void onServiceDisconnected(ComponentName componentName) {

        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        start_service=findViewById(R.id.start_service);
        stop_service=findViewById(R.id.stop_service);
        start_bind=findViewById(R.id.start_Bind);
        stop_bind=findViewById(R.id.stop_Bind);

        start_service.setOnClickListener(this::onClick);
        stop_service.setOnClickListener(this::onClick);
        start_bind.setOnClickListener(this::onClick);
        stop_bind.setOnClickListener(this::onClick);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.start_service:
                Intent startService=new Intent(this,MyService.class);
                startService(startService);//启动服务
                break;

            case R.id.stop_service:
                Intent stopService=new Intent(this,MyService.class);
                stopService(stopService);//停止服务只需在服务逻辑中任何位置调用stopself()就可以停止服务
                break;

            case R.id.start_Bind:
                Intent startBind=new Intent(this,MyService.class);
                //将MyService于MainActivity绑定,第一个参数是Intent对象,第二个是前面创建的
                //ServiceConnection实例,第三个参数是标志位BIND_AUTO_CREATE,表示绑定后自动创建服务
                //这回让Myservice中onCreate执行而onStartcommand不会执行
                bindService(startBind,connection,BIND_AUTO_CREATE);
                break;

            case R.id.stop_Bind:
                Intent stopBind=new Intent(this,MyService.class);
                unbindService(connection);
                break;

            default:break;
        }

    }
}

Myservice

package com.example.servicetest;

import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
import android.util.Log;


//服务得在MainFest中注册
public class MyService extends Service {

    public MyService() {
    }

    //服务创建时调用
    @Override
    public void onCreate() {
        super.onCreate();
        Log.d("MyService","在运行onCreate");
    }

    //服务启动时调用
    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Log.d("MyService","在运行onStartCommand");
        return super.onStartCommand(intent, flags, startId);
    }

    //服务结束时调用
    @Override
    public void onDestroy() {
        super.onDestroy();
        Log.d("MyService","在运行onDestroy");
    }

    private DownloadBinder mBinder=new DownloadBinder();

        class DownloadBinder extends Binder{
            public  void  Download(){
                Log.d("MyService","在运行Download()");
            }
            public  void  StopDownload(){
                Log.d("MyService","在运行StopDownload()");
            }
        }

    //这是service中唯一的一个抽象方法,得在子类中实现
    @Override
    public IBinder onBind(Intent intent) {
        // TODO: Return the communication channel to the service.
        return mBinder;
    }
}
  移动开发 最新文章
Vue3装载axios和element-ui
android adb cmd
【xcode】Xcode常用快捷键与技巧
Android开发中的线程池使用
Java 和 Android 的 Base64
Android 测试文字编码格式
微信小程序支付
安卓权限记录
知乎之自动养号
【Android Jetpack】DataStore
上一篇文章      下一篇文章      查看所有文章
加:2022-02-24 15:24:46  更:2022-02-24 15:26:58 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年9日历 -2024/9/23 18:27:01-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码