Why Cameradevice null when accessing on foreground service - android

I have app that works fine in Android 10 but now I need to make it work on Android 11.
App uses broadcast receiver so it can be launched from background on notification widget/view
First issue was that when I run it on 11 I got android.hardware.camera2.CameraAccessException: CAMERA_DISABLED (1):
So I added permissions android:foregroundServiceType="camera" so I got it run at first place
Now when the app is in background and is activated from widget via broadcast message
the app doesn't activate. I get the the receiver message and when getting the Controller.getInstance(context)
and I check the context cameradevice it's null.
I found this https://developer.android.com/guide/components/foreground-services#access-restrictions
I am just guessing that if this is the case shouldn't there be some Permission Restricted Message instead of having the cameradevice is null.
And if this is the case that it now fails with access restrictions is there way to make it work.

Related

Why Android foreground service stops when I manually disable the location in Andriod settings?

I have written a foreground service in my app and I am doing some API operation every 5 minutes once. My app is mainly used for location tracking and it always needs location permission. If user disable the location for my app by any chance then I just want to create a local notification from my service. My app users mostly don't bring my app to foreground. So I want to check whether the location permission is enabled or not. If not then I just want to show a notification which tells the user that the app needs location permission. Now I just manually went to android settings and disabled the location permission for my app. I have two services running in my app. Suddenly both stopped working. I just want to know the reason why it stopped? Will it be stopped always until I enable the permission? What can I do to intimate my user to enable the permission? As my app always runs in background, I want to handle it in service. Please give your opinion to solve my issue.
NullPointerException: Attempt to read from field'LanguageInitBean$Data LanguageInitBean.data' on a null object reference
When I change the storage permission, I can't read the data stored in memory
Ask for help
public static class LanguageInitCache{
public static LanguageInitBean languageInitBean;
}
private void startToNext(String response) {
languageInitBean = GsonUtils.json2Bean(response, LanguageInitBean.class);
}

context.startActivity in broadcast receiver sometimes not working (targetSDK 19 on an SDK 27 device)

I call startActivity in an implicit broadcast receiver's onReceive function under certain very limited circumstances.
Intent transactionIntent = new Intent(context, TransactionActivity.class);
transactionIntent.putExtra(Constants.Extras.ORDER_ID, orderId);
transactionIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
context.startActivity(transactionIntent);
I have logs before this line and logs in the onCreate function of the activity. Some customers are reporting random instances of this activity not creating itself. I can see in the logs that startActivity is called and onCreate is not. One of them I know for sure is an Android 8.1 Oreo device. My target SDK is 19 (vast majority are old devices, this is not an app on the Play store). I can also tell the Background activity toggle in the system settings for the app is not switched off so that is not the problem.
What situations can this happen (extreme memory pressure?) and how can I capture it to verify what is going on?
EDIT:
The app is not crashing (no crashes logged for this version of the app, and it does nothing but setContentView before logging the activity creation). I don't think device sleep is an issue because I am targeting pre M, doze mode etc. doesn't come in to play. The user was also using the app a few seconds before this call to startActivity failed, and 30 seconds later they were using the app as well, so that window doesn't seem likely large enough to allow for any kind of sleep or wake lock issues. I also don't think the user could have managed to swipe the app out of memory between the app calling startActivity and onCreate, this is happening sporadically (for this one user is happened 1 out of 6 times).
The app is a for a busy, mains connected Point of Sale device.

Location in Foreground service with Background permission VS in app only permissions Android Q

I am verifying my location service compatibility with Android Q but I am a little unsure how my app is going to react since in my testing I have seen not difference when granting Background permission vs Only while app is running.
Coming off this statement from the Q migration documentation
An app is considered to be in the background unless one of its
activities is visible or the app is running a foreground service.
Since the location service is a foreground service does the difference in permission even matter in this case?
Well it's a bit of a tricky question.
When running location foreground service on Q you need "while app running" permission + to declare in the manifest this foreground service is of type location. Your app has no need of the background permission.
If your app do asks for background permission, the user can get suspicious and reject any location permission :( So it is not recommended to ask the user for permissions you don't need.
highly recommend to watch this video from the google IO: Updating Your Apps for Location Permission Changes in Android Q (Google I/O'19)
You can read more about t here: documantation
And you can see googles example project on GitHub for location updates on Q with foreground service here :LocationUpdatesForegroundService
You just need run foreground service instead of background. Otherwise your app will crash during background services start when there is no activity on the foreground

Avoid Screen Overlay Detected for service that uses SYSTEM_ALERT_WINDOW

My app main usage is overlay, the overlay is running from a service.
Android Security add the nice "Screen Overlay Detected"
I want to avoid "Screen Overlay Detected" when user tries to change permissions. so... I've add an AccessiblityService that detects:
if ( event.getPackageName().equals("com.google.android.packageinstaller") ){
stopService(myServiceIntent);
}
However, even now I see this message popping. (when my service is stopped...).
I saw Twilight does it without problem.
What am I missing?
p.s. - I've also tried building a signed apk but saw exact same behavior.
It seems I've been able to resolve this.
a) stopService isn't assured your service will be stopped.
as described here :
It will not be destroyed until all of these bindings are removed. See > the Service documentation for more details on a service's lifecycle.
b) I was able to kill my service by sending intent that called stopSelf().
However process killing/starting can be slow.
c) Best resolution: so it seems Android checks for view visibility. no need to kill services or do anything more complicated.
Current way I'm doing it:
- AccessibilityService (already used by my app) monitor "com.google.android.packageinstaller" though it can be refined to class: "com.android.packageinstaller.permission.ui.ManagePermissionsActivity"
Once detected in this class, we send Intent to "duck", and when we're out, we send another intent that we're back on.
The service handles those calls by:
[ourView].setVisibility(View.INVISIBLE); // when permission settings shown
[ourView].setVisibility(View.VISIBLE); // when normal flow
As long as Android 6.x is buggy on some devices where this "overlay alert" is displayed without any reason (on 2 to 5% of the devices according to my analytics data), the best solution is to avoid the whole permission process by defining the targetSdk to 22. Take care that you can't downgrade the target sdk for a new version or this will induce a INSTALL_FAILED_PERMISSION_DOWNGRADE error when the user updates requiring an unisntall/install of the app.

Run program immediately after booting in android?

I need to know why my app didn't run immediately after booting in android real phone? My app run but after a few second.
First, you should make sure that your app has been installed in phone memory for your receiver to receive boot completion broadcast, As this broadcast occurs before media scanning.
just add an attribute to manifest file's root element
installlocation = "internalOnly"
rest is same as explained above.

Categories

Resources