java - Android、UI マネージャーを使用したバックグラウンド サービス

okwaves2024-01-25  7

物流上の問題が発生しています。私は Android/Java についてはあまり詳しくありません (Bluetooth デバイスを表示する単純な UI アプリをいくつか作成しましたが、それだけです)。 反応ネイティブを検討していましたが、適合しないようです。 Bluetooth デバイスに自動的に接続し、データの入出力を処理するバックグラウンド サービスを作成したいと考えています。また、起動後も無期限に実行する必要があります。 ただし、Bluetooth デバイスの選択や、そのサービス/BT 接続を介したカスタム メッセージの送信など、設定用の UI も必要です。

それは可能でしょうか? カスタム通知アプリなど、そのようなアプリを見たことがあります。 それにはどのようにアプローチすればよいでしょうか。UI からサービスと通信することは可能でしょうか?

必要なのは方向性またはキーワードだけであり、すぐに解決できるものを探しているわけではありません。 ありがとうございます。



------------------------

Android 用の XML リソース ファイル レイアウトを使用する必要があります。それがあなたの UI です。

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" />

</androidx.constraintlayout.widget.ConstraintLayout>

次に、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);
            }
        });
    }

基本的に、ボタンと EditText ボックスを備えた UI を構築します。は XML ファイルで作成されたオブジェクトなので、Java でそれらをプログラムして、通信する限り必要なことを実行します。

1

もっと具体的に言うと、UI を備えた Android アプリをすでに作成しました。単純なものではありますが、それでもです。まだわかっていないのは、UI からも管理でき、常にバックグラウンドで実行できるバックグラウンド サービスを作成する方法です。

– 不安

2020 年 9 月 4 日 5:54



------------------------

最初に誤解したので、その回答はそのままにして、まったく新しい回答を書きます。

まずはホーム画面のウィジェットを調べることから始めるべきだと思います。 https://android-developers.googleblog.com/2009/04/introducing-home-screen-widgets-and.html#:~:text=One%20exciting%20new%20feature%20in%20the%20Android%201.5,詳細%20about%20a%20song%20playing%20in%20the%20background。

UI を提供する記事があります。その後、ユーザーのホーム画面上のウィジェットからウィジェットを介してバックエンド サービスと連携できるようになります。

ウィジェットについて読み、ウィジェットのデザインとウィジェットが何をする必要があるかについてのアイデアを理解したら、次のようにします。 https://www.tutorialspoint.com/android/android_bluetooth.htm これBluetooth データ転送の仕組みについて説明します。 Bluetooth アダプターなどのライブラリを使用し、アプリの Bluetooth 機能を処理するための特定のクラスを呼び出します。

最後に、バックエンド ストレージが必要な場合は、Android がデバイス上のストレージとして使用する SQLite データベースを調べてください。

総合生活情報サイト - OKWAVES
総合生活情報サイト - OKWAVES
生活総合情報サイトokwaves(オールアバウト)。その道のプロ(専門家)が、日常生活をより豊かに快適にするノウハウから業界の最新動向、読み物コラムまで、多彩なコンテンツを発信。