Keep screen off while receiving notification and device is locked - android

I googled a lot to manage notifications and I found that can be done using Notification Listener but I am stuck with the problem that is, I want to keep screen off (not event blinking of light) while receiving notification.
I used following code:
if (Build.VERSION.SDK_INT >= 23) {
if ((notificationManager != null) && !notificationManager.isNotificationPolicyAccessGranted()) {
Intent intent = new Intent(android.provider.Settings.ACTION_NOTIFICATION_POLICY_ACCESS_SETTINGS);
startActivityForResult(intent, RQS_ENABLE_DO_NOT_DISTURB_SCREEN_FLASH);
} else {
if (b) {
notificationManager.setInterruptionFilter(NotificationManager.INTERRUPTION_FILTER_NONE);
} else {
notificationManager.setInterruptionFilter(NotificationManager.INTERRUPTION_FILTER_ALL);
}
}
} else {
Toast.makeText(this, "Don't have notification policy permissions", Toast.LENGTH_LONG);
}
Can anyone give me idea to do this?
Your help would be appreciated. Thank you.

Related

Way to get current running foreground app/process

I am curious if there is any way available in Android OS to tell the app that an application is just launched. Basically, in my app I want to notify my app that an application is going to be launched or just launched. On that event, I want to perform some operations like block that application if user(using my application) has included that in block list.Any help or direction will be highly appreciated..
first Add
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (!Settings.System.canWrite(context)) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
Intent intent = new Intent(Settings.ACTION_MANAGE_WRITE_SETTINGS,
Uri.parse("package:" + getPackageName()));
startActivityForResult(intent, 200);
}
} else {
//Do work
}

un-snooze notification

I am using a notification listener to snooze unwanted notifications, but how do I un-snooze these once my app is no longer running? Currently they only way I have found to do this is the user must reboot their phone.
This is how I am snoozing the notification, I found out I can use getSnoozedNotifications to get a list of snoozed apps, but what is the command to un-snooze it?
snoozeNotification(sbn.getKey(), Long.MAX_VALUE - System.currentTimeMillis());
And this is how I get the snoozed apps, I just can't find anywhere that tells me the command to un-snooze it, I have even tried to set the duration to 0.
private void clearSnoozedNotifications()
{
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
StatusBarNotification sbns[] = getSnoozedNotifications();
for (StatusBarNotification sbn : sbns) {
try {
if (sbn == null) {
Log.e("Notification Listener", "sbn is null");
return;
}
Log.e("Notification Listener", "Starting: " + sbn.getKey());
snoozeNotification(sbn.getKey(), 0);
} catch (Exception e) {
Log.e("Notification Listener", "error: " + e.getMessage());
}
}
}
}
I have observed on Android 11 that you can restore the notification by calling snoozeNotification on the same key with a low duration number greater than zero.
For example I use the following:
snoozeNotification(key, 100L);
On earlier versions of Android the notifications will re-appear after a reboot when they have been snoozed, in Android 11 they won't.

Avoid screen lighting (wake up) when Notification received in lock mode in android

I am currently building an application, In which I want to avoid the screen lighting when I received any notification if my android mobile in lock state.
I am trying to find the solution but I couldn't get any way to do it.
I tried below code on switch.setOnCheckedChangeListener(this);
but it's not working.
if (Build.VERSION.SDK_INT >= 23) {
if ((notificationManager != null) && !notificationManager.isNotificationPolicyAccessGranted()) {
Intent intent = new Intent(android.provider.Settings.ACTION_NOTIFICATION_POLICY_ACCESS_SETTINGS);
startActivityForResult(intent, RQS_ENABLE_DO_NOT_DISTURB_SCREEN_FLASH);
} else {
if (b) {
notificationManager.setInterruptionFilter(NotificationManager.INTERRUPTION_FILTER_NONE);
} else {
notificationManager.setInterruptionFilter(NotificationManager.INTERRUPTION_FILTER_ALL);
}
}
} else {
Toast.makeText(this, "Don't have notification policy permissions", Toast.LENGTH_LONG);
}
Can anyone will help me to do this?
Thank you in advance.
Please add these lines to the starting of your Activity, before setContentView and after super.onCreate:
getWindow().addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED
+ WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED
+ WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD
+ WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON
+ WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);
You can avoid WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON from flags.

android airplane permission doesn't work properly - full example?

