Android service ClassCastException when casting to custom application - android

I've noticed in Google Play Console crashes that I get ClassCastException when casting to a CustomApplication in a Service or BroadcastReceiver:
((CustomApplication) getApplication()).getDiComponent().inject(this);
The name is set in manifest:
<application
android:name=".CustomApplication"
I also tried using getApplicationContext() but got same result:
((CustomApplication) getApplicationContext()).getDiComponent().inject(this);
This code is used in onCreate() method of a Service or onReceive of a broadcast receiver.
I cannot reproduce this crash but I have quite a lot looking at numbers. Any idea why it happens and how I could fix it?
UPDATES
OS affected: from 6.0 and up
Devices: mostly ZTE phones, 70%, but some Samsung also

Related

Samsung 8+ does not receive any Activity Recognition events

I'm running the Google Codelab Android Activity Recognition API example on my Samsung 8+
https://codelabs.developers.google.com/codelabs/activity-recognition-transition/
And although the example starts fine on my phone, I do not get any events from the activity recognition API.
I first though that it might be a permission issue, but On Android 9 I could not find a permission setting for the ACTIVITY_RECOGNITION, this seems to be necessary for Android 10 onwards.
Could someone give a pointer what possible reasons might be that my Samsung 8+ is not getting any Activity Recognition events?
I have 2 Xiaomi phone that is running Android 9. 1 is Redmi Note 3 and is working . The other 1 is Pocophone and it is not working.
Then I tested with Samsung Galaxy Grand Prime Android 5. It is working. Now I am confused why some device it appears but some not appear.
Even registering broadcast receiver in Manifest file,you need to register in dynamically in Oreo+ otherwise it will not work.
Try this.Add this code in main activity or in startCommand in Service.It worked for me.I have tested this code on Android 10 too..worked perfectly.You dont need to register broadcast receiver in Manifest.
#Override
protected void onStart() {
super.onStart();
IntentFilter intentFilter=new IntentFilter(Constants.BROADCAST_DETECTED_ACTIVITY);
intentFilter.addCategory(Intent.CATEGORY_DEFAULT);
registerReceiver(broadcastReceiver,intentFilter);
}

Unable to start service in Android 6.0

I'm trying to run a service by using following code:
Intent i = new Intent(getApplicationContext(),MyService.class);
startService(i);
This code works fine on versions older than Android 6.0 Marshmallow. But here on Android 6.0, the service never gets created. startService() is returning null. I'm calling it from an Activity in the same package. My manifest is like:
<service
android:name="com.example.app2.MyService"
android:label="#string/app_name">
</service>
Also my application is not crashing, it's just the service is not starting (not created).
Once I was having the same problem. It was a very small and stupid mistake. I had given <service> tag outside <application> tag in manifest.xml. After moving it inside <application> tag service started successfully. Check for any typos in service name also. Hope it helps!
If you are accessing any device hardware features(Camera, Storage, Location etc) in the Service. You need to ask runtime permission in the Marshmallow devices. Otherwise, it will make a crash if your compile SDK version >=23. Please paste your Crash log or Service code for further clarification.
Please check the link to implement the feature:
https://developer.android.com/training/permissions/index.html
https://developer.android.com/training/permissions/requesting.html

android.provider.Settings.ACTION_BLUETOOTH_SETTINGS crashes on Samsung

Anybody have any idea why
Intent pairIntent = new Intent(android.provider.Settings.ACTION_BLUETOOTH_SETTINGS);
startActivityForResult(pairIntent, 0);
Crashes on all Samsung devices, but works fine on emulator, HTC, Sony, LG etc.
EDITED -----------------------------------
Turns out Samsung also requires BLUETOOTH_ADMIN in the manifest
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
If you have an app in production, you need to have something that will allow you to get crash logs. That could be the default stuff that you get from shipping through the Play Store, or an open source solution like ACRA, or any number of service providers.
With regards to your crash, there is no guarantee that this activity is available. Quoting the documentation:
In some cases, a matching Activity may not exist, so ensure you safeguard against this.
The "safeguard" could be wrapping your startActivity() call in an exception handler, watching for ActivityNotFoundException.
Also, please note that you use startActivity(), not startActivityForResult(), with this Intent action. Again, quoting the documentation:
Output: Nothing.
This means that there is no result, and using startActivityForResult() is a waste of time.

Android Boot Completed Receiver doesn't work on system app

My application contains a BOOT_COMPLETED receiver like described in other threads here. It works perfectly until I changed my app into a system application. Now the Receiver does not trigger the event anymore.
Any ideas for this issue? I'm using Android Kitkat 4.4.2 on a Radxa Rock Pro. Compiled my own image to register the app as system application.
we need more detail to help you, post the code and the manifest, also debug log, in other hand you must use Log.i(TAG, "onReceive."); to know if the problem is in onReceive() or when trying to run the service class.

java.lang.SecurityException: Not allowed to bind to service Intent

I implemented reverse geocoding inside GCMIntentService onMessage method. I got exception java.lang.SecurityException: Not allowed to bind to service Intent { act=com.android.location.service.GeocodeProvider pkg=com.google.android.location }
after application running long time. If i restart device it works again fine. Also i seen android open isssue Reconnection with Geocoder services not possible when Network Location Service is killed.. Is it any work around to solve this issue?
Note : I am running Application in Samsung galaxy Tab 2(Android 4.1.1)

Categories

Resources