Android,带有UI管理器的后台服务

如何解决Android,带有UI管理器的后台服务

我遇到后勤问题。我对android / java不太熟悉(我制作了一些显示蓝牙设备的简单UI应用程序,仅此而已)。 正在考虑对本机进行反应,但似乎不合适。 我要制作后台服务,该服务将自动连接到蓝牙设备,并处理数据的进出,还需要无限期地运行并在启动后运行。 但是我还需要一些UI进行设置,例如选择Bluetooth设备,或通过该服务/ BT连接传输自定义消息。

那有可能吗? 我见过类似的应用,例如自定义通知应用。 如何实现,是否可以通过UI与服务进行通信?

我只需要一些方向或关键字,而不是寻找现成的解决方案。 谢谢。

解决方法

您需要为Android使用XML资源文件布局。那就是你的用户界面。

https://developer.android.com/guide/topics/ui/declaring-layout 阅读此书,如果您还不熟悉android,请选择developer.android.com,因为它是您最好的朋友。

您将像在android中学习Java一样学习XML。它随处可见。

示例布局代码:

<androidx.constraintlayout.widget.ConstraintLayout 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"
android:orientation="vertical"
android:background="@color/colorPrimary"
>

<TextView
    android:id="@+id/mainheader"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="1dp"
    android:text="@string/greetings"
    android:background="@color/colorAccent"
    android:textAlignment="center"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.498"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

<Button
    android:id="@+id/buttonMainMenu"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/menu"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/mainheader" />

<Button
    android:id="@+id/buttonMortgageCalculator"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Mortgage Calculator"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.0"
    app:layout_constraintStart_toEndOf="@+id/buttonMainMenu"
    app:layout_constraintTop_toBottomOf="@+id/mainheader" />

<Button
    android:id="@+id/buttonTest"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Test"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toEndOf="@+id/buttonMortgageCalculator"
    app:layout_constraintTop_toBottomOf="@+id/mainheader" />

<TextView
    android:id="@+id/homePageDescription"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="@string/homeDescription"
    android:background="@color/colorAccent"
    android:textAlignment="center"
    app:layout_constraintBottom_toTopOf="@+id/imageView"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.0"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/buttonMortgageCalculator"
    app:layout_constraintVertical_bias="0.0" />

<ImageView
    android:id="@+id/imageView"
    android:layout_width="393dp"
    android:layout_height="380dp"
    android:cropToPadding="true"
    android:src="@drawable/tobiasmealeyhouse"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent" />

然后在您的main.activity中: Java代码:

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //Go to Main Menu
        Button mainmenu = findViewById(R.id.buttonMainMenu);
        mainmenu.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                Intent goToMenuIntent = new Intent(view.getContext(),MainActivity.class);
                startActivityForResult(goToMenuIntent,0);
            }
        });

        //Go to Mortgage Calculator
        Button mortgagecalculator = findViewById(R.id.buttonMortgageCalculator);
        mortgagecalculator.setOnClickListener(new View.OnClickListener(){
            public void onClick(View view) {
                Intent mortgageCalculatorIntent = new Intent(view.getContext(),MortgageCalculator.class);
                startActivityForResult(mortgageCalculatorIntent,0);
            }
        });

        //Go to test
        Button test = findViewById(R.id.buttonTest);
        test.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                Intent goToTestIntent = new Intent(view.getContext(),Test.class);
                startActivityForResult(goToTestIntent,0);
            }
        });
    }

enter image description here

enter image description here

从本质上讲,您将使用包含在XML文件中创建的对象的按钮和EditText框来构建UI,然后使用Java对其进行编程以完成您想要进行的通信。

,

由于我是第一次误解了你的意思,所以我只剩下那个答案,然后再写一个新的答案。

我认为您应该先查看主屏幕小部件,然后: https://android-developers.googleblog.com/2009/04/introducing-home-screen-widgets-and.html#:~:text=One%20exciting%20new%20feature%20in%20the%20Android%201.5,details%20about%20a%20song%20playing%20in%20the%20background

有一篇关于它的文章,为您提供了UI。然后,您可以通过小部件在用户主屏幕上的小部件中与后端服务进行交互。

