MainActivity上でQRリーダーを呼ぶように

This commit is contained in:
ろむねこ 2024-01-17 16:54:21 +09:00
parent ae7af9d01a
commit 8bd8900286
No known key found for this signature in database
GPG Key ID: FA1F39A1BA37D168

View File

@ -1,7 +1,11 @@
package com.example.childguard; package com.example.childguard;
import static java.security.AccessController.getContext;
import androidx.activity.result.ActivityResultLauncher;
import androidx.appcompat.app.ActionBar; import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity; import androidx.appcompat.app.AppCompatActivity;
import androidx.browser.customtabs.CustomTabsIntent;
import androidx.core.app.ActivityCompat; import androidx.core.app.ActivityCompat;
import androidx.core.app.NotificationCompat; import androidx.core.app.NotificationCompat;
import androidx.core.app.NotificationManagerCompat; import androidx.core.app.NotificationManagerCompat;
@ -19,6 +23,7 @@ import android.content.Intent;
import android.content.IntentFilter; import android.content.IntentFilter;
import android.content.pm.PackageManager; import android.content.pm.PackageManager;
import android.graphics.Color; import android.graphics.Color;
import android.net.Uri;
import android.os.Bundle; import android.os.Bundle;
import android.os.Vibrator; 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.EventListener;
import com.google.firebase.firestore.FirebaseFirestore; import com.google.firebase.firestore.FirebaseFirestore;
import com.google.firebase.firestore.FirebaseFirestoreException; 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 { public class MainActivity extends AppCompatActivity {
@ -68,6 +77,24 @@ public class MainActivity extends AppCompatActivity {
return df.format(date); return df.format(date);
} }
private final ActivityResultLauncher<ScanOptions> 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 @Override
protected void onCreate(Bundle savedInstanceState) { protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
@ -94,6 +121,14 @@ public class MainActivity extends AppCompatActivity {
return true; 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検知機能 //Bluetooth検知機能
IntentFilter intentFilter = new IntentFilter(); IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(BluetoothDevice.ACTION_ACL_CONNECTED); intentFilter.addAction(BluetoothDevice.ACTION_ACL_CONNECTED);