I want that when my application is running the power button (which upon pressing locks the screen & screen goes BLACK), should be disabled. So that the user cannot lock the screen.
I have noticed this thing in Samsung Galaxy S phone's Default Camera App. That's the same reason I am trying to do the same. I have also a Camera related App.
try this one
int val=android.provider.Settings.System.getInt(getContentResolver(),
SCREEN_OFF_TIMEOUT);
android.provider.Settings.System.putInt(getContentResolver(),
SCREEN_OFF_TIMEOUT, -1);
Toast.makeText(this, "Disabled Screen Timeout", Toast.LENGTH_LONG).show();
SharedPreferences.Editor editor = settings.edit();
editor.putInt("ScreenTimeout",val);
editor.commit();
}
} catch(Throwable er) {
Toast.makeText(this, "Error "+er.getMessage(), Toast.LENGTH_LONG).show();
}
that will set screen off
to disable key guard in android use
KeyguardManager keyguardManager = (KeyguardManager)getSystemService(Activity.KEYGUARD_SERVICE);
KeyguardLock lock = keyguardManager.newKeyguardLock(KEYGUARD_SERVICE);
lock.disableKeyguard();
and use permition
<uses-permission android:name="android.permission.DISABLE_KEYGUARD"/>
to keep screen alive
#Override
protected void onCreate(Bundle icicle) {
super.onCreate(icicle);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
}
I have reached this using root access.
sqlite3 /data/data/com.android.providers.settings/databases/settings.db
insert into secure (name, value) values ('lockscreen.disabled', 1);
or depending on version
sqlite3 /data/system/locksettings.db
insert into locksettings (name, value) values ('lockscreen.disabled', 1);
Related
Settings the developer's flag "Stay awake" might result in constant battery drain of your debugging device. So, how can you easily keep the device screen on ONLY while debugging?
try this
try
{
if( BuildConfig.DEBUG && Debug.isDebuggerConnected() )
{
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
}
else
{
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
}
}
catch(Exception e)
{
e.printStackTrace();
}
this is for vertical
//vetical position
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); // Make to run your application only in portrait mode
this is for screen always screen on
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
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!
I have a small app that i have been working on. I wanted a fresh install to run from on my Galaxy Nexus rooted running 4.2.1. I uninstalled the app, then tried to reinstall it via eclipse. It installs fine, no errors in logcat, console ect... but when you click to run the app, it just closes right on opening. I have tried this on 2 different phones, same thing.
On the Galaxy Nexus, if I restore my ROM back before I uninstalled, I can run it from eclipse all I want. It's only after uninstall and reinstall that I get the force close. It is installed on the ROM backup.
Any ideas?! How can I find out what is going on here?
EDIT:::
Activity is only declared once in manifest...
Tried changing the version number... no effect
added some Log.e() to the MainActivity:
protected void onCreate(Bundle savedInstanceState) {
Log.e(TAG,"STARTING APP");
super.onCreate(savedInstanceState);
// show no back arrow
Log.e(TAG,"AFTER ONCREATE");
setContentView(R.layout.activity_firstload);
Log.e(TAG,"AFTER SETCONTENTVIEW");
getPrefs();
Log.e(TAG,"GET PREFS");
finish();
Log.e(TAG,"AFTER FINISH");
}
The only tag that shows in the LogCat is "AFTER FINISH"
If I get rid of finish, the MainActivity stays open. All that main activity does is check for Preferences. Here is my GetPrefs()
private void getPrefs() {
// Get the xml/preferences.xml preferences
Log.e(TAG,"GET PREFS 1");
SharedPreferences prefs = PreferenceManager
.getDefaultSharedPreferences(getBaseContext());
lp = prefs.getString("defaultreport", "");
Log.v(TAG, lp);
if (lp.equals("esac")) {
// Toast.makeText(MainActivity.this, "ESAC", Toast.LENGTH_SHORT)
// .show();
Intent i = new Intent(MainActivity.this, ESACActivity.class);
startActivity(i);
} else if (lp.equals("sac")) {
// Toast.makeText(MainActivity.this, "SAC", Toast.LENGTH_SHORT)
// .show();
Intent i = new Intent(MainActivity.this, SACActivity.class);
startActivity(i);
} else if (lp.equals("msar")) {
// Toast.makeText(MainActivity.this, "MSAR", Toast.LENGTH_SHORT)
// .show();
Intent i = new Intent(MainActivity.this, MSARActivity.class);
startActivity(i);
}
}
AH>>> May have found something. The preferences initially are set to "" (null) so what would it load?! So I need a screen asking which they'd like to set on FIRST RUN I guess...
EDIT EDIT::: Needed to check for first run in Prefs...
if (prefs.getString("defaultreport", null) == null)
{
startActivity(new Intent(this, Preferences.class));
return;
}
Your string that decides what to run is set to nothing if there is no preference.
lp = prefs.getString("defaultreport", "");
And since you have no option for that case nothing will run and the initial activity will close without starting any other.
I am looking for how we change the vibrate settings in Jelly Bean. I found that all the pre-JB vibrate settings have been deprecated, but don't see any new AudioManager.[change the vibrate settings] code anywhere. There is a setting "Vibrate when ringing" which I would like to know how to play with.
Thanks for you help.
In android4.1 you can use this to control "vibrate & ringing"
Settings.System.putInt(mContentResolver, Settings.System.VIBRATE_WHEN_RINGING, enable ? 1 : 0);
From the Documentation:
This method is deprecated.
Applications should maintain their own vibrate policy based on current ringer mode that can be queried via getRingerMode().
Seems like Google wants Vibration settings to be handled by each app for itself on an app to app basis, adjusting it's settings by querying getRingerMode().
Settings.System.VIBRATE_WHEN_RINGING is declared with #hide annotation so you may have troubles to use it in Android Studio.
So maybe you have to replace Settings.System.VIBRATE_WHEN_RINGING by its value : "vibrate_when_ringing"
It is annoying that it is declared with the #hide annotation, because it means that Google don't want external developpers using it...
This issue has plagued me for a couple of days now. Finally got it working. Make sure that you have the right permissions as well.
uses-permission android:name="android.permission.WRITE_SETTINGS"/
protected void onCreate(Bundle savedInstanceState) {
audioManager = (AudioManager) getApplicationContext().getSystemService(Context.AUDIO_SERVICE);
}
Then in my on click listener I utilized the following:
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
if (vibrate == 1) {
try {
Settings.System.putInt(getContentResolver(), "vibrate_when_ringing", 1);
audioManager.setVibrateSetting(AudioManager.VIBRATE_TYPE_RINGER, AudioManager.VIBRATE_SETTING_ON); //Set it to never vibrate on ring:
audioManager.setVibrateSetting(AudioManager.VIBRATE_TYPE_NOTIFICATION, AudioManager.VIBRATE_SETTING_ON); //Set it to never vibrate on notify
boolean vibrateWhenRinging;
vibrateWhenRinging = (Settings.System.getInt(getContentResolver(), "vibrate_when_ringing") == 1);
Toast.makeText(MainActivity.this, Boolean.toString(vibrateWhenRinging), Toast.LENGTH_LONG).show();
} catch (Exception e) {
Toast.makeText(MainActivity.this, "System vibrate error", Toast.LENGTH_LONG).show();
}
} else {
try {
Settings.System.putInt(getContentResolver(), "vibrate_when_ringing", 0);
audioManager.setVibrateSetting(AudioManager.VIBRATE_TYPE_RINGER, AudioManager.VIBRATE_SETTING_OFF); //Set it to never vibrate on ring:
audioManager.setVibrateSetting(AudioManager.VIBRATE_TYPE_NOTIFICATION, AudioManager.VIBRATE_SETTING_OFF); //Set it to never vibrate on notify
boolean vibrateWhenRinging;
vibrateWhenRinging = (Settings.System.getInt(getContentResolver(), "vibrate_when_ringing") == 1);
Toast.makeText(MainActivity.this, Boolean.toString(vibrateWhenRinging), Toast.LENGTH_LONG).show();
} catch (Exception e) {
Toast.makeText(MainActivity.this, "System vibrate error in else statement", Toast.LENGTH_LONG).show();
}
}
}
I'm trying to create an option (using check box preferences) in my app for the user to disable and re-enable the lock screen. I use disableKeyguard() to disable the lock screen and it works flawlessly but I can't get reenableKeyguard() to work. The code is pretty simple, I don't know why it's not working.
public void onSharedPreferenceChanged(SharedPreferences taskprefs,
String tasks_pref) {
boolean skiplock = taskprefs.getBoolean("pref_skiplock", false);
boolean screentimeout = taskprefs.getBoolean("pref_screentimeout",
false);
skiplock(skiplock);
// Log.v("TaskActivity", "Skiplock value is " + skiplock);
// Log.v("TaskActivity", "ScreenTimeout value is " + screentimeout);
}
private void skiplock(boolean action) {
KeyguardManager keyguardManager = (KeyguardManager) getSystemService(Activity.KEYGUARD_SERVICE);
KeyguardLock lock = keyguardManager.newKeyguardLock(KEYGUARD_SERVICE);
//
if (action == true) {
lock.disableKeyguard();
Toast.makeText(getApplicationContext(), "Lockscreen Disabled",
Toast.LENGTH_SHORT).show();
}
//
else if (action==false) {
lock.reenableKeyguard();
Toast.makeText(getApplicationContext(), "Lockscreen Enabled",
Toast.LENGTH_SHORT).show();
}
}
The issue was that a new Keyguard object is created (declared) inside the skiplock() method every single time it is called. A simple solution is to declare the keyguard as a global object and refer to that single object so its status does not get reset when the method finishes executing.
In short, just use "KeguardLock lock" and "KeyguardManager keyguardManager" as global constructors.