掌握布局和基本控件的属性功能及使用方法
掌握startService()方法与stopService()方法启动和关闭服务
通过线性布局和相对布局来搭建一个界面,界面效果如下图所示。当点击“关灯”按钮后,转变到第二个状态。在第二个状态中,点击“开灯”按钮后,跳转回第一个界面状态。鼓励使用startService()方法与stopService()方法,自主设计类似功能的实验案例。
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#efede0">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="150dp"
>
<ImageButton
android:layout_width="150dp"
android:layout_height="50dp"
android:layout_centerHorizontal="true"
android:background="@drawable/btn_open"
android:id="@+id/ib"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="关灯"
android:textSize="20sp"
android:textColor="@color/black"
android:id="@+id/tv"/>
</RelativeLayout>
<ImageView
android:layout_width="wrap_content"
android:layout_height="200dp"
android:background="@drawable/img_open"
android:layout_marginTop="100dp"
android:id="@+id/iv"/>
</LinearLayout>文章来源:https://www.toymoban.com/news/detail-452645.html
文章来源地址https://www.toymoban.com/news/detail-452645.html
MainActivity.java
package com.example.shiyan8;
import androidx.appcompat.app.AppCompatActivity;
import android.app.Service;
import android.content.Intent;
import android.os.Bundle;
import android.os.IBinder;
import android.util.Log;
import android.view.View;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
private ImageButton imageButton;
private ImageView imageView;
private TextView textView;
private boolean isOpen=true;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init();
}
private void init() {
imageButton = findViewById(R.id.ib);
imageView = findViewById(R.id.iv);
textView = findViewById(R.id.tv);
imageButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (isOpen) {
imageButton.setBackgroundResource(R.drawable.btn_close);
imageView.setBackgroundResource(R.drawable.img_close);
textView.setText("开灯");
isOpen=false;
Intent intent = new Intent(MainActivity.this, MyService.class);
stopService(intent);
}else{
imageButton.setBackgroundResource(R.drawable.btn_open);
imageView.setBackgroundResource(R.drawable.img_open);
textView.setText("关灯");
isOpen=true;
Intent intent = new Intent(MainActivity.this, MyService.class);
startService(intent);
}
}
});
}
}
MyService.java
package com.example.shiyan8;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;
public class MyService extends Service {
public MyService() {
}
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.i("service","服务已开启。");
return super.onStartCommand(intent, flags, startId);
}
@Override
public void onDestroy() {
super.onDestroy();
Log.i("service","服务已关闭。");
}
}
到了这里,关于Android移动应用开发——开灯与关灯(小兔子)——实验八——服务的启动与关闭的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!