Android: Firebase データに変更がない場合でも、onStartCommand() メソッドが複数回呼び出されます
私は Android を初めて使用し、Firebase の概念をテストしています。デバイスを登録した後、startService メソッドを呼び出しています。
これが私の登録活動コードです
if (status.trim().equalsIgnoreCase("success")) {
//Displaying a success toast
Toast.makeText(RegistrationActivity.this, "Registered successfully", Toast.LENGTH_SHORT).show();
//Opening shared preference
SharedPreferences sharedPreferences = getSharedPreferences(Constants.SHARED_PREF, MODE_PRIVATE);
//Opening the shared preferences editor to save values
SharedPreferences.Editor editor = sharedPreferences.edit();
//Storing the unique id
editor.putString(Constants.UNIQUE_ID, uniqueId);
//Saving the boolean as true i.e. the device is registered
editor.putBoolean(Constants.REGISTERED, true);
//Applying the changes on sharedpreferences
editor.apply();
Log.d("NotificationIntent","Registration success");
//Starting our listener service once the device is registered
startService(new Intent(getBaseContext(), NotificationListenerService.class));
}
これが私の NotificationListenerService です
public class NotificationListenerService extends Service {
SharedPreferences sharedpreferences;
public static final String MyPREFERENCES = "MyPrefs" ;
@Override
public IBinder onBind(Intent intent) {
return null;
}
//When the service is started
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.d("NotificationListenerService","onStartCommand");
//Opening sharedpreferences
SharedPreferences sharedPreferences = getSharedPreferences(Constants.SHARED_PREF, MODE_PRIVATE);
//Getting the firebase id from sharedpreferences
String id = sharedPreferences.getString(Constants.UNIQUE_ID, null);
//Creating a firebase object
Firebase firebase = new Firebase(Constants.FIREBASE_APP + id);
//Adding a valueevent listener to firebase
//this will help us to track the value changes on firebase
firebase.addValueEventListener(new ValueEventListener() {
//This method is called whenever we change the value in firebase
@Override
public void onDataChange(DataSnapshot snapshot) {
Log.d("NotificationListenerService","onDataChange");
//Getting the value from firebase
//We stored none as a initial value
String msg = snapshot.child("msg").getValue().toString();
//So if the value is none we will not create any notification
if (msg.equals("none"))
return;
//If the value is anything other than none that means a notification has arrived
//calling the method to show notification
//String msg is containing the msg that has to be shown with the notification
showNotification(msg);
}
@Override
public void onCancelled(FirebaseError firebaseError) {
Log.e("The read failed: ", firebaseError.getMessage());
}
});
return START_STICKY;
}
firebase の値に変更がない場合、アプリケーションが閉じている間に StartCommand を呼び出すと、通知が複数回受信されます。
------------に答える------------
ValueListener.onDataChange() はデータの変更に対して起動し、リスナーが追加されたときにも d が起動します。その結果、onStartCommand() が呼び出され、メッセージが「none」でない場合は常に通知を受け取ります。
onStartCommand() が START_STICKY を返すため、アプリがバックグラウンドにあり、システムがサービスを強制終了した場合 (メモリ不足が原因で発生する可能性があります)、サービスが再起動され、別の通知が投稿されます。
タグ:
関連記事:
負荷分散 - Elixir Ecto データベース接続の問題を検出するにはどうすればよいですか?
機械学習 - 画像検索のためにフィッシャーベクトル間の距離を測定する方法は?