android-studio BindService服务

.xml



    
        

MainActivity.java

package com.example.myapplication;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
    private Button btn_start;
    private Button btn_stop;
    private TextView where;
    private TextView dengji;
    String str1 = "地震源";
    String str2 = "美国";
    String str3 = "地震等级";
    String str4 = "7级";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        btn_start=(Button)findViewById(R.id.btn_start) ;
        btn_stop=(Button)findViewById(R.id.btn_stop) ;
        where= (TextView) findViewById(R.id.where);
        dengji=(TextView)findViewById(R.id.dengji);
        where.setText(str1);//设置原来的文本
        where.setTag(false);//标记textview为false(表示没有被点击过)
        dengji.setText(str3);//设置原来的文本
        dengji.setTag(false);//标记textview为false(表示没有被点击过)
        btn_start.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    boolean flag = (boolean) where.getTag();//当点击时,首先判断是否已经点击过
                    if(!flag){//没有被点击过
                        where.setText(str2);
                        where.setTag(true);
                    }else{//已经点击过了
                        where.setText(str1);
                        where.setTag(false);
                    }
                }
        });
        btn_stop.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                boolean flag = (boolean) dengji.getTag();//当点击时,首先判断是否已经点击过
                if(!flag){//没有被点击过
                    dengji.setText(str4);
                    dengji.setTag(true);
                }else{//已经点击过了
                    dengji.setText(str3);
                    dengji.setTag(false);
                }
            }
        });
    }
}

BindService.java

package com.example.myapplication;
import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
import android.widget.Toast;
public class BindService extends Service {
    @Override
    public IBinder onBind(Intent intent) {
        MyBinder mMyBinder =new MyBinder();
        return mMyBinder;
    }
    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        return super.onStartCommand(intent, flags, startId);
    }
    private void kaiqijiance(int money) {
            if (money > 1000){
                Toast.makeText(getApplicationContext(),"",1).show();
            }else {
                Toast.makeText(getApplicationContext(),"",1).show();
            }
            Toast.makeText(getApplicationContext(), "地震监测服务绑定成功", 0).show();
    }
    public class MyBinder extends Binder {
        public void callBanZheng(int money) {
            kaiqijiance(money);
        }
    }
}
  

你可能感兴趣的:(Android开发)