From 8bd8900286ece5fca087971cd719d5045b62e8ac Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E3=82=8D=E3=82=80=E3=81=AD=E3=81=93?= Date: Wed, 17 Jan 2024 16:54:21 +0900 Subject: [PATCH] =?UTF-8?q?MainActivity=E4=B8=8A=E3=81=A7QR=E3=83=AA?= =?UTF-8?q?=E3=83=BC=E3=83=80=E3=83=BC=E3=82=92=E5=91=BC=E3=81=B6=E3=82=88?= =?UTF-8?q?=E3=81=86=E3=81=AB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/example/childguard/MainActivity.java | 35 +++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/app/src/main/java/com/example/childguard/MainActivity.java b/app/src/main/java/com/example/childguard/MainActivity.java index fa612de..a8600be 100644 --- a/app/src/main/java/com/example/childguard/MainActivity.java +++ b/app/src/main/java/com/example/childguard/MainActivity.java @@ -1,7 +1,11 @@ package com.example.childguard; +import static java.security.AccessController.getContext; + +import androidx.activity.result.ActivityResultLauncher; import androidx.appcompat.app.ActionBar; import androidx.appcompat.app.AppCompatActivity; +import androidx.browser.customtabs.CustomTabsIntent; import androidx.core.app.ActivityCompat; import androidx.core.app.NotificationCompat; import androidx.core.app.NotificationManagerCompat; @@ -19,6 +23,7 @@ import android.content.Intent; import android.content.IntentFilter; import android.content.pm.PackageManager; import android.graphics.Color; +import android.net.Uri; import android.os.Bundle; import android.os.Vibrator; @@ -49,6 +54,10 @@ import com.google.firebase.firestore.DocumentSnapshot; import com.google.firebase.firestore.EventListener; import com.google.firebase.firestore.FirebaseFirestore; import com.google.firebase.firestore.FirebaseFirestoreException; +import com.google.zxing.integration.android.IntentIntegrator; +import com.google.zxing.integration.android.IntentResult; +import com.journeyapps.barcodescanner.ScanContract; +import com.journeyapps.barcodescanner.ScanOptions; public class MainActivity extends AppCompatActivity { @@ -68,6 +77,24 @@ public class MainActivity extends AppCompatActivity { return df.format(date); } + private final ActivityResultLauncher QrLauncher = registerForActivityResult( + new ScanContract(), + result -> { + if (result != null) { + String url = result.getContents(); + Log.d("QRFragment", "onActivityResult: " + url); + if (url != null) { + Uri uri = Uri.parse(url); + CustomTabsIntent.Builder builder = new CustomTabsIntent.Builder(); + CustomTabsIntent customTabsIntent = builder.build(); + customTabsIntent.launchUrl(this, uri); + } + } else { + Log.d("QRFragment", "onActivityResult: null"); + } + } + ); + @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); @@ -94,6 +121,14 @@ public class MainActivity extends AppCompatActivity { return true; }); + findViewById(R.id.fab_scan_qr_code).setOnClickListener(v -> { + Log.d("QRFragment", "onClick: called"); + //QRリーダ起動 + ScanOptions options = new ScanOptions(); + options.setPrompt("QRコードを読み取ってください"); + QrLauncher.launch(options); + }); + //Bluetooth検知機能 IntentFilter intentFilter = new IntentFilter(); intentFilter.addAction(BluetoothDevice.ACTION_ACL_CONNECTED);