android-studio BindService服务

本文详细探讨了如何在 Android Studio 中使用 BindService 进行服务绑定,从 MainActivity.java 和 BindService.java 的代码实例出发,揭示了服务绑定的过程、优势以及注意事项,帮助开发者更好地理解和应用 BindService 技术。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity"
    android:orientation="horizontal">
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <Button
            android:id="@+id/btn_start"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="620dp"
            android:background="#03A9F4"
            android:text="开启地震监测服务"
            android:textStyle="bold" />
        <Button
            android:id="@+id/btn_stop"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@+id/btn_start"
            android:layout_marginTop="10dp"
            android:background="#03A9F4"
            android:text="获取地震监测信息"
            android:textStyle="bold" />
                <RelativeLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content">
                    <TextView
                        android:layout_centerHorizontal="true"
                        android:id="@+id/where"
                        android:layout_width="match_parent"
                        android:layout_height="50dp"
                        android:text="美国"
                        android:textSize="20dp"
                        android:textStyle="bold"
                        android:gravity="center"
                        android:layout_marginTop="480dp" />
                    <TextView
                        android:layout_centerHorizontal="true"
                        android:id="@+id/dengji"
                        android:layout_width="match_parent"
                        android:layout_height="50dp"
                        android:text="7级"
                        android:textSize="20dp"
                        android:textStyle="bold"
                        android:layout_below="@id/where"
                        android:layout_marginTop="20dp"
                        android:gravity="center" />
                </RelativeLayout>
    </RelativeLayout>
</LinearLayout>

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);
        }
    }
}
  

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值