How to fix NoClassDefFoundError: android.media.session.MediaSessionManager - android

I made an app it run perfectly in api 20+ but for android version 4.4 and less it is getting crashed with error NoClassDefFoundError: android.media.session.MediaSessionManager this is the stack trace that i am getting in the developer console .
java.lang.NoClassDefFoundError: android.media.session.MediaSessionManager
at beatbox.neelay.beatbox.MediaService.initMediaSession(MediaService.java:634)
at beatbox.neelay.beatbox.MediaService.onStartCommand(MediaService.java:170)
at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:2913)
at android.app.ActivityThread.access$2100(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1442)
at android.os.Handler.dispatchMessage(Handler.java:110)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:5339)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:828)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:644)
at dalvik.system.NativeStart.main(Native Method)
All I am able to understand from this is the error is in the initMediaSession method .this is my initMediaSession method
private void initMediaSession() throws RemoteException {
if (mediaSessionManager != null) return; //mediaSessionManager exists
mediaSessionManager = (MediaSessionManager) getSystemService(Context.MEDIA_SESSION_SERVICE);
// Create a new MediaSession
mediaSession = new MediaSessionCompat(getApplicationContext(), "AudioPlayer");
//Get MediaSessions transport controls
transportControls = mediaSession.getController().getTransportControls();
//set MediaSession -> ready to receive media commands
mediaSession.setActive(true);
//indicate that the MediaSession handles transport control commands
// through its MediaSessionCompat.Callback.
mediaSession.setFlags(MediaSessionCompat.FLAG_HANDLES_TRANSPORT_CONTROLS);
//Set mediaSession's MetaData
updateMetaData();
// passing the data
// Attach Callback to receive MediaSession updates
mediaSession.setCallback(new MediaSessionCompat.Callback() {
// Implement callbacks
#Override
public void onPlay() {
super.onPlay();
messagesent();
a = false;
resumeMedia();
buildNotification(PlaybackStatus.PLAYING);
}
#Override
public void onPause() {
super.onPause();
messagesent();
a = true;
pauseMedia();
buildNotification(PlaybackStatus.PAUSED);
}
#Override
public void onSkipToNext() {
super.onSkipToNext();
skipToNext();
updateMetaData();
buildNotification(PlaybackStatus.PLAYING);
}
#Override
public void onSkipToPrevious() {
super.onSkipToPrevious();
skipToPrevious();
updateMetaData();
buildNotification(PlaybackStatus.PLAYING);
}
#Override
public void onStop() {
super.onStop();
removeNotification();
//Stop the service
pauseMedia();
messagesent();
stopSelf();
}
#Override
public void onSeekTo(long position) {
super.onSeekTo(position);
}
});
}
I dont understand why it is getting crashend for 4.4 and less devices and how i can fix this .I googled and got this but this post dont tell how tofix this.

MediaSessionManager was only added in api 21 (5.0)
If it's absolutely necessary to use it then you can set your min sdk to 21 or check your build number with:
android.os.Build.VERSION.SDK
and not call this service with devices with lower sdks

It seems that the reason may be due to multidex. Check your apk method count at Get Method Count
You can enable multidex by adding dependency
compile 'com.android.support:multidex:1.0.1'
then enable it in config
defaultConfig {
multiDexEnabled true
}
add following snippet in android section of your
dexOptions {
javaMaxHeapSize "4g"
preDexLibraries false
}
afterEvaluate {
tasks.matching {
it.name.startsWith('dex')
}.each { dx ->
if (dx.additionalParameters == null) {
dx.additionalParameters = []
}
dx.additionalParameters += '--multi-dex'
// this is optional
dx.additionalParameters += "--main-dex-list=$projectDir/multidex.keep".toString()
}
}
compileOptions {
incremental false
}

I am also following the same tutorial and had same issue. I found the solution to this. Simply check if your SDK >21 then only use method initMediaSession();

Related

How to solve this error 'com.google.ads.mediation.admob.AdMobAdapter cannot be cast to com.google.android.gms.ads.mediation.m '?

