I have an IntentService running background and listening incoming TCP messages. When a desired message has been received, the device should wake up and MainActivity be on top.
I can start a new MainActivity and wake up the device, but then I have many instances of it. The best solution would be sending a broadcast message to the MainActivity, but then I cannot wake up the screen.
I have these permissions set:
<uses-permission android:name="android.permission.DISABLE_KEYGUARD" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
I have this in my onResume()
final Window win = getWindow();
win.addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED |
WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD |
WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON |
WindowManager.LayoutParams.FLAG_ALLOW_LOCK_WHILE_SCREEN_ON);
I can call even my onResume() function, but the screen is black. When I set MainActivity as singleTop, starting new MainActivity keeps the screen black.
Any idea?
Related
im an amateur programmer and i need help, i want to make update location when i click my switch button and get the location send the address to the retrofit every 1 or 5 minutes. Most likely im gonna use the 1 minute first to test it and make textview to test it.
i already searching and copy-paste, testing many codes from the internet. But im confused, many of them either won't work when the screen off, expired, in kotlin or just sending notification once. Even one of them didn't get me the address to test it for textview in Main Activity. I also heard about the Android phone that won't allow your app to run in the background.
Then i heard about Work Manager from here: https://medium.com/#Lakshya_Punhani/background-services-running-forever-in-android-100261df88ee
But idk how to do it. Can anyone show the the simple but complete codes to do it, so i can call it from my Main Activity and will still running in the background unless i press again my switch button from main activity?
Handler to get location & address after every 1 or 5 seconds. Inside handler get location using FusedLocationProviderClient.
var currentLatitude : Double = 0.0
var currentLongitude : Double = 0.0
private FusedLocationProviderClient fusedLocationClient;
#Override
protected void onCreate(Bundle savedInstanceState) {
fusedLocationClient = LocationServices.getFusedLocationProviderClient(this);
Handler(Looper.getMainLooper()).postDelayed({
if (location != null) {
currentLatitude = location?.latitude!!
currentLongitude = location.longitude
}
}, 1000) //1000 milliseconds = 1 second
}
Also don't forget to add permissions in manifest.
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
I'm working with Firebase to send Push notification. When I have the app minimized and the screen lock and Data network on 3G, and batery at 90%, I can't receive notifications. I need to receive notifications while the screen is locked.
I noted this in the log at the moment to screen lock.
2019-06-13 16:40:35.901 2372-9542/? D/MC_BatteryStatsData: operate removing wakelock wake:com.exampleapp/.FCMListenerService
This is for a Moto C Android version 7.
I tried to add this permission to the manifest file.
<uses-permission android:name="android.permission.WAKE_LOCK"/>
Disabled battery saver. It is in the menu Settings -> Battery.
And here is my FCMInstanceIDListenerService.java
public class FCMInstanceIDListenerService extends FirebaseInstanceIdService {
private static final String TAG = "MyInstanceIDLS";
#Override
public void onTokenRefresh() {
String refreshedToken = FirebaseInstanceId.getInstance().getToken();
MetodosRepo.setPreference(getBaseContext(), Globals.DEVICE_TOKEN, refreshedToken);
}
}
And the FMCListenerService.java
public class FCMListenerService extends FirebaseMessagingService {
#Override
public void onMessageReceived(RemoteMessage message) {
Log.e(_TAG, "onMessageReceived");
}
}
I expect to receive a notification while the screen is locked.º
From Android 8.0 they put a background execution limit and hence you are not receiving the push notification when your application is in the background.
I would like to suggest implementing a JobScheduler which will connect to your server to see if there is any pending notification and create the notification alert accordingly. Here is a sample project on how you can implement your JobScheduler.
However, Android takes care of the push notifications and you will receive a notification after 3-5 minutes I think (not almost instantly when the service is in the foreground).
Several Android devices famously the Nexus series have a magnetic sensor that is NOT the android.hardware.Sensor.TYPE_MAGNETIC_FIELD used exclusively to turn the screen ON/OFF automatically using phone cases that have a small magnet inside them.
On my application, I'm using magnet detection using android.hardware.Sensor.TYPE_MAGNETIC_FIELD and the SensorManager to detect user touches the phone in a case with some magnets inside.
The code works without issues, the problem is that it's extremely easy on those devices to accidentally trigger the screen ON/OFF sensor thou, turning off the screen.
I've tried:
android:keepScreenOn="true" on the XML
using the permission android.permission.WAKE_LOCK and acquiring a wake-lock via PowerManager.
Both without success.
Is there a way to temporarily disable this sensor while my activity is resumed?
The keepScreenOn = true in manifest also doesn't work here as the action of hall effect sensor is same as pressing power button.
Here, I offer a work-around that I have tested. In this approach you display your activity regardless of hall-effect sensor locking the device and turning of the display.
Before beginning the unlocking action
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON |
WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD |
WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED |
WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);
credits for above code - https://stackoverflow.com/a/45951927/9640177
Also make sure to add Permissions
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>
<uses-permission android:name="android.permission.WAKE_LOCK" />
Setting these flags will make sure that your window will be visible above lockscreen.
Now, listen for ACTION_SCREEN_OFF intent. To do this add a broadcast receiver that listens for this intent broadcasted by the system. You need to register the receiver locally. (stackoverflow.com/a/9478013/9640177)
in manifest
<receiver android:name=".receiver">
<intent-filter>
<action android:name="android.intent.action.SCREEN_OFF"/>
</intent-filter>
</receiver>
Receiver class
public class receiver extends BroadcastReceiver {
MyActivity activity;
receiver(MyActivity activity){
this.activity = activity;
}
#Override
public void onReceive(final Context context, Intent intent) {
activity.turnOnScreen();
}
}
Inside activity
// register receiver
...
IntentFilter screenStateFilter = new IntentFilter();
screenStateFilter.addAction(Intent.ACTION_SCREEN_OFF);
receiver r = new receiver(MyActivity.this);
registerReceiver(r, screenStateFilter);
...
// function to turn on display
public void turnOnScreen(){
PowerManager powerManager = (PowerManager) getSystemService(Context.POWER_SERVICE);
PowerManager.WakeLock wakeLock = powerManager.newWakeLock(PowerManager.FULL_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP, "my:Tag");
wakeLock.acquire();
wakeLock.release();
}
credit - https://stackoverflow.com/a/44706632/9640177
This will turn the display on.
Don't forget to remove these flags after your activity has done the job and does not require to be in forefront.
getWindow().removeFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON |
WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD |
WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED |
WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);
This will make your window disappear from lock-screen and make the application function normally.
Hope this helps.
the most pragmatic solution to the problem might be ...
to disable the smart cover feature physically rather than by software.
eg. take a razorblade and tweezers and then remove the magnet.
... or simply get another cover without a magnet.
alternatively, file a bug against drivers/input/lid.c.
Hí,
I have an java application that makes use bluetooth to search for devices. When the user presses the native Bluetooth button to turn it off my application shows the bluetooth status offline. When the user presses the native Bluetooth button to turn it on, my application should go back to work but it does not. For my scan back to work I need to close and open my application.
How to fix this programmatically ?
Make sure you have the following permissions in your manifest:
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
You can enable bluetooth by using the following in an Activity or Service class:
BluetoothAdapter.getDefaultAdapter().enable();
If you are trying to listen for when Bluetooth is enabled, register a BroadcastReceiver listening for the BluetoothAdapter.ACTION_STATE_CHANGED action:
final int state = intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, BluetoothAdapter.STATE_OFF);
if (state == BluetoothAdapter.STATE_OFF) {
// Bluetooth is turned off
} else if (state == BluetoothAdapter.STATE_ON) {
// Do your scan
}
I have a broadcastreceiver class that starts a service. What I want to happen is when it starts the service it should also turn the device screen on. I haven't found a solution to implement. I tried extending the WakefulBroadcastReceiver but it won't open the screen, rather it keeps the device on if it was already on. Does anyone know of a solution?
try this method
public void turnScreenOn(Context context) {
PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
#SuppressWarnings("deprecation")
WakeLock wakeLock = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP | PowerManager.ON_AFTER_RELEASE, "MyWakeLock");
wakeLock.acquire();
wakeLock.release();
}
requires the permission
<uses-permission android:name="android.permission.WAKE_LOCK"/>