如何修复来自BroadcastReceiver的重复值?

时间:2019-01-30 18:52:06

标签: java android android-intent service broadcastreceiver

我正在使用BroadcastReceiver来获取电池状态(例如电池电量百分比等)。我还使用了“永无休止”服务,即使客户端关闭了该应用程序,该服务也仍在运行。我的问题是,当客户端第二次打开应用程序时,BroadcastReceiver中的值(电池百分比)会重复。

logcat的示例(第一次运行):电池百分比:80%,前台:true

实施例从logcat中(从第一运行关闭的应用):电池百分比:80%,在前景:假

logcat的示例(第二次运行):电池百分比:80%,前台:false,电池百分比:80%,前台:true

logcat的示例(从第二次运行中关闭的应​​用):电池百分比:80%,前台:false,电池百分比:80%,前台:false

如您所见,BroadcastReceiver中的所有内容在应用程序第二次打开后都会重复。

我尝试注销了BroadcastReceiver,但问题仍然出现。

MainActivity类

public class MainActivity extends AppCompatActivity {

public boolean inForeground = true;

Intent mServiceIntent;
Context context;

public Context getContext() {
    return context;
}

private BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {

    @Override
    public void onReceive(Context context, Intent intent) {

        int level = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);
        int scale = intent.getIntExtra(BatteryManager.EXTRA_SCALE, -1);

        float batteryPct = (level / (float)scale);
        int batteryPercentage = (int)((batteryPct)*100);

        Log.i("Battery percent", ""+batteryPercentage+"%!");
        Log.i("In foreground", String.valueOf(inForeground));
    }
};

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    context = this;
    SensorService mSensorService = new SensorService(getContext());
    mServiceIntent = new Intent(getContext(), mSensorService.getClass());
    if (!isMyServiceRunning(mSensorService.getClass())) {
        startService(mServiceIntent);
    }

    IntentFilter iFilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
    Context mContext = getApplicationContext();
    mContext.registerReceiver(mBroadcastReceiver, iFilter);
}

@Override
protected void onResume() {
    super.onResume();
    inForeground = true;
}

@Override
protected void onPause() {
    inForeground = false;
    super.onPause();
}

// Services
private boolean isMyServiceRunning(Class<?> serviceClass) {
    ActivityManager manager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
    for (ActivityManager.RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
        if (serviceClass.getName().equals(service.service.getClassName())) {
            Log.i ("Running Service", true+"!");
            return true;
        }
    }
    Log.i ("Running Service", false+"!");
    return false;
}

@Override
protected void onDestroy() {
    stopService(mServiceIntent);
    Log.i("Main", "onDestroy!");
    super.onDestroy();
}
}

SensorService类

public class SensorService extends Service {
public SensorService(Context applicationContext) {
    super();
    Log.i("Called", "SensorService!");
}

public SensorService() {
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    super.onStartCommand(intent, flags, startId);
    return START_STICKY;
}

@Override
public void onDestroy() {
    super.onDestroy();
    Log.i("Exit", "onDestroy!");
    Intent broadcastIntent = new Intent(this, SensorRestarterBroadcastReceiver.class);
    sendBroadcast(broadcastIntent);
}

@Nullable
@Override
public IBinder onBind(Intent intent) {
    return null;
}
}

SensorRestarterBroadcastReceiver类

public class SensorRestarterBroadcastReceiver extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {
    Log.i(SensorRestarterBroadcastReceiver.class.getSimpleName(), "Ongoing Service!");
    context.startService(new Intent(context, SensorService.class));
}
}

预期输出应为电池百分比:80%,在前台:true ,但实际输出是重复的,看起来像是电池百分比:80%,在前台:false,电池百分比:80%,前景:是

编辑

我已经在onCreate和onResume中尝试过使用它

Context mContext = getApplicationContext();
SensorService mSensorService = new SensorService(getContext());
mServiceIntent = new Intent(getContext(), mSensorService.getClass());
    if (isMyServiceRunning(mSensorService.getClass())) {
        mContext.unregisterReceiver(mBroadcastReceiver);
    }

编辑2

我将 onDestroy()更改为此:

@Override
protected void onDestroy() {

    Context mContext = getApplicationContext();
    SensorService mSensorService = new SensorService(getContext());
    mServiceIntent = new Intent(getContext(), mSensorService.getClass());
    if (isMyServiceRunning(mSensorService.getClass())) {
        mContext.unregisterReceiver(mBroadcastReceiver);
    }

    stopService(mServiceIntent);
    Log.i("Main", "onDestroy!");
    inForeground = false;
    super.onDestroy();
}

现在,代码在前台运行良好,但是当应用程序在后台运行时,我没有任何读数。

0 个答案:

没有答案