am using firebase adapter in my project, now am integrate admob for rewarded ad it's working fine, but every video after completion it will throw following error...
java.lang.ClassCastException: com.google.ads.mediation.admob.AdMobAdapter cannot be cast to com.google.android.gms.ads.mediation.m
at com.google.android.gms.ads.internal.mediation.client.y.a(:com.google.android.gms.dynamite_dynamitemodulesa#12685008#12.6.85 (020306-197041431):262)
at com.google.android.gms.ads.internal.reward.c.b(:com.google.android.gms.dynamite_dynamitemodulesa#12685008#12.6.85 (020306-197041431):54)
at com.google.android.gms.ads.internal.reward.client.e.onTransact(:com.google.android.gms.dynamite_dynamitemodulesa#12685008#12.6.85 (020306-197041431):56)
at android.os.Binder.transact(Binder.java:380)
at com.google.android.gms.internal.ads.zzej.transactAndReadExceptionReturnVoid(Unknown Source)
at com.google.android.gms.internal.ads.zzahb.zze(Unknown Source)
at com.google.android.gms.internal.ads.zzahm.resume(Unknown Source)
at com.mopub.mobileads.GooglePlayServicesRewardedVideo$1.onResume(GooglePlayServicesRewardedVideo.java:84)
at com.mopub.common.MoPubLifecycleManager.onResume(MoPubLifecycleManager.java:83)
at com.mopub.common.MoPub.onResume(MoPub.java:257)
at com.coderays.realquiz.RealQuizDashBoard.onResume(RealQuizDashBoard.java:956)
at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1280)
at android.app.Activity.performResume(Activity.java:6096)
at android.app.ActivityThread.performResumeActivity(ActivityThread.java:3011)
at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:3063)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1361)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5343)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:905)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:700)
i will try this method https://stackoverflow.com/a/49959522/6477998 but still throw this error.
my gradle is..
dependencies {
implementation('com.mopub:mopub-sdk-interstitial:5.0.0#aar') {
transitive = true
}
// For rewarded videos. This will automatically also include interstitials
implementation('com.mopub:mopub-sdk-rewardedvideo:5.0.0#aar') {
transitive = true
}
implementation 'com.google.firebase:firebase-core:16.0.0'
implementation 'com.google.firebase:firebase-ads:15.0.1'
}
Which one to change, please give me a solution, am searching still 3 days but no improvement.
Advance thanx...
thanks, the error will be fixed after long time.
Step 1: i remove my adapter class GooglePlayServicesRewardedVideo.java from com.mopub.mobileads( if you are using GooglePlayServicesInterstitial.java please remove ).
Step 2: then, put admob mediation sdk into build.gradle i.e implementation 'com.mopub.mediation:admob:15.0.0.8'.
Step 3: then, remove the following lines in your rewarded display activity.
public class MainActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
MoPubRewardedVideos.initializeRewardedVideo(this);
MoPub.onCreate(this); // remove
// ...
}
#Override
public void onPause() {
super.onPause();
MoPub.onPause(this); // remove
}
#Override
public void onResume() {
super.onResume();
MoPub.onResume(this); // remove
}
// The following methods are required for Chartboost rewarded video mediation
#Override
public void onStart() {
super.onStart();
MoPub.onStart(this); // remove
}
#Override
public void onRestart() {
super.onRestart();
MoPub.onRestart(this); // remove
}
#Override
public void onStop() {
super.onStop();
MoPub.onStop(this); // remove
}
#Override
public void onDestroy() {
super.onDestroy();
MoPub.onDestroy(this); // remove
}
#Override
public void onBackPressed() {
super.onBackPressed();
MoPub.onBackPressed(this); // remove
}
}
Step 4 : syn your project, and take a build. Its working fine without error.
thanks.

Android Facebook Login Sdk: getActivity().getCallingActivity() returns null from their LoginFragment.class

