2017-02-10 287 views
0

我創建了一個偵聽Firebase位置的服務。什麼是Google Play服務MeasurementBrokerService以及如何阻止它?

這是一個內存使用率低的簡單服務。不幸的是,我看到名爲MeasurementBrokerService的Google Play服務服務加入我的服務並且沒有釋放內存。

不像一個相關的問題: "Service MeasurementBrokerService is in use" is showing in my application process

上述問題有沒有公認的答案,所以好心不標記這是我不使用火力,appindexing重複

以下是我的應用程序級別gradle這個文件:

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:[email protected]') { 
     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' 

是否有人可以指導我爲如何從加入我的進程停止服務。

的服務如下:

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(); 

} 


} 

回答

1

我查了,不知怎的,還是谷歌AppMeasurement服務是我的設備上運行。

我已轉向使用以下方法:

下面以火力停止收集數據。

https://firebase.google.com/support/guides/disable-analytics告訴我們以下方法

<meta-data android:name="firebase_analytics_collection_deactivated" android:value="true" /> 

僅憑這一點還是不能從初始化停止appMeasurement。

已經解決的問題是加入以下應用的gradle級文件中的第二個步驟:

configurations { 
    all*.exclude group: 'com.google.firebase', module: 'firebase-core' 
} 

這基本上將刪除火力分析使用所有的代碼。

在情況下,它仍然不能正常工作,請確保gradle這個不包括廣泛的播放服務的依賴

如:編譯「com.google.android.gms:發揮服務地點:10.0.1 「

,而不是‘com.google.android.gms:播放服務:10.0.1’

最壞的情況,我還沒有測試,這是上面的解決了這個問題對我來說是:

https://developers.google.com/analytics/devguides/collection/android/v4/advanced

谷歌應該真的停止放置不需要的東西沒有開發許可恕我直言。希望這可以幫助別人

相關問題