権限の要求を追加

This commit is contained in:
ろむねこ 2024-01-23 09:45:08 +09:00
parent 235afb92cb
commit fab0f75d1a
No known key found for this signature in database
GPG Key ID: FA1F39A1BA37D168

View File

@ -23,6 +23,7 @@ import android.widget.FrameLayout;
import android.widget.Toast; import android.widget.Toast;
import androidx.activity.result.ActivityResultLauncher; import androidx.activity.result.ActivityResultLauncher;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity; import androidx.appcompat.app.AppCompatActivity;
import androidx.browser.customtabs.CustomTabsIntent; import androidx.browser.customtabs.CustomTabsIntent;
import androidx.core.app.ActivityCompat; import androidx.core.app.ActivityCompat;
@ -35,6 +36,9 @@ import com.google.firebase.firestore.FirebaseFirestore;
import com.journeyapps.barcodescanner.ScanContract; import com.journeyapps.barcodescanner.ScanContract;
import com.journeyapps.barcodescanner.ScanOptions; import com.journeyapps.barcodescanner.ScanOptions;
// Manifest
import android.Manifest;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
@ -85,6 +89,10 @@ public class MainActivity extends AppCompatActivity {
db = FirebaseFirestore.getInstance();//Firebaseとの紐づけ db = FirebaseFirestore.getInstance();//Firebaseとの紐づけ
if (!hasPermissions()) {
requestPermissions();
}
BottomNavigationView bottomNavigationView = findViewById(R.id.nav_view); BottomNavigationView bottomNavigationView = findViewById(R.id.nav_view);
this.homeFragment = HomeFragment.newInstance("test", "test"); this.homeFragment = HomeFragment.newInstance("test", "test");
@ -188,6 +196,44 @@ public class MainActivity extends AppCompatActivity {
} }
private boolean hasPermissions() {
return ActivityCompat.checkSelfPermission(this, Manifest.permission.BLUETOOTH) == PackageManager.PERMISSION_GRANTED &&
ActivityCompat.checkSelfPermission(this, Manifest.permission.BLUETOOTH_ADMIN) == PackageManager.PERMISSION_GRANTED &&
ActivityCompat.checkSelfPermission(this, Manifest.permission.BLUETOOTH_SCAN) == PackageManager.PERMISSION_GRANTED &&
ActivityCompat.checkSelfPermission(this, Manifest.permission.BLUETOOTH_CONNECT) == PackageManager.PERMISSION_GRANTED &&
ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED &&
ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED &&
ActivityCompat.checkSelfPermission(this, Manifest.permission.POST_NOTIFICATIONS) == PackageManager.PERMISSION_GRANTED &&
ActivityCompat.checkSelfPermission(this, Manifest.permission.VIBRATE) == PackageManager.PERMISSION_GRANTED &&
ActivityCompat.checkSelfPermission(this, Manifest.permission.FOREGROUND_SERVICE) == PackageManager.PERMISSION_GRANTED;
}
private void requestPermissions() {
ActivityCompat.requestPermissions(this,
new String[]{
Manifest.permission.BLUETOOTH,
Manifest.permission.BLUETOOTH_ADMIN,
Manifest.permission.BLUETOOTH_SCAN,
Manifest.permission.BLUETOOTH_CONNECT,
Manifest.permission.ACCESS_FINE_LOCATION,
Manifest.permission.ACCESS_COARSE_LOCATION,
Manifest.permission.POST_NOTIFICATIONS,
Manifest.permission.VIBRATE,
Manifest.permission.FOREGROUND_SERVICE
},
2);
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (requestCode == 2) {
if (!hasPermissions()) {
Toast.makeText(this, "Permissions not granted.", Toast.LENGTH_SHORT).show();
}
}
}