zoukankan      html  css  js  c++  java
  • Service

    package com.example.test;
    
    import android.content.ComponentName;
    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.Button;
    import android.widget.EditText;
    import android.widget.RadioGroup;
    import android.widget.TextView;
    
    public class HeightActivity extends AppCompatActivity {
    private Button mBt;
        private EditText mEt;
        private TextView mTv;
        private RadioGroup mRg;
        int sex=0;
        double height=0;
        private ServiceConnection coon=new ServiceConnection() {
            @Override
            public void onServiceConnected(ComponentName name, IBinder service) {
                HeightService.MyBinder binder= (HeightService.MyBinder) service;
                double result=binder.getResult();
                mTv.setText("你的标准体重是:"+result+""+"kg");
    
            }
    
            @Override
            public void onServiceDisconnected(ComponentName name) {
    
            }
        };
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_height);
            mBt= (Button) findViewById(R.id.bt_height);
            mTv= (TextView) findViewById(R.id.tv_height);
            mEt= (EditText) findViewById(R.id.et_height);
            mRg= (RadioGroup) findViewById(R.id.rg_height);
            mRg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(RadioGroup group, int checkedId) {
                    switch (checkedId){
                        case R.id.ab_nan:
                            sex=0;
                            break;
                        case R.id.ab_nv:
                            sex=1;
                            break;
                    }
                }
            });
            mBt.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent=new Intent(HeightActivity.this,HeightService.class);
                    intent.putExtra("sex",sex);
    
                    try {
                        height=Double.parseDouble(mEt.getText().toString()+"");
                    }catch (Exception e){
                        mEt.setText("");
                        mEt.requestFocus();
                        return;
                    }
                    intent.putExtra("height",height);
                    bindService(intent,coon,BIND_AUTO_CREATE);
    
                }
            });
    
        }
    }
    package com.example.test;
    
    import android.app.Service;
    import android.content.Intent;
    import android.os.Binder;
    import android.os.IBinder;
    import android.support.annotation.Nullable;
    import android.util.Log;
    
    /**
     * Created by Administrator on 2016/10/28.
     */
    public class HeightService extends Service {
    double result=0;
        @Override
        public IBinder onBind(Intent intent) {
            int sex=intent.getIntExtra("sex",0);
            double height=intent.getDoubleExtra("height",0);
            calc(sex,height);
            return new MyBinder();
        }
        private void calc(int sex,double height){
            if (sex==0){
                result=(height-80)*0.7;
            }else {
                result=(height-70)*0.6;
            }
        }
        class MyBinder extends Binder{
            public double getResult(){
                return result;
            }
        }
    }
    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        tools:context="com.example.test.HeightActivity">
        <LinearLayout
            android:layout_marginTop="50dp"
            android:gravity="center"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">
            <TextView
                android:layout_gravity="center"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textSize="20sp"
                android:layout_marginLeft="40dp"
                android:text="性别:"/>
            <RadioGroup
                android:id="@+id/rg_height"
    
                android:orientation="horizontal"
                android:layout_width="match_parent"
                android:layout_height="wrap_content">
                <RadioButton
                    android:id="@+id/ab_nan"
                    android:checked="true"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="男的"/>
                <RadioButton
                    android:id="@+id/ab_nv"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="女的"/>
            </RadioGroup>
        </LinearLayout>
        <LinearLayout
            android:orientation="horizontal"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textSize="20sp"
                android:layout_marginLeft="40dp"
                android:text="身高:"/>
            <EditText
                android:id="@+id/et_height"
                android:layout_width="100dp"
                android:layout_height="wrap_content" />
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="cm"
                android:textSize="20sp" />
        </LinearLayout>
        <Button
            android:layout_marginTop="20dp"
            android:layout_gravity="center"
            android:id="@+id/bt_height"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="计算"/>
        <TextView
            android:id="@+id/tv_height"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />
    
    </LinearLayout>

  • 相关阅读:
    MT4系统自带指标代码
    [转载]一个鼠标键盘控制两台甚至多台主机的方法
    国内外证券交易系统开发平台比较
    Win8系统下MT4不能添加指标无法找到技术指标
    Unable to locate package错误解决办法
    ubuntu 常见错误--Could not get lock /var/lib/dpkg/lock
    ubuntu永久修改主机名
    Ant之build.xml详解
    Memory和Storage有什么区别?
    ubuntu下创建eclipse桌面快捷方式
  • 原文地址:https://www.cnblogs.com/jiang2538406936/p/6008426.html
Copyright © 2011-2022 走看看