Shared Preferences deleted automatically - android

I'm developing an application in which I'm using a background service, and I save my data to shared preferences (as I know Android can kill services whenever there's low memory state). It works fine, and I periodically save my data to preferences. But as a rare scenario, I noticed as Android killed my service and restarted it (there was a gap of around 20-30 seconds between this), in this interval my preferences got automatically cleared. I checked up the dumpstate to infer the reason behind this, but I cannot understand why this happens.
Here's the relevant part of logs, which indicate data clearance from preferences:
06-25 17:30:34.848 2452 2966 V ApplicationPolicy: isApplicationStateBlocked userId 0 pkgname com.example.gaurav
06-25 17:30:34.863 2452 2966 D EnterpriseDeviceManager: ContainerId: 0
06-25 17:30:34.863 2452 2966 V ApplicationPolicy: isApplicationStateBlocked userId 0 pkgname com.example.gaurav
06-25 17:30:34.863 2452 2966 D PackageManager: START CLEAR APPLICATION USER DATA: observer{1126181288}
06-25 17:30:34.863 2452 2966 D PackageManager: pkg{<packageName>}
06-25 17:30:34.863 2452 2966 D PackageManager: user{0}
06-25 17:30:34.898 2831 2831 D ContainerEventsRelayManager: <<< Intent action is >>> : android.intent.action.PACKAGE_RESTARTED
06-25 17:30:34.898 2831 2831 D ContainerEventsRelayManager: <<< Intent data is >>> : package:com.example.gaurav
06-25 17:30:34.898 2695 2695 D ContexualWidgetMonitor: id = 0
06-25 17:30:34.898 2695 2695 D ContexualWidgetMonitor: action =android.intent.action.PACKAGE_RESTARTED
06-25 17:30:34.898 2695 2695 D ContexualWidgetMonitor: pkg =null
06-25 17:30:34.898 2695 2695 D ContexualWidgetMonitor: mCheckMissedEvent =false
06-25 17:30:34.903 2831 2831 D ContainerEventsRelayManager: <<< Intent action is >>> : android.intent.action.PACKAGE_DATA_CLEARED
06-25 17:30:34.903 2831 2831 D ContainerEventsRelayManager: <<< Intent data is >>> : package:com.example.gaurav
06-25 17:30:34.908 2452 2580 I PackageManager: remove MCS_UNBIND message and Posting MCS_UNBIND 10 secs later
Though this scenario didn't occur again, but I'd really like to know what's happening behind the scenes, and why does it happen.
I've searched other similar threads here, but they indicate the scenario where null key is inserted into preferences, so I guess this is entirely different.
Any help would be appreciated. Also, as an information, this problem occurred on Samsung Galaxy Note 2 device.

this happens because you are writing the Preferences in a background thread.
It is a known issue that when two threads access the same preferences file, the preferences get deleted.
You should not use SharedPreferences with processes, instead use a Database.
Here`s another SO question with the same problem.
I am going through the exact same problem, and I'm going for SQLite.

Related

Android camera with api lvl33 and package name lenght > 30

I have a strange issue with the Android camera on Android 13.
My Application has a package name with a length that's greater than 30.
Whatever I do, e.g com.google.mlkit.barcode.Barcode , or creating a surface with androidx.camera, its always the same.Everything works fine until Android 12 and does not work with android 13.
But When I use a Flavour with a shorter package name (30 oder less), it works.
I reported this issue to google, but changing my flavours to shorter ones is not really an option in a productiv running app, does anyone have any idea how to avoid this?
I already updated target ad compiled sdk to 33 and use the newest stable versions of all implementations.
I have the following output in my logcat, if i start a surface on Android 13 with a long package name and the sreec stays just black:
CameraRepository de....Sass.dimadimadimadimadimadima D Added camera: 1
Camera2CameraInfo de....Sass.dimadimadimadimadimadima I Device Level: INFO_SUPPORTED_HARDWARE_LEVEL_3
CameraValidator de....Sass.dimadimadimadimadimadima D Verifying camera lens facing on SCW_sprout, lensFacingInteger: null
[...]
DeferrableSurface de....Sass.dimadimadimadimadimadima D Surface created[total_surfaces=2, used_surfaces=0](androidx.camera.core.impl.ImmediateSurface#ce8aba5}
DeferrableSurface de....Sass.dimadimadimadimadimadima D Surface created[total_surfaces=3, used_surfaces=0](androidx.camera.core.impl.ImmediateSurface#523af88}
Camera2CameraImpl de....Sass.dimadimadimadimadimadima D {Camera#b0329a4[id=0]} Use case ImageCapture:androidx.camera.core.ImageCapture-f64980f6-eaa9-435a-819f-8c8db175888e ACTIVE
PreviewView de....Sass.dimadimadimadimadimadima D Surface requested by Preview.
UseCaseAttachState de....Sass.dimadimadimadimadimadima D Active and attached use case: [] for camera: 0
Camera2CameraImpl de....Sass.dimadimadimadimadimadima D {Camera#b0329a4[id=0]} Use case Preview:androidx.camera.core.Preview-a1671e1e-e37b-4ab4-b0ad-8cd7ee2a695e ACTIVE
Camera2CameraImpl de....Sass.dimadimadimadimadimadima D {Camera#b0329a4[id=0]} Resetting Capture Session
Camera2CameraImpl de....Sass.dimadimadimadimadimadima D {Camera#b0329a4[id=0]} Releasing session in state INITIALIZED
Camera2CameraImpl de....Sass.dimadimadimadimadimadima D {Camera#b0329a4[id=0]} Attempting to force open the camera.
CameraStateRegistry de....Sass.dimadimadimadimadimadima D tryOpenCamera(Camera#b0329a4[id=0]) [Available Cameras: 1, Already Open: false (Previous state: null)] --> SUCCESS
CameraStateRegistry de....Sass.dimadimadimadimadimadima D Recalculating open cameras:
Camera#b0329a4[id=0] OPENING
Camera#49590e[id=1] UNKNOWN
Open count: 1 (Max allowed: 1)
Camera2CameraImpl de....Sass.dimadimadimadimadimadima D {Camera#b0329a4[id=0]} Opening camera.
Camera2CameraImpl de....Sass.dimadimadimadimadimadima D {Camera#b0329a4[id=0]} Transitioning camera internal state: INITIALIZED --> OPENING
CameraStateMachine de....Sass.dimadimadimadimadimadima D New public camera state CameraState{type=OPENING, error=null} from OPENING and null
CameraStateMachine de....Sass.dimadimadimadimadimadima D Publishing new public camera state CameraState{type=OPENING, error=null}
UseCaseAttachState de....Sass.dimadimadimadimadimadima D All use case: [androidx.camera.core.ImageCapture-f64980f6-eaa9-435a-819f-8c8db175888e89172630, androidx.camera.core.ImageAnalysis-98e2e014-5983-45be-9438-697f6ebd4550206087601, androidx.camera.core.Preview-a1671e1e-e37b-4ab4-b0ad-8cd7ee2a695e2326616] for camera: 0
libc de....Sass.dimadimadimadimadimadima W Access denied finding property "persist.vendor.camera.privapp.list"
CameraService pid-1414 I CameraService::connect call (PID -1 "de.supinfo.FISass.dimadimadimadimadimadima", camera ID 0) and Camera API version 2
CameraService pid-1414 I [hmdcb] CameraService::makeClient call (PID 25019 "de.supinfo.FISass.dimadimadimadimadimadima", camera ID 0) and Camera API version 2, g_isCTS 0, g_isCTS_V 0,
Camera2ClientBase pid-1414 I Camera 0: Opened. Client: de.supinfo.FISass.dimadimadimadimadimadima (PID 25019, UID 10007)
libc de....Sass.dimadimadimadimadimadima W Access denied finding property "vendor.camera.aux.packagelist"
CameraManagerGlobal de....Sass.dimadimadimadimadimadima E Camera 5 is not available. Ignore physical camera status change
libc de....Sass.dimadimadimadimadimadima W Access denied finding property "vendor.camera.aux.packagelist"
libc de.supinfo.FISass.pct W Access denied finding property "vendor.camera.aux.packagelist"`

IntentService scheduled by AlarmManager sometimes die

I have an app that every minute launches an IntentService that gets the UsageStats and UsageEvents on this period.
I used setExact() method of AlarmManager to set the moment that the IntentService is executed and on the onCreate method of the IntentService i call again to setExact() to schedule the execution of the IntentService the next minute. I did this because the method setRepeating() of AlarmManager didn't perform with accuracy on one minute periods. The code i use is:
alarmM.setExact(AlarmManager.ELAPSED_REALTIME,SystemClock.elapsedRealtime()+60000,alarmAppIntent);
This performs correctly most of times and when the device is charging. However, when the device is in normal use sometimes the service stops repeating every minute (i create a notification everytime the service executes to see if the service continues executing) and when the IntentService stops repeating Logcat says:
03-02 12:16:02.221 1173 12379 I ActivityManager: Process com.app.usage (pid 6390) has died
03-02 12:16:02.222 1173 1423 D DisplayManagerService: Display listener for pid 6390 died.
03-02 12:16:02.222 1173 12379 D ActivityManager: SVC-handleAppDiedLocked: app = ProcessRecord{73a4530 6390:com.app.usage/u0a251}, app.pid = 6390
03-02 12:16:02.222 1173 12379 D ActivityManager: cleanUpApplicationRecord -- 6390
03-02 12:16:02.222 1173 12379 D ActivityManager: SVC-mBroadcastQueues: com.android.server.am.BroadcastQueue#e800e85
03-02 12:16:02.222 1173 12379 D ActivityManager: SVC-mBroadcastQueues: com.android.server.am.BroadcastQueue#d0e4da
03-02 12:16:02.223 2049 2306 E WtProcessController: Error pid or pid not exist
03-02 12:16:02.315 1173 1378 D PerfServiceManager: [PerfService] MESSAGE_USER_DISABLE: 92
03-02 12:16:02.315 1173 1378 I libPerfService: [perfScnDisable] scenario:92
03-02 12:16:02.315 1173 1378 I libPerfService: 92: set max: -1, 0, -1,
03-02 12:16:02.316 1173 1378 I libPerfService: 92: set freq: -1, -1, 933000,
03-02 12:16:02.316 1173 1378 I libPerfService: 92: set gpu level: 0
03-02 12:16:02.447 3149 3166 I PerfService: PerfServiceNative_userEnableTimeoutMs:30, 1050
03-02 12:16:02.448 3149 3166 I PerfService: PerfServiceNative_notifyDisplayType:2
The device i am using is a Xiaomi Redmi Note 4 Pro with MIUI 9.2 (Android 6.0). I've tried to set no battery economizer to the app but the problem continues.
Is there some way to stop the app dying using other libraries or configuration?

How can I get a Bluetooth keyboard input event in Android?

I would like to give specific events when typing Bluetooth keyboard.
My app is always running as a service.
When you input a key with Bluetooth keyboard, the following log is recorded.
/? I / WCNSS_FILTER: do_write: IBS write: fc
/? I / WCNSS_FILTER: Direction (1): bytes: 19: bytes_written: 19
/? D / InputReader: Input event (15): value = 1 when = 586669729008000
/? I / InputDispatcher: Delivering key to (1099): action: 0x0 (0)
/? D / ViewRootImpl: ViewPostImeInputStage processKey 0
/? D / SecContentProvider2: query (), uri = 15 selection = getToastEnabledState
/? D / SecContentProvider2: query (), uri = 15 selection = getToastShowPackageNameState
Is there a way to get the call to 'InputReader: Input event' from the service?
or should I use another method?

Screen light up slowly due to low priority of systemserver

When I installed 30 more apps on my android devices, the screen light up time expires 3s.
Using adb shell ps -t -p to checkout the priority of threads:
USER PID PPID VSIZE RSS PRIO NICE RTPRI SCHED WCHAN
PC NAME system 553 1 6964 856 20 0 0 0
poll_sched 00000000 S /system/bin/imsdatadaemon
system 893 380 1229888 92080 18 -2 0 0 SyS_epoll_ 00000000 S system_server
u0_a94 8515 380 1199228 38548 12 -8 0 0 SyS_epoll_ 00000000 S com.baidu.netdisk
u0_a97 10901 380 848872 98740 12 -8 0 0 SyS_epoll_ 00000000 Scom.hiapk.marketpho
Nice value(priority) of many apps are higher than system_server, which prepares the showing interface when light up screen. I think this result in system_server taking more time to light up screen. In logcat:
01-04 08:02:01.733 893 1276 I DisplayPowerController: Unblocked screen on after 2740 ms
To solve the problem, I want to improve the priority of system_server(original it is THREAD_PRIORITY_FOREGROUND), will any things goes wrong in this way?
public final class SystemServer {
private void run() {
......
// Ensure binder calls into the system always run at foreground priority.
BinderInternal.disableBackgroundScheduling(true);
// Prepare the main looper thread (this thread).
android.os.Process.setThreadPriority(
android.os.Process.THREAD_PRIORITY_URGENT_DISPLAY);
android.os.Process.setCanSelfBackground(false);
Looper.prepareMainLooper();
// Initialize native services.
System.loadLibrary("android_servers");
....
}
}

Log Cat Running Continuously

I am getting infinite number of lines in the log cat. Even if i am not running the program, the messages are getting displayed infinitely. I even reopened the eclipse.
But, I dont know, what is the reason. The memory in my C drive is 1.7 gb left. I hope this wont be a problem. Can you please tell me what the error could be.
This is the verbose logcat.
Log Cat Error :
DCD ON
DCD ON
DCD ON
D/Tethering(373): interfaceLinkStateChanged wlan0, true
interfaceStatusChanged wlan0, true
DCD ON
DCD ON
E/AlarmManagerService(373): android_server_AlarmManagerService_set to type=3, 248683.937000000
V/AlarmManager(373): waitForAlarm result :8
V/AlarmManager(373): ClockReceiver onReceive() ACTION_TIME_TICK
E/AlarmManagerService(373): android_server_AlarmManagerService_set to type=3, 248542.915000000
W/AlarmManager(373): FACTORY_ON= 0
D/STATUSBAR-Clock(492): onReceive() - ACTION_TIME_TICK
D/ClockWidget(373): refreshTime()
TTS Message = It is 9 : 30 AM, Monday, February 24
DCD ON
D/BatteryService(373): update start
level:100 scale:100 status:5 health:2 present:true voltage: 4313 temperature: 230 technology: Li-ion AC powered:false USB powered:true icon:17303181 invalid charger:0
02-24 09:30:04.331: D/ThermistorObserver(373): mBatteryBRForAMOLED : onReceive() : newTemperature : 230
updateLocked, mLastBroadcastState =0 , oldAction = null
D/STATUSBAR-BatteryController(492): onReceive() - ACTION_BATTERY_CHANGED
onReceive() - level:100
onReceive() - plugged:2
D/STATUSBAR-BatteryController(492): onReceive() - BATTERY_STATUS_FULL:
D/BatteryService(373): turn on LED for fully charged
D/STATUSBAR-PhoneStatusBar(492): ACTION_BATTERY_CHANGED
D/STATUSBAR-PhoneStatusBar(492): NORMAL_BATTERY
D/PowerUI(492): getBatteryOnline : battery online file not found
E/SMD(105): DCD ON
I took a some lines from log cat and displayed it.
I am not running alarm manager anywhere. I have not used it anywhere.
You can filter the logcat messages using this statement in the filter textbox:
tag:MyTag
or
app:com.myapplicationpackage
I think these are the logs because you have set alarm in your device and for all the other basic apps that your device has by default. :)
Those logs are probably sent by the System..
Add a filter from the LogCat options so that you only see logs from a particular application or tag.. You can filter logs on the basis of type (i.e. error, warning, debug etc), Package name of your application, Tag of the log (the tag that you send in Log.d or Log.e etc) and even a regex.
Take a look at this answer for more details:
Filter tags in LogCat (in Android Eclipse Plug-In)

Categories

Resources