How to Prompt User to Enter Pin in Android Lock Screen? - android

Android screen lock/ unlock programmatically
and
How to lock/unlock phone programmatically : Android
and many questions i have searched for answer but i didn't got the exact answer for my usage.
I would like to get a enter credentials or Enter pin page in Lock Screen Default System Lock Screen.
When we say Ok Google the Google will prompt to enter the credentials .
I need the same time. I m asking just to wipe out screen and enter credentials
Give me the answer which would for All android versions.

You can use the below code to open password/pin/pattern screen and validate user device credentials:
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP) {
KeyguardManager km = (KeyguardManager) getSystemService(KEYGUARD_SERVICE);
if (km.isKeyguardSecure()) {
Intent authIntent = km.createConfirmDeviceCredentialIntent(getString(R.string.dialog_title_auth), getString(R.string.dialog_msg_auth));
startActivityForResult(authIntent, INTENT_AUTHENTICATE);
}
}
and also implement onActivityResult's method to get the results in your case is successful or not.
// call back when password is correct
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == INTENT_AUTHENTICATE) {
if (resultCode == RESULT_OK) {
//do something you want when pass the security
}
}
}
You can check ref URL from here

Related

Android In app update immediate update is cancelable

Google Immediate app update, as far as I understand, shouldn't be cancelable, but the dialog as an X icon that cancels the update process.
Is there a way to remove the cancel option?
I'm using the latest play core version, 1.10.2
Thanks
At the beginning, I had the same understanding, but looking up in documentation, I wasn't able to find this information.
Both InApp update types are cancelable, and the only way to prevent the user to update before uses your app will be finish the app if onActivityResult() returns something diffrent than OK
The difference between Flexible and Immediate update is:
In Flexible update, if the user decide to update, it can uses the app during the download and when the download is finished, you need show some U.I to call the completeUpdate().
In Immediate update, if the user decide to update, it can't use uses the app, because a fullscreen progress dialog will be show. After the download is completed, the Android will install the new version even if your app is in background.
InAppUpdate Doc
You can handle the cancel button and back button click listener.
If the user clicks cancel or back button then
onActivityResult
callback is executed and you can reopen the app update screen if
resultcode == RESULT_CANCELED
#Override
protected void onActivityResult(int requestCode, int resultCode, #Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == APP_UPDATE_REQ_CODE) {
if (resultCode == RESULT_CANCELED) {
// App update is canceled by the user so re-open the in-app update screen.
// This block of code is executed if user clicks cancel button from the in-app update screen or press the hardware back button.
checkForAppUpdate();
} else if (resultCode == RESULT_OK) {
// App successfully updated to the latest version.
finish();
} else {
// Update fails then re-open the in-app update screen.
checkForAppUpdate();
}
}
}
private void checkForAppUpdate(){
if (this instanceof SplashActivity){
return;
}
if (appUpdateManager == null){
appUpdateManager = AppUpdateManagerFactory.create(this);
}
Task<AppUpdateInfo> appUpdateInfoTask = appUpdateManager.getAppUpdateInfo();
appUpdateInfoTask.addOnSuccessListener(appUpdateInfo -> {
if (appUpdateInfo.updateAvailability() == UpdateAvailability.UPDATE_AVAILABLE
&& appUpdateInfo.isUpdateTypeAllowed(AppUpdateType.IMMEDIATE)) {
startUpdateFlow(appUpdateInfo);
}
});
Official android document:

How can I skip an activity created by androids CAMERA_ACTIVITY?

I'm using androids default camera capture and then a crop library to take a photo then crop it to a square to be displayed on the next layout, the picture stored on the device and a record created on a database.
Intent camera_intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
camera_intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(image_file));
startActivityForResult(camera_intent, CAM_REQUEST);
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if(requestCode==2 || requestCode == 6709) {
if (resultCode == RESULT_CANCELED) {
} else if (resultCode == RESULT_OK) {
//Crop.pickImage(this);
if (requestCode == Crop.REQUEST_CROP && resultCode == RESULT_OK) {
//doSomethingWithCroppedImage(outputUri);
setResult(resultCode);
} else {
File cropme = new File(tempPicture[4]);
if (Build.VERSION.SDK_INT >= 24) {
try {
Method m = StrictMode.class.getMethod("disableDeathOnFileUriExposure");
m.invoke(null);
} catch (Exception e) {
e.printStackTrace();
}
}
new Crop(Uri.fromFile(cropme)).output(Uri.fromFile(cropme)).asSquare().start(this);
}
}
}
The problem is there is a picture confirmation page as shown below that's redundant and will save the user a lot of time if I'm able to remove it.
How can I go about either editing the default camera capture activity or using another camera template online?
I'd like to make process as efficient as possible so if there's a better way of doing this let me know.
The problem is there is a picture confirmation page as shown below that's redundant and will save the user a lot of time if I'm able to remove it.
Do not use ACTION_IMAGE_CAPTURE. Use the camera APIs directly (e.g., android.hardware.Camera, android.hardware.camera2.*) or via a library that wraps them (e.g., CameraKit-Android, Fotoapparat).
How can I go about either editing the default camera capture activity
There are ~10,000 Android device models. These ship with dozens, if not hundreds, of different camera apps. Plus, users install their own. Any of those can respond to ACTION_IMAGE_CAPTURE. Whether any of them have a confirmation screen is up to the developers of those apps, not you. If you want complete control over the camera experience, do not delegate photo-taking to ACTION_IMAGE_CAPTURE, but write your own camera code.

