zoukankan      html  css  js  c++  java
  • android学习笔记(三)

    认识Service

    • 启动服务:
    `startService(new Intent(MainActivity.this,MyService.class));`
    • 结束服务:
    stopService(new Intent(MainActivity.this,MyService.class));

    绑定服务

    除了startService外,还可以通过绑定的方式启动服务。

    新建一个Service

    public class MyService extends Service {
        public MyService() {
        }
    
        @Override
        public IBinder onBind(Intent intent) {
            // TODO: Return the communication channel to the service.
            return new Binder();
        }
    //在启动了服务后执行该方法
        @Override
        public int onStartCommand(Intent intent, int flags, int startId) {
            new Thread(){
                @Override
                public void run() {
                    super.run();
    
                    while(true){
                    System.out.println("服务正在运行");
    
                    try {
                        sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                }
            }.start();
    
            return super.onStartCommand(intent, flags, startId);
        }
    }

    MainAvtivity

    private Intent intent;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main1);
    
            intent = new Intent(MainActivity.this,MyService.class);
    
            findViewById(R.id.btn_service).setOnClickListener(this);
    //服务的实例在一个操作系统中只可能有一个
            findViewById(R.id.btn_stopService).setOnClickListener(this);
    
            findViewById(R.id.bindService).setOnClickListener(this);
    
        }
    
        @Override
        public void onClick(View view) {
            switch (view.getId()){
                case R.id.btn_service:
                    startService(intent);
                    break;
                case R.id.btn_stopService:
                    stopService(intent);
                    break;
                case R.id.bindService:
    //                参数(intent,连接状态量,)
                    bindService(intent,this, Context.BIND_AUTO_CREATE);
                    break;
                case R.id.unbingService:
                    unbindService(this);
                    break;
            }
        }
    //服务绑定成功之后执行
        @Override
        public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
            System.out.println("服务已经绑定");
        }
    //服务所在进程崩溃或者杀死的时候开始
        @Override
        public void onServiceDisconnected(ComponentName componentName) {
    
        }

    xml视图

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/btn_service"
        android:text="启动服务"
        />
    
        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/btn_stopService"
            android:text="停止服务"
            />
        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/bindService"
            android:text="绑定服务"
            />
        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/unbingService"
            android:text="解绑服务"
            />

    与Service进行通信

    启动Service并传递数据

    其实和activity内部的传递数据一样,不过是传递数据的对象变成了Service。

    MainActivity

    package com.example.myapplication;
    
    import android.content.ComponentName;
    import android.content.Context;
    import android.content.Intent;
    import android.content.ServiceConnection;
    import android.os.IBinder;
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.EditText;
    import android.widget.TextView;
    
    public class MainActivity extends AppCompatActivity implements View.OnClickListener, ServiceConnection {
        private Intent intent;
        private EditText editText;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main1);
    
            editText = (EditText) findViewById(R.id.editText_service);
            intent = new Intent(MainActivity.this,MyService.class);
    
            findViewById(R.id.btn_service).setOnClickListener(this);
    //服务的实例在一个操作系统中只可能有一个
            findViewById(R.id.btn_stopService).setOnClickListener(this);
    
    
    
        }
    
        @Override
        public void onClick(View view) {
            switch (view.getId()){
                case R.id.btn_service:
    //                利用service传递数据
                    intent.putExtra("data",editText.getText().toString());
                    startService(intent);
                    break;
                case R.id.btn_stopService:
                    stopService(intent);
                    break;
    
            }
        }
    //服务绑定成功之后执行
        @Override
        public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
            System.out.println("服务已经绑定");
        }
    //服务所在进程崩溃或者杀死的时候开始
        @Override
        public void onServiceDisconnected(ComponentName componentName) {
    
        }
    }
    

    新建一个Service

    package com.example.myapplication;
    
    import android.app.Service;
    import android.content.Intent;
    import android.os.Binder;
    import android.os.IBinder;
    import android.provider.Settings;
    import android.widget.EditText;
    
    public class MyService extends Service {
        private boolean running = false;
        private String data = "123";
    
        public MyService() {
        }
    
        @Override
        public IBinder onBind(Intent intent) {
            // TODO: Return the communication channel to the service.
            return new Binder();
        }
    
        @Override
        public int onStartCommand(Intent intent, int flags, int startId) {
    //        service接收数据
            data = intent.getStringExtra("data");
    
            return super.onStartCommand(intent, flags, startId);
        }
    
        @Override
        public void onCreate() {
            super.onCreate();
    
            running = true;
            new Thread(){
                @Override
                public void run() {
                    super.run();
    
                    while(running){
    
                        System.out.println(data);
    
                        try {
                            sleep(1000);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }.start();
        }
    
        @Override
        public void onDestroy() {
            super.onDestroy();
    
            running = false;
        }
    }
    

    xml视图

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical" android:layout_width="match_parent"
        android:layout_height="match_parent">
    
        <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/editText_service"
            android:text="默认信息"
            />
    
    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/btn_service"
        android:text="启动服务"
        />
    
        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/btn_stopService"
            android:text="停止服务"
            />
    
    
    </LinearLayout>
  • 相关阅读:
    守卫者的挑战
    黑魔法师之门
    noip2015 普及组
    noip2015 提高组day1、day2
    40026118素数的个数
    高精度模板
    经典背包系列问题
    修篱笆
    [LintCode] Linked List Cycle
    [LintCode] Merge Two Sorted Lists
  • 原文地址:https://www.cnblogs.com/comefuture/p/8305938.html
Copyright © 2011-2022 走看看