I'm using the facebook-sdk for the facebook login button. This the facebook library I'm using:
implementation ('com.facebook.android:facebook-android-sdk:4.0.0'){
exclude group: 'com.android.support'
}
This is the crash raised by Firebase Crashlytics:
https://i.stack.imgur.com/jqArL.png
On this devices:
Galaxy S7 not rooted Android 7.0
Huawei Honor 9(STF-L09) not rooted Android 8.0
I check the code to know what is the Facebook library doing but I didn't find anything that I can do from my code to solve it. This is the method that raised the crash(LoginFragment.java line 68):
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (savedInstanceState != null) {
loginClient = savedInstanceState.getParcelable(SAVED_LOGIN_CLIENT);
loginClient.setFragment(this);
} else {
loginClient = new LoginClient(this);
}
callingPackage = getActivity().getCallingActivity().getPackageName();
request = (LoginClient.Request)
getActivity().getIntent().getParcelableExtra(EXTRA_REQUEST);
loginClient.setOnCompletedListener(new LoginClient.OnCompletedListener() {
#Override
public void onCompleted(LoginClient.Result outcome) {
onLoginClientCompleted(outcome);
}
});
}
This is the getCallingActivity method that returns null from android.app.Activity.java
public ComponentName getCallingActivity() {
try {
return ActivityManager.getService().getCallingActivity(mToken);
} catch (RemoteException e) {
return null;
}
}
I cannot reproduce the error with the testing devices at the office so I only have the Crashlytics report to solve it. I 'm clueless because it seems to work for most of the devices
Any help would be amazing, thanks in advance

Audience Network interstitial/rewarded ad show Android crash on Unity

