service

Posted storm47

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了service相关的知识,希望对你有一定的参考价值。

activity

package com.hanqi.text_service;

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

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
    //普通方式启动
    public void b1(View view){
        //准备意图:显示意图
        Intent intent=new Intent(this,MyService.class);

        intent.putExtra("name","垃圾");

        startService(intent);
        Toast.makeText(MainActivity.this, "MyService启动", Toast.LENGTH_SHORT).show();
    }
    //普通方式结束
    public void b2(View view){
        //准备意图:显示意图
        Intent intent=new Intent(this,MyService.class);

        stopService(intent);
        Toast.makeText(MainActivity.this, "MyService停止", Toast.LENGTH_SHORT).show();
    }

    //绑定开始

    //实例化服务连接类
    ServiceConnection sc;
    //接收返回的数据 类对象
    MyService.MyBinder myb;
    public void b3(View view){
        Intent intent=new Intent(this,MyService.class);
        if(sc==null) {
            sc = new ServiceConnection() {
                @Override
                public void onServiceConnected(ComponentName name, IBinder service) {

                     myb = (MyService.MyBinder) service;
                      Toast.makeText(MainActivity.this, "绑定启动完成,接收数据"+myb.text(), Toast.LENGTH_SHORT).show();
                }

                //异常断开连接状态触发
                @Override
                public void onServiceDisconnected(ComponentName name) {
                    Toast.makeText(MainActivity.this, "服务连接中断", Toast.LENGTH_SHORT).show();

                }
            };
        }
        //1)意图
        //2)服务连接
        //3)启动方式,一般用Context.BIND_AUTO_CREATE 字段创建
        bindService(intent, sc, Context.BIND_AUTO_CREATE);
    }
    //结束捆绑服务
    public void b4(View view){
        if(sc!=null){
            unbindService(sc);
            sc=null;
        }
        else {
            Toast.makeText(MainActivity.this, "请先绑定服务", Toast.LENGTH_SHORT).show();
        }
    }

    //点击接收实时数据
    public void b5(View view){
        //服务的运行数据
        //用代理对象

        Toast.makeText(MainActivity.this, "读到的服务数据"+ myb.text(), Toast.LENGTH_SHORT).show();

    }
}

service

package com.hanqi.text_service;

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

public class MyService extends Service {
    public MyService() {
        Log.e("TAG","MyService被构造");
    }

    public class MyBinder extends Binder{
        //数据交护的方法
        public int text(){
            return anInt;
        }
    }
    //回调方法
    //绑定
    int anInt=0;
    @Override
    public IBinder onBind(Intent intent) {
        Log.e("TAG","onBind绑定");

        //创建业务逻辑
        new Thread(){
            @Override
            public void run() {
                while(anInt<20){
                    anInt++;
                    try {
                        Thread.sleep(1000);
                    }catch (Exception e){
                        e.printStackTrace();
                    }
                }
            }
        }.start();

        return new MyBinder();
    }

    @Override
    public void onCreate() {
        super.onCreate();
        Log.e("TAG", "oncreat调用");
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        Log.e("TAG", "onDestroy调用");
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        String s=intent.getStringExtra("name");

        Log.e("TAG", "onStartCommand被调用并接收数据"+s);
        return super.onStartCommand(intent, flags, startId);

    }

    @Override
    public void onRebind(Intent intent) {
        super.onRebind(intent);
        Log.e("TAG", "onRebind");
    }

    @Override
    public boolean onUnbind(Intent intent) {
        Log.e("TAG", "onUnbind");
        return super.onUnbind(intent);
    }
}

以上是关于service的主要内容,如果未能解决你的问题,请参考以下文章

java [Intent] Intent片段以启动Activity,Service或发送广播。 #android_snippet #android

Grails 根据环境将 Service 注入到 Controller

如何在片段中隐藏键盘?

java.util.MissingResourceException: Can't find bundle for base name init, locale zh_CN问题的处理(代码片段

uefi规范示例提到EfiCoreImageHandle。怎么弄?

如何用最新的KARAF替换pax-logging-service与pax-logging-log4j2