在阅读了有关小部件的知识之后,对小部件的设计及其需要执行的操作有了一个了解: https://www.tutorialspoint.com/android/android_bluetooth.htm 这使您开始了解蓝牙数据传输的工作方式。您将使用诸如蓝牙适配器之类的库,并调用特定的类来处理应用程序的蓝牙功能。

最后,如果您需要后端存储,请查看android用于设备存储的SQLite数据库。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


依赖报错 idea导入项目后依赖报错,解决方案:https://blog.csdn.net/weixin_42420249/article/details/81191861 依赖版本报错:更换其他版本 无法下载依赖可参考:https://blog.csdn.net/weixin_42628809/a
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下 2021-12-03 13:33:33.927 ERROR 7228 [ main] o.s.b.d.LoggingFailureAnalysisReporter : *************************** APPL
错误1:gradle项目控制台输出为乱码 # 解决方案:https://blog.csdn.net/weixin_43501566/article/details/112482302 # 在gradle-wrapper.properties 添加以下内容 org.gradle.jvmargs=-Df
错误还原:在查询的过程中,传入的workType为0时,该条件不起作用 &lt;select id=&quot;xxx&quot;&gt; SELECT di.id, di.name, di.work_type, di.updated... &lt;where&gt; &lt;if test=&qu
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct redisServer’没有名为‘server_cpulist’的成员 redisSetCpuAffinity(server.server_cpulist); ^ server.c: 在函数‘hasActiveC
解决方案1 1、改项目中.idea/workspace.xml配置文件,增加dynamic.classpath参数 2、搜索PropertiesComponent,添加如下 &lt;property name=&quot;dynamic.classpath&quot; value=&quot;tru
删除根组件app.vue中的默认代码后报错:Module Error (from ./node_modules/eslint-loader/index.js): 解决方案:关闭ESlint代码检测,在项目根目录创建vue.config.js,在文件中添加 module.exports = { lin
查看spark默认的python版本 [root@master day27]# pyspark /home/software/spark-2.3.4-bin-hadoop2.7/conf/spark-env.sh: line 2: /usr/local/hadoop/bin/hadoop: No s
使用本地python环境可以成功执行 import pandas as pd import matplotlib.pyplot as plt # 设置字体 plt.rcParams[&#39;font.sans-serif&#39;] = [&#39;SimHei&#39;] # 能正确显示负号 p
错误1:Request method ‘DELETE‘ not supported 错误还原:controller层有一个接口,访问该接口时报错:Request method ‘DELETE‘ not supported 错误原因:没有接收到前端传入的参数,修改为如下 参考 错误2:cannot r
错误1:启动docker镜像时报错:Error response from daemon: driver failed programming external connectivity on endpoint quirky_allen 解决方法:重启docker -&gt; systemctl r
错误1:private field ‘xxx‘ is never assigned 按Altʾnter快捷键,选择第2项 参考:https://blog.csdn.net/shi_hong_fei_hei/article/details/88814070 错误2:启动时报错,不能找到主启动类 #
报错如下,通过源不能下载,最后警告pip需升级版本 Requirement already satisfied: pip in c:\users\ychen\appdata\local\programs\python\python310\lib\site-packages (22.0.4) Coll
错误1:maven打包报错 错误还原:使用maven打包项目时报错如下 [ERROR] Failed to execute goal org.apache.maven.plugins:maven-resources-plugin:3.2.0:resources (default-resources)
错误1:服务调用时报错 服务消费者模块assess通过openFeign调用服务提供者模块hires 如下为服务提供者模块hires的控制层接口 @RestController @RequestMapping(&quot;/hires&quot;) public class FeignControl
错误1:运行项目后报如下错误 解决方案 报错2:Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project sb 解决方案:在pom.
参考 错误原因 过滤器或拦截器在生效时,redisTemplate还没有注入 解决方案:在注入容器时就生效 @Component //项目运行时就注入Spring容器 public class RedisBean { @Resource private RedisTemplate&lt;String
使用vite构建项目报错 C:\Users\ychen\work&gt;npm init @vitejs/app @vitejs/create-app is deprecated, use npm init vite instead C:\Users\ychen\AppData\Local\npm-