Avoid unregistering the same receiver more than once.
Bug: 231496005 Test: atest com.android.cts.net.HostsideRestrictBackgroundNetworkTests Change-Id: Iba17daa70f14bdee1d1a7d32cb9dc64623ba1d24 Merged-In: Iba17daa70f14bdee1d1a7d32cb9dc64623ba1d24
This commit is contained in:
parent
3c80d34147
commit
d1e236eb05
2 changed files with 20 additions and 10 deletions
|
|
@ -23,6 +23,7 @@ android_test_helper_app {
|
|||
defaults: ["cts_support_defaults"],
|
||||
sdk_version: "test_current",
|
||||
static_libs: [
|
||||
"androidx.annotation_annotation",
|
||||
"CtsHostsideNetworkTestsAidl",
|
||||
"NetworkStackApiStableShims",
|
||||
],
|
||||
|
|
|
|||
|
|
@ -30,6 +30,8 @@ import android.os.Bundle;
|
|||
import android.os.RemoteException;
|
||||
import android.util.Log;
|
||||
|
||||
import androidx.annotation.GuardedBy;
|
||||
|
||||
import com.android.cts.net.hostside.INetworkStateObserver;
|
||||
|
||||
/**
|
||||
|
|
@ -37,6 +39,7 @@ import com.android.cts.net.hostside.INetworkStateObserver;
|
|||
*/
|
||||
public class MyActivity extends Activity {
|
||||
|
||||
@GuardedBy("this")
|
||||
private BroadcastReceiver finishCommandReceiver = null;
|
||||
|
||||
@Override
|
||||
|
|
@ -47,8 +50,11 @@ public class MyActivity extends Activity {
|
|||
|
||||
@Override
|
||||
public void finish() {
|
||||
if (finishCommandReceiver != null) {
|
||||
unregisterReceiver(finishCommandReceiver);
|
||||
synchronized (this) {
|
||||
if (finishCommandReceiver != null) {
|
||||
unregisterReceiver(finishCommandReceiver);
|
||||
finishCommandReceiver = null;
|
||||
}
|
||||
}
|
||||
super.finish();
|
||||
}
|
||||
|
|
@ -71,14 +77,17 @@ public class MyActivity extends Activity {
|
|||
super.onResume();
|
||||
Log.d(TAG, "MyActivity.onResume(): " + getIntent());
|
||||
Common.notifyNetworkStateObserver(this, getIntent(), TYPE_COMPONENT_ACTIVTY);
|
||||
finishCommandReceiver = new BroadcastReceiver() {
|
||||
@Override
|
||||
public void onReceive(Context context, Intent intent) {
|
||||
Log.d(TAG, "Finishing MyActivity");
|
||||
MyActivity.this.finish();
|
||||
}
|
||||
};
|
||||
registerReceiver(finishCommandReceiver, new IntentFilter(ACTION_FINISH_ACTIVITY));
|
||||
synchronized (this) {
|
||||
finishCommandReceiver = new BroadcastReceiver() {
|
||||
@Override
|
||||
public void onReceive(Context context, Intent intent) {
|
||||
Log.d(TAG, "Finishing MyActivity");
|
||||
MyActivity.this.finish();
|
||||
}
|
||||
};
|
||||
registerReceiver(finishCommandReceiver, new IntentFilter(ACTION_FINISH_ACTIVITY),
|
||||
Context.RECEIVER_EXPORTED);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
|||
Loading…
Reference in a new issue