i'm trying to display Facebook ads on Android. I have included the Audience Network .jar on my Android Studio Project. Also added the activity to the Android Manifest.
This is the build.gradle:
android {
compileSdkVersion 25
buildToolsVersion "25.0.2"
testOptions {
unitTests.returnDefaultValues = true
}
defaultConfig {
minSdkVersion 15
targetSdkVersion 25
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
shrinkResources false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
debuggable false
}
debug {
debuggable true
jniDebuggable true
minifyEnabled false
shrinkResources false
}
}
}
dependencies {
// Required -- JUnit 4 framework
testCompile 'junit:junit:4.12'
// Optional -- Mockito framework
testCompile 'org.mockito:mockito-core:1.10.19'
compile 'com.google.android.gms:play-services:10.2.0'
compile files('libs/AudenceNetwork-4.24.0.jar')
compile 'com.android.support:appcompat-v7:25.0.0'
compile 'com.android.support:recyclerview-v7:25.0.0'
}
This is a snippet on how i load and show interstitials and rewarded:
com.facebook.ads.InterstitialAd interstitial;
com.facebook.ads.RewardedVideoAd rewarded
public void loadInterstitial() {
activity.runOnUiThread(new Runnable() {
#Override
public void run() {
if (interstitial == null) {
interstitial = new com.facebook.ads.InterstitialAd(activity, interstitialAd.getCode());
CustomFacebookInterstitialAdListener interestitialListener = new CustomFacebookInterstitialAdListener(AudienceNetworkAdProvider.this);
interstitial.setAdListener(interestitialListener);
}
interstitial.loadAd();
}
});
}
public void showInterstitial() {
if (isInterstitialAvailable()) {
activity.runOnUiThread(new Runnable() {
#Override
public void run() {
interstitial.show();
}
});
}
}
public void loadRewardedAd() {
activity.runOnUiThread(new Runnable() {
#Override
public void run() {
if (rewarded == null) {
rewarded = new com.facebook.ads.RewardedVideoAd(activity, rewardedAd.getCode());
CustomFacebookRewardedAdListener rewardedListener = new CustomFacebookRewardedAdListener(AudienceNetworkAdProvider.this);
rewarded.setAdListener(rewardedListener);
}
rewarded.loadAd();
}
});
}
public void showRewardedAd() {
if (isRewardedAdAvailable()) {
activity.runOnUiThread(new Runnable() {
#Override
public void run() {
rewarded.show();
}
});
}
}
The code works fine on a Android native test app.
I'm making an adaptation to Unity, so i can show ads on a game. However, every time i want to display an ad it randomly crashes the app. Most of the times it let me see one interstitial or rewarded, but after that crashes.
This is how my Plugins/Android folder looks like:
AndroidManifest.xml
appcompat-v7-24.0.0
myaudiencenetworkadapter.aar
play-services-10.0.1.aar
play-services-auth-10.0.1
play-services-auth-base-10.0.1
play-services-basement-10.0.1
play-services-drive-10.0.1
play-services-tasks-10.0.1
recyclerview-v7-24.0.0
support-v4-24.0.0
It crashes when com.facebook.ads.RewardedVideoAd.show() method is invoked.
This is the log of the crash:
08-07 16:51:30.861 16820-16820/? E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.somepackage.test, PID: 16820
java.lang.Error: FATAL EXCEPTION [main]
Unity version : 5.5.3f1
Device model : samsung SM-G925I
Device fingerprint: samsung/zeroltedv/zerolte:6.0.1/MMB29K/G925IDVS3EQF1:user/release-keys
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void com.facebook.ads.internal.DisplayAdController.c()' on a null object reference
at com.facebook.ads.RewardedVideoAd.show(Unknown Source)
at com.boxit.ads.facebook.AudienceNetworkAdProvider$3.run(AudienceNetworkAdProvider.java:168)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:158)
at android.app.ActivityThread.main(ActivityThread.java:7229)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
Any ideas?
You shouldn't be messing with Java and the Facebook jar in Android Studio while there is an official Unity Facebook plugin. See this link for instructions on how to set it up and here to get the actual SDK.
When you download the plugin you will find InterstitialAdScene, RewardedVideoAdScene and NativeAdScene scenes that contains sample codes for you should load InterstitialAdScene scene since that's what you are looking for.
Below is C# Interstitial Ad example from the InterstitialAdTest.cs file in the AudienceNetwork\Samples\Interstitial folder.
public class InterstitialAdTest : MonoBehaviour
{
private InterstitialAd interstitialAd;
private bool isLoaded;
// UI elements in scene
public Text statusLabel;
// Load button
public void LoadInterstitial ()
{
this.statusLabel.text = "Loading interstitial ad...";
// Create the interstitial unit with a placement ID (generate your own on the Facebook app settings).
// Use different ID for each ad placement in your app.
InterstitialAd interstitialAd = new InterstitialAd ("YOUR_PLACEMENT_ID");
this.interstitialAd = interstitialAd;
this.interstitialAd.Register (this.gameObject);
// Set delegates to get notified on changes or when the user interacts with the ad.
this.interstitialAd.InterstitialAdDidLoad = (delegate() {
Debug.Log ("Interstitial ad loaded.");
this.isLoaded = true;
this.statusLabel.text = "Ad loaded. Click show to present!";
});
interstitialAd.InterstitialAdDidFailWithError = (delegate(string error) {
Debug.Log ("Interstitial ad failed to load with error: " + error);
this.statusLabel.text = "Interstitial ad failed to load. Check console for details.";
});
interstitialAd.InterstitialAdWillLogImpression = (delegate() {
Debug.Log ("Interstitial ad logged impression.");
});
interstitialAd.InterstitialAdDidClick = (delegate() {
Debug.Log ("Interstitial ad clicked.");
});
// Initiate the request to load the ad.
this.interstitialAd.LoadAd ();
}
// Show button
public void ShowInterstitial ()
{
if (this.isLoaded) {
this.interstitialAd.Show ();
this.isLoaded = false;
this.statusLabel.text = "";
} else {
this.statusLabel.text = "Ad not loaded. Click load to request an ad.";
}
}
void OnDestroy ()
{
// Dispose of interstitial ad when the scene is destroyed
if (this.interstitialAd != null) {
this.interstitialAd.Dispose ();
}
Debug.Log ("InterstitialAdTest was destroyed!");
}
// Next button
public void NextScene ()
{
SceneManager.LoadScene ("AdViewScene");
}
}
I finally solved the issue.
I was destroying Audience Network ads on the onPause event from my Unity activity. Every time an ad is displayed, onPause event is called so it was a mess.

Firebase realtime DB events not triggered in APK, but are in editor

