一、功能介绍
该项目实现的功能主要有:
- 实现开灯关灯效果
- 通过“开灯”与“关灯”按钮的点击事件来实现服务的开启与关闭功能
二、代码实现
1. 资源准备
将项目所需要的图片btn_close.png 、btn_open.png 、img_close.png 、img_open.png导入程序的drawable-hdpi 文件夹中(默认情况下程序中没有drawable-hdpi 文件夹,需手动在res 文件夹中创建一个。
2. 布局文件设计
在layout文件夹中编辑activity_main.xml
<?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:background="#efede0"
android:gravity="center"
android:orientation="vertical"
tools:context=".MainActivity">
<Button
android:id="@+id/btn_switch"
android:layout_width="150dp"
android:layout_height="50dp"
android:background="@drawable/btn_close"
android:text="开灯" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="50dp">
<ImageView
android:id="@+id/iv_open"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/img_open"/>
<ImageView
android:id="@+id/iv_close"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/img_close" />
</RelativeLayout>
</LinearLayout>
3. 实现逻辑功能
3.0 全局配置文件
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.hzj.switches">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.Switches">
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
<!-- 注册服务 -->
<service
android:name=".MyService"
android:enabled="true"
android:exported="true" />
</application>
</manifest>
3.1 创建服务类
public class MyService extends Service {
@Override
public void onCreate() {
super.onCreate();
Log.i("MyService", "创建服务,执行 onCreate()方法");
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.i("MyService", "开启服务,执行 onStartCommand()方法");
return super.onStartCommand(intent, flags, startId);
}
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onDestroy() {
super.onDestroy();
Log.i("MyService", "关闭服务,执行 onDestroy()方法");
}
}
3.2 首页功能实现
public class MainActivity extends AppCompatActivity {
private Button btn_switch;
private ImageView iv_open, iv_close;
// isOpen 表示存放开灯与关灯状态的变量,为 false 时表示关灯状态,为 true 时表示开灯状态
private boolean isOpen = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init();
}
private void init() {
btn_switch = findViewById(R.id.btn_switch);
iv_open = findViewById(R.id.iv_open);
iv_close = findViewById(R.id.iv_close);
btn_switch.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (isOpen) {//说明此时界面上的图显示的是开灯状态
btn_switch.setText("开灯");
btn_switch.setBackgroundResource(R.drawable.btn_close);
iv_open.setVisibility(View.GONE); //隐藏开灯按钮图片
iv_close.setVisibility(View.VISIBLE);//显示关灯按钮图片
isOpen=false;//设置为关灯状态
//关闭服务
Intent intent = new Intent(MainActivity.this, MyService.class);
stopService(intent);
} else { //此时界面上的图显示的是关灯状态
btn_switch.setText("关灯");
btn_switch.setBackgroundResource(R.drawable.btn_open);
iv_open.setVisibility(View.VISIBLE);//显示开灯按钮图片
iv_close.setVisibility(View.GONE); //隐藏关灯按钮图片
isOpen=true; //设置为开灯状态
//开启服务
Intent intent = new Intent(MainActivity.this, MyService.class);
startService(intent);
}
}
});
}
}
三、效果展示