Android studio: How to create BiometricManager and other bugs - android-studio

So as a homework from collage i have to make a simple login app using fingerprint, i followed this video: How to Make a FingerPrint Authentication System in Android Studio and Java
But im getting some error that aren't explained and i can't find a answer form, this is my code:
package com.example.actividad14;
import androidx.annotation.NonNull;
import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.content.ContextCompat;
import android.graphics.Color;
import android.hardware.biometrics.BiometricManager;
import android.hardware.biometrics.BiometricPrompt;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import java.util.concurrent.Executor;
public class MainActivity extends AppCompatActivity {
#RequiresApi(api = Build.VERSION_CODES.Q)
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView msg_txt = findViewById(R.id.txt_msg);
Button login_btn = findViewById(R.id.login_btn);
BiometricManager biometricManager = BiometricManager.from(this);
switch (biometricManager.canAuthenticate()){
case BiometricManager.BIOMETRIC_SUCCESS:
msg_txt.setText("You can use the fingerprint sensor to login");
msg_txt.setTextColor(Color.parseColor("#Fafafa"));
break;
case BiometricManager.BIOMETRIC_ERROR_NO_HARDWARE:
msg_txt.setText("The device doesn't have a fingerprint sensor");
login_btn.setVisibility(View.GONE);
break;
case BiometricManager.BIOMETRIC_ERROR_HW_UNAVAILABLE:
msg_txt.setText("The biometric sensor is currently unavailable");
login_btn.setVisibility(View.GONE);
break;
case BiometricManager.BIOMETRIC_ERROR_NONE_ENROLLED:
msg_txt.setText("Your device doesn't have any fingerprint saved, please check your security settings");
login_btn.setVisibility(View.GONE);
break;
}
Executor executor = ContextCompat.getMainExecutor(this);
BiometricPrompt biometricPrompt = new BiometricPrompt(MainActivity.this, executor, new androidx.biometric.BiometricPrompt.AuthenticationCallback() {
#Override
public void onAuthenticationError(int errorCode, #NonNull CharSequence errString) {
super.onAuthenticationError(errorCode, errString);
}
#Override
public void onAuthenticationSucceeded(#NonNull androidx.biometric.BiometricPrompt.AuthenticationResult result) {
super.onAuthenticationSucceeded(result);
Toast.makeText(getApplicationContext(),"Login Succes!", Toast.LENGTH_SHORT).show();
}
#Override
public void onAuthenticationFailed() {
super.onAuthenticationFailed();
}
});
final BiometricPrompt.PrompInfo prompInfo = BiometricPrompt.PrompInfo.Builder()
.setTitle("Login")
.setDescription("Use your fingerprint to login in your app")
.setNegativeButtonText("Cancel")
.build();
login_btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
biometricPrompt.authenticate(prompInfo);
}
});
}
}
At BiometricManager biometricManager = BiometricManager.from(this); im getting "cannot resolve method 'from' in 'biometricmanager'", but every video i have seen says that you create your BiometricManager this way.
At BiometricPrompt biometricPrompt = new BiometricPrompt(MainActivity.this, executor, new androidx.biometric.BiometricPrompt.AuthenticationCallback() { im getting "'BiometricPrompt' has private access in 'android.hardware.biometrics.BiometricPrompt'".
At final BiometricPrompt.PrompInfo prompInfo = BiometricPrompt.PrompInfo.Builder() im getting "cannot resolve PromptInfo"
And finally at biometricPrompt.authenticate(prompInfo); im getting "cannot resolve method '(authenticate(BiometricPrompt.PrompInfo))'"

Sorry for the inconvinience, i fount out what was wrong, instead of using:
import android.hardware.biometrics.BiometricManager;
import android.hardware.biometrics.BiometricPrompt;
use
import androidx.biometric.BiometricManager;
import androidx.biometric.BiometricPrompt;
in the build.gradle i used
implementation 'androidx.biometric:biometric:1.2.0-alpha04'
But this can depend on new updates, so try with the newer ones also:
https://mvnrepository.com/artifact/androidx.biometric/biometric?repo=google
And finally i had PrompInfo instead of PromptInfo
This other post really helped me:
Android BiometricPrompt: Cannot resolve symbol PromptInfo

Related

Alarm notification is not working for API>=26 android emulators

I am trying to send a notification that appears under the top navigation bar as follows:
My codes work for android emulator with API < 26, but they do not work for android emulator with API >= 26. I found that it is because I need to add channel, so I did. However, although the codes compile well and go through all loops, and there is no error in logcat. Still, the notification does not appear on the screen.
Any help will be greatly appreciated. FYI, here is my FragmentAlarm.java:
package com.example.dailybible3;
import android.app.AlarmManager;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import androidx.annotation.RequiresApi;
import androidx.core.app.NotificationCompat;
import androidx.fragment.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.Toast;
import java.util.Calendar;
public class FragmentAlarm extends Fragment {
private View view;
private CheckBox box_sun, box_mon, box_tue, box_wed, box_thu, box_fri, box_sat;
private CheckBox order_history, order_bible;
private CheckBox ninety_days, one_year;
private Button btn_save;
public static FragmentAlarm newInstance() {
FragmentAlarm fragmentAlarm = new FragmentAlarm();
return fragmentAlarm;
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
//inflate the layout for this fragment
view = inflater.inflate(R.layout.fragment_alarm, container, false);
/* start of set notification */
createNotificationChannel();
/* end of set notification */
//save button
btn_save = (Button) view.findViewById(R.id.save_button);
btn_save.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
String message = "This is a notificaiton example";
NotificationCompat.Builder builder = new NotificationCompat.Builder(
getContext(), "notifyBibleVerse"
)
.setSmallIcon(R.drawable.ic_bible_english)
.setContentTitle("Today's Verse")
.setContentText("Genesis 1 - 3")
.setPriority(NotificationCompat.PRIORITY_DEFAULT);
/* start of setting notification */
Intent intent = new Intent(getActivity(), MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.putExtra("message", message);
PendingIntent pendingIntent = PendingIntent.getBroadcast(getActivity(), 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
builder.setContentIntent(pendingIntent);
NotificationManager notiifcationManager = (NotificationManager) getActivity().getSystemService(
Context.NOTIFICATION_SERVICE
);
notiifcationManager.notify(0, builder.build());
toastMessage("Alarm is set!");
}
});
return view;
}
private void createNotificationChannel() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
CharSequence name = "DailyBibleChannel";
String description = "Channel for Bible Verse Reminder";
int importance = NotificationManager.IMPORTANCE_DEFAULT;
NotificationChannel channel = new NotificationChannel("notifyBibleVerse", name, importance);
channel.setDescription(description);
}
}
private void toastMessage(String message){
Toast.makeText(getActivity(), message, Toast.LENGTH_SHORT).show();
}
}
I am still not sure how to solve the above problem, but I found the working code from here: https://codinginflow.com/tutorials/android/alarmmanager. The major difference I see is that the working code has AlarmManager. Hope this help someone who is having troubles to set a notification.

