I am making a home application and I think that it will be suitable if I use a fullscreen and not show the status bar. So now I want to be able to open or expand the status bar with a button on the menu, similar to the way some default home applications have in the menu. I know its possible since the default home does it. Is this done through an intent? If so can I have the code for it. If not well then I would appreciate it if you guys showed me how. Thanks!
See if this helps and let me know...
try{
Object service = getSystemService("statusbar");
Class<?> statusbarManager = Class.forName("android.app.StatusBarManager");
Method expand = statusbarManager.getMethod("expand");
expand.invoke(service);
}
catch(Exception ex){
....
}
uses permission : "android.permission.EXPAND_STATUS_BAR";
The code below works for me:
boolean shown = true;
private void showHide() {
Window w = this.getWindow();
if(shown)
{
w.setFlags(0,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
}
else
{
w.setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
}
shown=!shown;
}
This one worked for me:
manifest:
<uses-permission android:name="android.permission.EXPAND_STATUS_BAR"/>
Code:
// https://gist.github.com/XinyueZ/7bad2c02be425b350b7f requires permission: "android.permission.EXPAND_STATUS_BAR"
#SuppressLint("WrongConstant", "PrivateApi")
#JvmStatic
fun setExpandNotificationDrawer(context: Context, expand: Boolean) {
try {
val statusBarService = context.getSystemService("statusbar")
val methodName =
if (expand)
if (Build.VERSION.SDK_INT >= 17) "expandNotificationsPanel" else "expand"
else
if (Build.VERSION.SDK_INT >= 17) "collapsePanels" else "collapse"
val statusBarManager: Class<*> = Class.forName("android.app.StatusBarManager")
val method: Method = statusBarManager.getMethod(methodName)
method.invoke(statusBarService)
} catch (e: Exception) {
e.printStackTrace()
}
}
However, someone wrote it doesn't work on all devices and Android versions (here), so I wrote a request to add official API for this, here.
Related
In my settings menu i have used a switch when i turn it on i want every button in-app to vibrate and when i turn the switch off i want every button in-the app to not vibrate.
The following is the code i have tried so far.
switch2!!.setOnCheckedChangeListener { buttonView, isChecked ->
if (switch2!!.isChecked) {
switch2!!.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
}
else {
vibrator.cancel()
}
}
You can do like this.
Using SharedPreference . https://developer.android.com/training/data-storage/shared-preferences
Once you stored the key, you can use the key at the other activity/fragment.
At onCreate , you get the value from SharedPreference.
You use variable to check it first before click the button.
If yes, then vibrate it. Refer to this. https://stackoverflow.com/a/52990380/9346054
**Don't forget to declare at AndroidManifest as the refer above.
<uses-permission android:name="android.permission.VIBRATE" />
Example.
Here at onCreate
val vibe = getSystemService(Context.VIBRATOR_SERVICE) as Vibrator
val isVibrate = sharedPref.getInt(getString(R.string.is_vibrate), defaultValue)
And then inside onClick button
if (isVisible) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
vibrator.vibrate(VibrationEffect.createOneShot(200, VibrationEffect.DEFAULT_AMPLITUDE))
} else {
vibrator.vibrate(200)
}
}
you need to add this permission first
<uses-permission android:name="android.permission.VIBRATE"/>
and next you can use this fun for vibrate mobile
fun vibratePhone(context: Context?, milliseconds: Int) {
val vibrator = context?.getSystemService(Context.VIBRATOR_SERVICE) as Vibrator
if (Build.VERSION.SDK_INT >= 26) {
vibrator.vibrate(
VibrationEffect.createOneShot(
milliseconds,
VibrationEffect.DEFAULT_AMPLITUDE
)
)
} else {
vibrator.vibrate(milliseconds)
}
}
I'm using an accessibility-service to check which application I'm using for-example I am using WhatsApp and I am trying to get its package name and compare it with my database if the package exists in my database then I'm closing the app so that I can't use this app, I just working on my idea. It's been days since I got this problem and I can't find the solutions to my problems can anyone help me. Thank you
The Problems that I am facing are listed below:
Accessibility service is automatically closing when I try to close my app. If I don't close the app it's working fine. For-example If I close my main application the service will turn off. Is there any way to stop that?
When I found the package name in my database what I'm doing is taking the user on the main screen "Home Page". That is not a good way to do it, can someone help me with this one. I need to properly block that app.
Is there any way to close the application which I am trying to block from recent apps?
My code is listed below:
Accessibility service class
override fun onServiceConnected() {
super.onServiceConnected()
val info = serviceInfo
info.eventTypes = AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED
info.flags = AccessibilityServiceInfo.DEFAULT
info.flags = AccessibilityServiceInfo.FLAG_INCLUDE_NOT_IMPORTANT_VIEWS
info.flags = AccessibilityServiceInfo.FLAG_REPORT_VIEW_IDS
info.flags = AccessibilityServiceInfo.FLAG_REQUEST_ENHANCED_WEB_ACCESSIBILITY
info.flags = AccessibilityServiceInfo.FLAG_RETRIEVE_INTERACTIVE_WINDOWS
info.feedbackType = AccessibilityServiceInfo.FEEDBACK_ALL_MASK
info.notificationTimeout = 100
info.packageNames = null
serviceInfo = info
}
override fun onAccessibilityEvent(event: AccessibilityEvent) {
if(AccessibilityEvent.eventTypeToString(event.eventType).contains("WINDOW")){
val nodeInfo = event.source
dfs(nodeInfo)
}
if (event.packageName != null) {
getAllBlockedApps(packageName = event.packageName.toString())
return
}
}
Block Application Code
val startMain = Intent(Intent.ACTION_MAIN)
startMain.addCategory(Intent.CATEGORY_HOME)
startMain.flags = Intent.FLAG_ACTIVITY_NEW_TASK
startMain.flags = Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS
startActivity(startMain)
I am trying to turn on hardware acceleration for my application
but I never seem to get a 'true' result from this function.
I tried all the methods in the Android Developers blog post about the
the tag android:hardwareAccelerated=true to the application
and even called
getWindow().setFlags(WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED,
WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED);
You should always call isHardwareAccelearted() after the view is attached to the window. I am not sure about your case as I can't see where actually you are calling it.
Also, the support level of various operations across API levels are mentioned here. I hope this helps.
You can also try this method to verify hardwareAcceleration.
public static boolean hasHardwareAcceleration(Activity activity) {
// Has HW acceleration been enabled manually in the current window?
Window window = activity.getWindow();
if (window != null) {
if ((window.getAttributes().flags
& WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED) != 0) {
return true;
}
}
// Has HW acceleration been enabled in the manifest?
try {
ActivityInfo info = activity.getPackageManager().getActivityInfo(
activity.getComponentName(), 0);
if ((info.flags & ActivityInfo.FLAG_HARDWARE_ACCELERATED) != 0) {
return true;
}
} catch (PackageManager.NameNotFoundException e) {
Log.e("Chrome", "getActivityInfo(self) should not fail");
}
return false;
}
For checking for View try this.
chat_wv.post(new Runnable() {
#Override
public void run() {
Log.e("isHardwareAccelerated", ""+chat_wv.isHardwareAccelerated());
}
});
i would like to use following code to turn off Button back that i got from https://stackoverflow.com/a/4937448/1218762
final Window win = getWindow();
final WindowManager.LayoutParams winParams = win.getAttributes();
winParams.flags |= WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON;
//set screen brightness to the lowest possible
winParams.screenBrightness = 0.01f;
if (Build.VERSION.SDK_INT < 8) {
// hack for pre-froyo to set buttonBrightness off
try {
Field buttonBrightness = winParams.getClass().getField(
"buttonBrightness");
buttonBrightness.set(winParams, 0);
} catch (Exception e) {
e.printStackTrace();
}
} else {
winParams.buttonBrightness = 0;
}
win.setAttributes(winParams);
Reference : visit Night Mode where you you can turn off Button Lights in Service , i know Services don't have Window but how is it possible ?
Thank You.
One of the simple way I would suggest you is to broadcast some kind of turning-off message from your Service to your Activity and perform the required actions inside onReceive method of BroadcastReceiver.
I'm developing an Android application that might be used at night. Therefor, I need to turn off the buttons' backlight. How can I do this? On my own phone the backlight turns off after a while, but on the Motorola Droid I don't think this happens.
I'm using a wakelock to keep the screen on. Should I use another flag or how can I do this?
PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
mWakeLock = pm.newWakeLock(PowerManager.SCREEN_DIM_WAKE_LOCK, WAKE_LOCK_TAG);
mWakeLock.acquire();
Thank you very much!
//Kaloer
There is a hack:
private void setDimButtons(boolean dimButtons) {
Window window = getWindow();
LayoutParams layoutParams = window.getAttributes();
float val = dimButtons ? 0 : -1;
try {
Field buttonBrightness = layoutParams.getClass().getField(
"buttonBrightness");
buttonBrightness.set(layoutParams, val);
} catch (Exception e) {
e.printStackTrace();
}
window.setAttributes(layoutParams);
}
I see that this is an old question that was mostly answered in a comment link, but to make it clear to anyone else who comes across this question, here's my own answer.
It's built-in since API 8. (doc)
float android.view.WindowManager.LayoutParams.buttonBrightness
This is a somewhat modified/simplified version of what I'm using in one of my apps (excluding irrelevant code). The inner class is required to prevent a crash at launch on older platforms that don't support it.
private void nightMode() {
Window win = getWindow();
LayoutParams lp = win.getAttributes();
if (prefs.getBoolean("Night", false))
changeBtnBacklight(lp, LayoutParams.BRIGHTNESS_OVERRIDE_OFF);
else changeBtnBacklight(lp, LayoutParams.BRIGHTNESS_OVERRIDE_NONE);
win.setAttributes(lp);
}
private void changeBtnBacklight(LayoutParams lp, float value) {
if (Integer.parseInt(Build.VERSION.SDK) >= 8) {
try {
new BtnBrightness(lp, value);
} catch (Exception e) {
Log.w(TAG, "Error changing button brightness");
e.printStackTrace();
}
}
}
private static class BtnBrightness {
BtnBrightness(LayoutParams lp, float v) {
lp.buttonBrightness = v;
}
}
AFAIK, there is no API to control the backlight of the buttons -- sorry!