I've found a lot of examples here but no one works for me...I can't understand what's wrong.what I need is to enter the "airplane mode" in my app and I search for a full example.
What I have done is
1. request permission in manifest (I've seen I need "WRITE_SETTINGS" permission)
<uses-permission-sdk-m android:name="android.permission.WRITE_SETTINGS" />
2. in the code, when I want to change Airplane Mode, I check for permission
private void setAirplaneMode() {
boolean permission;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
permission = Settings.System.canWrite(this);
} else {
permission = ContextCompat.checkSelfPermission(this, android.Manifest.permission.WRITE_SETTINGS) == PackageManager.PERMISSION_GRANTED;
}
if (permission) {
//here I go to activate the airplane mode
setAirplaneModeIntent(true);
} else {
if (ActivityCompat.shouldShowRequestPermissionRationale(this, android.Manifest.permission.WRITE_SETTINGS)) {
//I need to explain why I want permission
//TODO
} else {
ActivityCompat.requestPermissions(this, new String[] android.Manifest.permission.WRITE_SETTINGS}, MY_PERMISSION_REQUEST_WRITE_SETTINGS);
}
}
3. I override the "onRequestPermissionsResult"
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResult) {
switch (requestCode) {
case MY_PERMISSION_REQUEST_WRITE_SETTINGS: {
if( grantResult.length > 0 &&
grantResult[0] == PackageManager.PERMISSION_GRANTED) {
setAirplaneModeIntent(true);
}
}
}
}
4. in the "setAirplaneModeIntent" I set the Airplane mode
private void setAirplaneModeIntent(boolean activateAirplaneMode) {
Settings.System.putInt(getContentResolver(), Settings.System.AIRPLANE_MODE_ON,
activateAirplaneMode ? 1 : 0);
Intent intent = new Intent(Intent.ACTION_AIRPLANE_MODE_CHANGED);
intent.putExtra("state", activateAirplaneMode);
sendBroadcast(intent);
}
when I debug, I enter in the "ActivityCompat.requestPermissions" code but when I reach the "onRequestPermissionsResult" the result is not PackageManager.PERMISSION_GRANTED.
What's wrong?
EDIT
I solve the "requestPermissions" part. For the "WRITE_SETTINGS" permission you need the ACTION_MANAGE_WRITE_SETTINGS intent.
See here:
https://developer.android.com/reference/android/provider/Settings.html#ACTION_MANAGE_WRITE_SETTINGS
https://developer.android.com/reference/android/Manifest.permission.html#WRITE_SETTINGS
http://stackoverflow.com/questions/39224303/write-settings-permission-not-granted-marshmallow-android
Anyway I got an error in the point 4, when I send the broadcast.
If I don't sent the broadcast, the "Settings.System.putInt" command seems doing nothing.
Someone can help me?
EDIT 2
I surrender. The only solution I have found that works is to open the network settings in this way
Intent intentAirplaneMode = new Intent(Settings.ACTION_AIRPLANE_MODE_SETTINGS);
intentAirplaneMode.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intentAirplaneMode);

Android: Creating a persistent event listener

I am trying to figure out how to implement an event listener (unsure if this is the proper term.) I want the service, once my app is launched, to listen for the phones power status. I am uncertain to as how android handles this situation so don't really know what to search for. I've been working with this code that uses a broadcast receiver:
BroadcastReceiver receiver = new BroadcastReceiver() {
public void onReceive(Context context, Intent intent) {
unregisterReceiver(this);
int plugged = intent.getIntExtra(BatteryManager.EXTRA_PLUGGED, -1);
if (plugged == BatteryManager.BATTERY_PLUGGED_AC) {
// on AC power
Toast.makeText(getApplicationContext(), "AC POWER", Toast.LENGTH_LONG).show();
} else if (plugged == BatteryManager.BATTERY_PLUGGED_USB) {
// on USB power
Toast.makeText(getApplicationContext(), "USB POWER", Toast.LENGTH_LONG).show();
startActivity(alarmClockIntent);
} else if (plugged == 0) {
Toast.makeText(getApplicationContext(), "On Battery", Toast.LENGTH_LONG).show();
} else {
// intent didnt include extra info
}
}
};
IntentFilter filter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
registerReceiver(receiver, filter);
The code works fine. When I open my app it will toast what the current status of the phone power is.
Here is what I am trying to do:
When the user launches the app, it is effectively turning on the service
The user can go about using the phone, but once it is plugged in, my service will catch that and use the code above
How do I adapt this code to achieve the objectives above?
You could keep the listener on for the battery status by removing the line
unregisterReceiver(this);
This way, the app will continue to listen to power status change in the background even though that the app is not running in the foreground. Note that at some point, you might still want to unregister your receiver. You probably want to allow the user to control that via settings.
One other note, your code contains starting activity in the receiver in below code:
else if (plugged == BatteryManager.BATTERY_PLUGGED_USB) {
// on USB power
Toast.makeText(getApplicationContext(), "USB POWER", Toast.LENGTH_LONG).show();
startActivity(alarmClockIntent);
}
If your activity is in the background then it can't start another activity. See this SO Question - how to start activity when the main activity is running in background?, the accepted answer has suggestion on how to handle situation that requires starting activity from the background

Categories

Resources