Android-TV speech recognition with manual input

I've implementing voice-search on my Android-TV app using the following small code
private void displaySpeechRecognizer() {
Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
startActivityForResult(intent, SPEECH_REQUEST_CODE);
}
#Override
public void onActivityResult(int requestCode, int resultCode,
Intent data) {
if (requestCode == SPEECH_REQUEST_CODE && resultCode == -1) {
List<String> results = data.getStringArrayListExtra(
RecognizerIntent.EXTRA_RESULTS);
String spokenText = results.get(0);
Toast.makeText(getActivity(), spokenText, Toast.LENGTH_LONG).show();
}
super.onActivityResult(requestCode, resultCode, data);
}
This is all working well and I get the result back for further processing.
The problem is that I also want to give the user the possibility to enter the search-string manually with the virtual keyboard.
In Googles own apps you do this be simply pressing RIGHT on the remote to give focus to the textbox after pressing the voice-search icon.
In my example above I can see the "built-in-textbox" when I press the search icon but if I try to navigate to it the search is interrupted and closed.
How do I access the search-textbox? This should cancel voice-input and bring up the keyboard instead, just like Play Store app.
Are you using Leanback support library for your Android TV app design?
I guess "Google Play Store app" and "YouTube app" are using BrowseFragment & SearchFragment for search. These Fragments are providing build-in search UI.
For the implementation, see Google's sample source code or SearchFragment – Android TV app Tutorial 12.

Use screen lock in my app

Is it possible to use the default security settings, which user has set to the phone, as a locking or login mechanism for my app too? I mean like when we reset the phone, it asks for phone password or pattern.
Is it possible the same way that I can use the default android password or pattern set by user to login to my app?
My goal is to bypass the developing effort and use some standard way of authentication without making user to remember another new password.
NOTE: I'm aware that I can lock the screen programmatically. But instead, I want to use the lock as a verification before performing any critical operation. (Just like how Settings ask for the password before resetting the the phone.)
Actually, there is an API to exactly that using the KeyguardManager.
First get a the Keyguard SystemService:
KeyguardManager km = (KeyguardManager)getSystemService(KEYGUARD_SERVICE);
And then request an authentication intent using:
Intent i = km.createConfirmDeviceCredentialIntent(title,description);
start this intent using startActivityForResult(Intent, int) and check for RESULT_OK if the user successfully completes the challenge.
This is for API level 21.
Previous versions might work with KeyguardLock.
I'm just following #agi with few enhancement,
public class MainActivity extends AppCompatActivity {
private static int CODE_AUTHENTICATION_VERIFICATION=241;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
KeyguardManager km = (KeyguardManager)getSystemService(KEYGUARD_SERVICE);
if(km.isKeyguardSecure()) {
Intent i = km.createConfirmDeviceCredentialIntent("Authentication required", "password");
startActivityForResult(i, CODE_AUTHENTICATION_VERIFICATION);
}
else
Toast.makeText(this, "No any security setup done by user(pattern or password or pin or fingerprint", Toast.LENGTH_SHORT).show();
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(resultCode==RESULT_OK && requestCode==CODE_AUTHENTICATION_VERIFICATION)
{
Toast.makeText(this, "Success: Verified user's identity", Toast.LENGTH_SHORT).show();
}
else
{
Toast.makeText(this, "Failure: Unable to verify user's identity", Toast.LENGTH_SHORT).show();
}
}
}
Update 2022,June:(minSdk : 24, targetSdk: 32,compileSdk : 32)
Code Enhancement to #Abhijit Kurane's reply...
Replace startActivityForResult with startActivityIfNeeded...(#startActivityForResult is deprecated as of now)
For Beginners : And put finish() inside the else statement of onActivityResult method for not letting the user to open the activity without verification!

Extending Android's Voice Search app

Is it possible to extend the Voice Search app? I know I can add a button in my own app to launch the voice recognition dialog, but I was wondering if I could extend the voice search app that launches automatically when you long press the physical "search" key.
send text to [contact] [message]
listen to [artist/song/album]
call [business]
call [contact]
send email to [contact] [message]
go to [website]
note to self [note]
navigate to [location/business name]
directions to [location/business name]
map of [location]
I'd basically like to add my own action to the above list.
Is this possible or will I have to create my own?
In a word, no. The app doesn't have the extension points you are looking for. You would have to write your own app entirely, which is something that others have done.
A simple method to Handle Voice Search
Step 1 Call this method on button click
public void startVoiceRecognition() {
Intent intent = new Intent("android.speech.action.RECOGNIZE_SPEECH");
intent.putExtra("android.speech.extra.LANGUAGE_MODEL", "free_form");
intent.putExtra("android.speech.extra.PROMPT", "Speak Now");
this.mContainerActivity.startActivityForResult(intent, 3012);
}
Step 2 Override onActivityResult method
# Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 3012 && resultCode == RESULT_OK) {
ArrayList < String > matches = data.getStringArrayListExtra("android.speech.extra.RESULTS");
String result= matches.get(0);
//Consume result
edittext.setText(result);
}
}
Thats all, DONE

Categories

Resources