trying to make a login page by using retrofit library and rest API in android studio with login API but login failed

package com.example.androidapp;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import com.google.android.material.textfield.TextInputEditText;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
public class Login extends AppCompatActivity {
EditText username, password;
Button btnLogin;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
username = findViewById(R.id.editTextEmail);
password = findViewById(R.id.editTextPassword);
btnLogin = findViewById(R.id.buttonLogin);
btnLogin.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if(TextUtils.isEmpty(username.getText().toString()) || TextUtils.isEmpty(password.getText().toString())){
Toast.makeText(Login.this,"Username / Password Required", Toast.LENGTH_LONG).show();
}else{
//proceed to login
login();
}
}
});
}
public void login(){
LoginRequest loginRequest = new LoginRequest();
loginRequest.setUsername(username.getText().toString());
loginRequest.setPassword(password.getText().toString());
Call<LoginResponse> loginResponseCall = ApiClient.getUserService().userLogin(loginRequest);
loginResponseCall.enqueue(new Callback<LoginResponse>() {
#Override
public void onResponse(Call<LoginResponse> call, Response<LoginResponse> response) {
Log.d("#sid","res"+response.code());
if(response.isSuccessful()){
Toast.makeText(Login.this,"Login Successful", Toast.LENGTH_LONG).show();
final LoginResponse loginResponse = response.body();
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
startActivity(new Intent(Login.this,Permission2.class).putExtra("data",loginResponse.getUsername()));
}
},700);
}else{
Toast.makeText(Login.this,"Login Failed", Toast.LENGTH_LONG).show();
}
}
#Override
public void onFailure(Call<LoginResponse> call, Throwable t) {
Toast.makeText(Login.this,"Throwable "+t.getLocalizedMessage(), Toast.LENGTH_LONG).show();
}
});
}
}
Whenever I am trying to make a login page for my application, its showing login failed and its not passing intent as well as unable to pass the data on 2nd page of my application. I am helpless and not getting any idea to resolve this issue.
I request you to kindly check my above coding and let me know if I have done anything wrong.
First of all, please reformat the pasted code to be more readable.
Second of all - did you add Internet permission to AndroidManifest file?
Third of all - you can print what's wrong to log, for example:
Log.d("LoginError", "${response.errorBody.string()}");
Also did you follow some kind of tutorial on how to use Retrofit? Maybe follow some easy tutorial to get idea how to work this out :)

