Android移动应用开发——开灯与关灯(小兔子)——实验八——服务的启动与关闭

这篇具有很好参考价值的文章主要介绍了Android移动应用开发——开灯与关灯(小兔子)——实验八——服务的启动与关闭。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

    掌握布局和基本控件的属性功能及使用方法
    掌握startService()方法与stopService()方法启动和关闭服务

通过线性布局和相对布局来搭建一个界面,界面效果如下图所示。当点击“关灯”按钮后,转变到第二个状态。在第二个状态中,点击“开灯”按钮后,跳转回第一个界面状态。鼓励使用startService()方法与stopService()方法,自主设计类似功能的实验案例。

Android移动应用开发——开灯与关灯(小兔子)——实验八——服务的启动与关闭

Android移动应用开发——开灯与关灯(小兔子)——实验八——服务的启动与关闭

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>

 Android移动应用开发——开灯与关灯(小兔子)——实验八——服务的启动与关闭文章来源地址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模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • 移动应用开发之路 05 Android Studio 简单登录界面制作

    学校开了一门移动应用开发课程,我一开始兴趣盎然,但是看到使用的环境是 Java 8 的时候心就凉了一半,在询问老师的意见之后决定使用现在比较常用的Android Studio完成学习,特此记录自学之路。 这篇是一个总结性质的文章,主要为了熟练运用之前讲过的几个UI控件。小项目

    2024年02月08日
    浏览(52)
  • 移动开发最佳实践:为 Android 和 iOS 构建成功应用的策略

    您可以将本文作为指南,确保您的应用程序符合可行的最重要标准。请注意,这份清单远非详尽无遗;您可以加以利用,并添加一些自己的见解。 要制作一个成功的应用程序,你需要了解你是为谁制作的。从创建用户角色开始–基于人口统计、行为模式、动机和目标,对理想

    2024年02月13日
    浏览(36)
  • Flutter与Android开发:构建跨平台移动应用的新选择

    本文内容提纲如下: 介绍Flutter技术:Flutter是一种由Google推出的开源UI工具包,用于构建高性能、跨平台的移动应用。文章将介绍Flutter的基本概念、特点和优势,包括其快速的开发速度、一致的用户界面和丰富的UI组件库等。 Flutter与Android开发的对比:文章将对比Flutter与传统

    2023年04月21日
    浏览(55)
  • 移动应用开发实验一Android studio设计三种计算器的UI

    使用必要的布局方式,设计下面三种计算器的界面: 简单的计算器 科学计算器 程序计算器 边框的设置是建立一个drawable的xml文件,然后写了边框宽度、颜色、圆角和内边距。调用的时候用到了background属性 。

    2024年02月11日
    浏览(44)
  • 移动应用开发 试题 复习

    1、Android 开发中常用的数据库是( ) A、 SQLite B、 Oracle C、 MySql D、 SQL Server A.SQLite 2、从其他应用中读取共享的数据库数据时,需要用到的是 query 方法,返回的是 Curser 数 据,那么这个方法是哪个对象的方法。( ) A、 SQLiteDatabase B、 SQLiteOpenHelper C、 ContentProvider D、 Content

    2024年02月13日
    浏览(30)
  • 第一篇【传奇开心果系列】beeware开发移动应用:轮盘抽奖移动应用

    一、项目目标 使用beeware的toga写传奇开心果轮盘抽奖安卓手机应用和苹果手机应用 二、开发传奇开心果轮盘抽奖安卓应用编程思路 要使用Beeware的Toga库来编写一个传奇开心果轮盘抽奖安卓应用,你需要按照以下步骤进行操作: 安装Beeware:首先,你需要安装Beeware的开发工具包

    2024年01月20日
    浏览(46)
  • 移动应用开发期末复习(自用复习勿转)

    主要考察实验中的通知:notification,service。数据库的增删改查操作结合界面的一些操作。 Android是一种基于Linux的软件平台和操作系统,采用了软件堆层(Software Stack)的架构,由下往上分别是Linux内核层、硬件抽象层、系统运行时库层(又称为中间件层)、应用程序框架层和系

    2024年02月05日
    浏览(35)
  • 移动应用开发介绍及iOS方向学习路线(HUT移动组版)

    ​ 作为一个HUT移动组待了一坤年(两年半)多的老人,在这里为还在考虑进哪个组的萌新们以及将来进组的新朋友提供一份关于移动应用开发介绍以及学习路线的白话文,因为我是iOS方向的,所以学习路线就只介绍iOS了,希望这篇文章对你了解移动应用开发有帮助。 ​ 从字

    2024年02月04日
    浏览(34)
  • Flutter:跨平台移动应用开发的未来

    Flutter的背景和概述 Flutter是由Google开发的一个开源UI工具包,用于构建漂亮、快速且高度可定制的移动应用程序。它于2017年首次发布,并迅速引起了开发者们的关注。Flutter采用了一种全新的方法来构建用户界面,通过使用自绘UI技术,可以实现高性能的跨平台应用开发。 Fl

    2024年01月22日
    浏览(79)
  • 第三篇【传奇开心果系列】Vant开发移动应用:财务管理应用

    使用vant实现财务管理应用:创建一个简单的财务管理应用,用户可以记录和跟踪他们的收入和支出,并生成报表和图表展示财务状况。 1. 首先,安装并引入Vant组件库,以便使用Vant提供的丰富组件来构建财务管理应用界面。 创建一个首页,包括收入、支出、报表和图表四个

    2024年01月22日
    浏览(50)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包