I have built a Unity game which needs to listen for updates to a database object. When I run the game in the Unity editor - realtime DB changed events are triggered and the game reacts accordingly. When I publish to my Android device, however, these events dont seem to get registered.
When I check the Unity logs, the error I see is:
InitializationException: Firebase modules failed to initialize: invites (missing dependency), messaging (missing dependency), remote_config (missing dependency)
Though I clearly have those packages imported.
Any thoughts?
firebaseReference.GetValueAsync().ContinueWith(task => {
if (task.IsFaulted) {
// Handle the error...
print ("failed watchers");
}
else if (task.IsCompleted) {
DataSnapshot snapshot = task.Result;
// Do something with snapshot...
print ("set watchers");
}
});
firebaseReference.ChildChanged += HandleChildChanged;
This error is related to Google Services; you have two solutions (I'll post only the related to the messaging, that's the only one I used):
1) Check the dependencies, using the Firebase plugin, and show a message to the user (the plugin does it for you).
2) Try to register the Firebase messaging service, catch any errors and fail silently (if that's the case);
Here is the code for solution 1:
PushNotificationManager.cs
using UnityEngine;
using Firebase;
public class PushNotificationManager : MonoBehaviour {
DependencyStatus dependencyStatus = DependencyStatus.UnavailableOther;
public void Start()
{
dependencyStatus = FirebaseApp.CheckDependencies();
if (dependencyStatus != DependencyStatus.Available)
{
FirebaseApp.FixDependenciesAsync().ContinueWith(task =>
{
dependencyStatus = FirebaseApp.CheckDependencies();
if (dependencyStatus == DependencyStatus.Available)
{
InitializeFirebase();
}
else
{
Debug.LogError("Could not resolve all Firebase dependencies: " + dependencyStatus);
}
});
}
else
{
InitializeFirebase();
}
}
protected void InitializeFirebase()
{
Firebase.Messaging.FirebaseMessaging.TokenReceived += OnTokenReceived;
Firebase.Messaging.FirebaseMessaging.MessageReceived += OnMessageReceived;
}
public void OnTokenReceived(object sender, Firebase.Messaging.TokenReceivedEventArgs token)
{
Debug.Log(token.Token);
}
public void OnMessageReceived(object sender, Firebase.Messaging.MessageReceivedEventArgs e)
{
//Debug.Log("Received a new message from: " + e.Message.From + " / " + e.Message.Data.ToString());
}
}
And here is the code for solution 2: PushNotificationManager.cs
using UnityEngine;
using System;
public class PushNotificationManager : MonoBehaviour {
public void Start()
{
InitializeFirebase();
}
protected void InitializeFirebase()
{
try
{
Firebase.Messaging.FirebaseMessaging.TokenReceived += OnTokenReceived;
Firebase.Messaging.FirebaseMessaging.MessageReceived += OnMessageReceived;
}
catch(Exception e)
{
Debug.LogError("Erro no Firebase: "+e);
}
}
// (...)
}
NOTE:
As far as I know, there's no solution till now, so this is only to prevent crashes to your app.
More info here: Bugs reported [Firebase-Unity]

what is Google Play services MeasurementBrokerService and how to stop it?

I have created a Service which listens to a Firebase location.
This is a simple service with low memory usage. Unfortunately, I am seeing a Google play services service called MeasurementBrokerService joining my service and not freeing memory.
Unlike a related question :
"Service MeasurementBrokerService is in use" is showing in my application process
The above question has no accepted answer, so kindly do not mark this as a duplicate
I am not using firebase-appindexing.
Following is my app level gradle file:
buildscript {
repositories {
maven { url 'https://maven.fabric.io/public' }
}
dependencies {
classpath 'io.fabric.tools:gradle:1.+'
}
}
apply plugin: 'com.android.application'
apply plugin: 'io.fabric'
repositories {
maven { url 'https://maven.fabric.io/public' }
}
android {
compileSdkVersion 25
buildToolsVersion "25.0.2"
defaultConfig {
applicationId "com.example.project.recommendedapp"
minSdkVersion 21
targetSdkVersion 25
versionCode 1
versionName "1.0"
// Enabling multidex support.
multiDexEnabled true
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:25.1.0'
compile 'com.android.support:cardview-v7:25.1.0'
compile 'com.android.support:recyclerview-v7:25.1.0'
compile 'com.android.support:design:25.1.0'
compile 'com.android.support:support-v4:25.1.0'
compile 'com.github.clans:fab:1.6.4'
compile 'com.google.android.gms:play-services-location:10.0.1'
compile('com.crashlytics.sdk.android:crashlytics:2.6.0#aar') {
transitive = true;
}
compile 'com.facebook.fresco:fresco:1.0.0'
compile 'com.google.firebase:firebase-database:10.0.1'
compile 'com.google.firebase:firebase-messaging:10.0.1'
compile 'com.squareup.okhttp3:okhttp:3.5.0'
}
apply plugin: 'com.google.gms.google-services'
Can someone please guide me as to how to stop the service from joining my process.
The service is as follows:
public class SubscriptionListenerService extends Service {
DatabaseReference userNodeSubscriptionRef;
ChildEventListener subscribedTopicsListener;
SharedPreferences sessionPref,subscribedTopicsPreference;
SharedPreferences.Editor subscribedtopicsprefeditor;
String userid;
boolean stoppedInternally = false;
SharedPreferences.OnSharedPreferenceChangeListener sessionPrefChangeListener;
#Nullable
#Override
public IBinder onBind(Intent intent) {
//do not need a binder over here
return null;
}
#Override
public void onCreate(){
super.onCreate();
Log.d("FragmentCreate","onCreate called inside service");
sessionPref = getSharedPreferences("SessionPref",0);
subscribedTopicsPreference=getSharedPreferences("subscribedTopicsPreference",0);
subscribedtopicsprefeditor=subscribedTopicsPreference.edit();
userid = sessionPref.getString("userid",null);
sessionPrefChangeListener = new SharedPreferences.OnSharedPreferenceChangeListener() {
#Override
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
Log.d("FragmentCreate","The shared preference changed "+key);
stoppedInternally=true;
sessionPref.unregisterOnSharedPreferenceChangeListener(this);
if(userNodeSubscriptionRef!=null && subscribedTopicsListener!=null){
userNodeSubscriptionRef.removeEventListener(subscribedTopicsListener);
}
stopSelf();
}
};
sessionPref.registerOnSharedPreferenceChangeListener(sessionPrefChangeListener);
subscribedTopicsListener = new ChildEventListener() {
#Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
if(!(dataSnapshot.getValue() instanceof Boolean)){
Log.d("FragmentCreate","Please test subscriptions with a boolean value");
}else {
if ((Boolean) dataSnapshot.getValue()) {
//here we subscribe to the topic as the topic has a true value
Log.d("FragmentCreate", "Subscribing to topic " + dataSnapshot.getKey()+" "+this.getClass().getName());
subscribedtopicsprefeditor.putBoolean(dataSnapshot.getKey(), true);
FirebaseMessaging.getInstance().subscribeToTopic(dataSnapshot.getKey());
} else {
//here we unsubscribed from the topic as the topic has a false value
Log.d("FragmentCreate", "Unsubscribing from topic " + dataSnapshot.getKey()+" "+this.getClass().getName());
subscribedtopicsprefeditor.remove(dataSnapshot.getKey());
FirebaseMessaging.getInstance().unsubscribeFromTopic(dataSnapshot.getKey());
}
subscribedtopicsprefeditor.commit();
}
}
#Override
public void onChildChanged(DataSnapshot dataSnapshot, String s) {
//either an unsubscription will trigger this, or a re-subscription after an unsubscription
if(!(dataSnapshot.getValue() instanceof Boolean)){
Log.d("FragmentCreate","Please test subscriptions with a boolean value");
}else{
if((Boolean)dataSnapshot.getValue()){
Log.d("FragmentCreate","Subscribing to topic "+dataSnapshot.getKey()+" "+this.getClass().getName());
subscribedtopicsprefeditor.putBoolean(dataSnapshot.getKey(),true);
FirebaseMessaging.getInstance().subscribeToTopic(dataSnapshot.getKey());
}else{
Log.d("FragmentCreate","Unsubscribing from topic "+dataSnapshot.getKey()+" "+this.getClass().getName());
subscribedtopicsprefeditor.remove(dataSnapshot.getKey());
FirebaseMessaging.getInstance().unsubscribeFromTopic(dataSnapshot.getKey());
}
subscribedtopicsprefeditor.commit();
}
}
#Override
public void onChildRemoved(DataSnapshot dataSnapshot) {
//Log.d("FragmentCreate","Unubscribing from topic "+dataSnapshot.getKey());
//FirebaseMessaging.getInstance().unsubscribeFromTopic(dataSnapshot.getKey());
}
#Override
public void onChildMoved(DataSnapshot dataSnapshot, String s) {
//do nothing, this won't happen --- rather this isnt important
}
#Override
public void onCancelled(DatabaseError databaseError) {
Log.d("FragmentCreate","Failed to listen to subscriptions node");
}
};
if(userid!=null){
Log.d("FragmentCreate","Found user id in service "+userid);
userNodeSubscriptionRef = FirebaseDatabase.getInstance().getReference().child("Users").child(userid).child("subscriptions");
userNodeSubscriptionRef.addChildEventListener(subscribedTopicsListener);
userNodeSubscriptionRef.keepSynced(true);
}else{
Log.d("FragmentCreate","Couldn't find user id");
stoppedInternally=true;
stopSelf();
}
}
#Override
public int onStartCommand(Intent intent,int flags,int startId){
//don't need anything done over here
//The intent can have the following extras
//If the intent was started by the alarm manager ..... it will contain android.intent.extra.ALARM_COUNT
//If the intent was sent by the broadcast receiver listening for boot/update ... it will contain wakelockid
//If it was started from within the app .... it will contain no extras in the intent
//The following will not throw an exception if the intent does not have an wakelockid in extra
//As per android doc... the following method releases the wakelock if any specified inside the extra and returns true
//If no wakelockid is specified, it will return false;
if(intent!=null){
if(BootEventReceiver.completeWakefulIntent(intent)){
Log.d("FragmentCreate","Wakelock released");
}else{
Log.d("FragmentCreate","Wakelock not acquired in the first place");
}
}else{
Log.d("FragmentCreate","Intent started by regular app usage");
}
return START_STICKY;
}
#Override
public void onDestroy(){
if(userNodeSubscriptionRef!=null){
userNodeSubscriptionRef.keepSynced(false);
}
userNodeSubscriptionRef = null;
subscribedTopicsListener = null;
sessionPref = null;
subscribedTopicsPreference = null;
subscribedtopicsprefeditor = null;
userid = null;
sessionPrefChangeListener = null;
if(stoppedInternally){
Log.d("FragmentCreate","Service getting stopped due to no userid or due to logout or data clearance...do not restart auto.. it will launch when user logs in or signs up");
}else{
Log.d("FragmentCreate","Service getting killed by user explicitly from running services or by force stop ... attempt restart");
//well basically restart the service using an alarm manager ... restart after one minute
AlarmManager alarmManager = (AlarmManager) this.getSystemService(ALARM_SERVICE);
Intent restartServiceIntent = new Intent(this,SubscriptionListenerService.class);
restartServiceIntent.setPackage(this.getPackageName());
//context , uniqueid to identify the intent , actual intent , type of pending intent
PendingIntent pendingIntentToBeFired = PendingIntent.getService(this,1,restartServiceIntent,PendingIntent.FLAG_ONE_SHOT);
if(Build.VERSION.SDK_INT>=23){
alarmManager.setExactAndAllowWhileIdle(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime()+600000,pendingIntentToBeFired);
}else{
alarmManager.setExact(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime()+600000,pendingIntentToBeFired);
}
}
super.onDestroy();
}
}
I checked that somehow still google AppMeasurement service was running on my device.
I have resorted to using the following approach:
The following stops data collection by firebase.
https://firebase.google.com/support/guides/disable-analytics tells us the following approach
<meta-data android:name="firebase_analytics_collection_deactivated" android:value="true" />
This alone still can't stop the appMeasurement from initializing.
The second step that has solved is adding the following to the app level gradle file:
configurations {
all*.exclude group: 'com.google.firebase', module: 'firebase-core'
}
This basically removes all the code that firebase analytics uses.
In case it still doesn't work, make sure that the gradle does not include broad play service dependency
eg: compile 'com.google.android.gms:play-services-location:10.0.1'
instead of 'com.google.android.gms:play-services:10.0.1'
Worst case and i have not tested this as the above solved it for me is:
https://developers.google.com/analytics/devguides/collection/android/v4/advanced
Google should really stop putting unwanted things without the dev's permission IMHO. Hope this helps someone

Categories

Resources