android studio error: reached end of file while parsing

package com.example.vicky.videostory;
import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.MediaController;
import android.widget.VideoView;
public class MainActivity extends AppCompatActivity {
Button btnPlay;
VideoView myvideo;
MediaController mediaController;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnPlay=(Button) findViewById(R.id.btn_Play);
myvideo=(videoView) findviewById(R.id.videoPlayer);
mediaController = new MediaController( this) ;
btnPlay.setOnClickListner(new View.OnClickListener() {
#Override
public void onClick(View view) {
String videoPath = "android.resource//com.example.vicky.videostory/"+R.raw.technology;
Uri uri=Uri.parse(videoPath);
myvideo.setVideoURI(uri);
myvideo.setMediaController(mediaController);
mediaController.setAnchorView(myvideo);
myvideo.start();
}
}
);
}
error: reached end of file while parsing
I think its just errors in brackets. Because when I click on an error it points towards brackets and says unexpected error.
this is the error please check whats wrong
You have problem in your syntax correct it first and then Rebuild Project also remember that follow the basics properly before doing code.

Android Studio - Creating a Loading/Splash Screen for WebView

I have followed through this tutorial (I am completely new to Android Studio): Loading/Splash Screen Tutorial
and I am unsure why my code isn't working correctly.
I get the loading circle come up on the app I have created, but it does not go away when the app loads the webpage.
I have gone over my code a few times now and I cannot see any errors (obviously there are some somewhere!) Just seems as if it isn't recognising that my webpage has loaded within the app.
Here is my code from my MainActivity.java file:
import android.net.http.SslError;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.webkit.SslErrorHandler;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.graphics.Bitmap;
import android.view.View;
import android.widget.ProgressBar;
public class MainActivity extends AppCompatActivity {
String ShowOrHideWebViewInitialUse = "show";
private WebView myWebView;
private ProgressBar spinner;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myWebView = (WebView) findViewById(R.id.webView);
spinner = (ProgressBar)findViewById(R.id.progressBar1);
myWebView.setWebViewClient(new CustomWebViewClient());
WebSettings webSettings = myWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
myWebView.getSettings().setDomStorageEnabled(true);
myWebView.getSettings().setDatabaseEnabled(true);
myWebView.getSettings().setMinimumFontSize(1);
myWebView.getSettings().setMinimumLogicalFontSize(1);
myWebView.setWebChromeClient(new WebChromeClient());
myWebView.loadUrl("https://node-red-test.ftp21.net:1024/ui");
myWebView.setWebViewClient(new WebViewClient() { #Override public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error){ handler.proceed(); } });
}
// This allows for a splash screen
// (and hide elements once the page loads)
private class CustomWebViewClient extends WebViewClient {
#Override
public void onPageStarted(WebView myWebView, String url, Bitmap favicon) {
// only make it invisible the FIRST time the app is run
if (ShowOrHideWebViewInitialUse.equals("show")) {
myWebView.setVisibility(myWebView.INVISIBLE);
}
}
#Override
public void onPageFinished(WebView view, String url) {
ShowOrHideWebViewInitialUse = "hide";
spinner.setVisibility(View.GONE);
view.setVisibility(myWebView.VISIBLE);
super.onPageFinished(view, url);
}
}
#Override
public void onBackPressed() {
if (myWebView.canGoBack()) {
myWebView.goBack();
} else {
super.onBackPressed();
}
}
}
I am currently running Android Studio version 2.1.2 - due to problems with later versions that I came across when following another tutorial.
Thanks,

MediaPLayer not stopping

I dont understand how to actually get this to work. My code is;
package com.example.user.medialert;
import android.app.Activity;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import java.io.IOException;
/**
* Created by User' on 9/24/2016.
*/
public class alert extends Activity {
final MediaPlayer Siren = new MediaPlayer() ;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.alert);
MediaPlayer Siren = MediaPlayer.create(this, R.raw.alertsiren);
try {
Siren.prepare();
} catch (IOException e) {
e.printStackTrace();
}
Siren.start();
}
public void onClickOk1(View view) {
Siren.stop();
Intent intent = new Intent(alert.this, counter.class);
startActivity(intent);
}
}
The onClick changes the activity as it is supposed to however when I add the Siren.Stop(); code, 'Siren' is invalid, the error being cannot resolve symbol.
I have tried
MediaPlayer Siren = new MediaPlayer();
above the onCreate method, allowing siren to be used in the onClick, but it still doesn't stop playing the sound.
Any ideas?
EDIT
Added to show the code I have updated attempting to make final, and this now crashes the app.
Highlighting Siren under the onCreate method shows that the program isn't linking the Siren in the Siren.stop(); call

Resources