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