Stop query request while app isn't active

This commit is contained in:
Pavle Portic 2019-01-17 00:45:59 +01:00
parent 4cc392067d
commit e0ca1627e7
Signed by: TheEdgeOfRage
GPG Key ID: 6758ACE46AA2A849
2 changed files with 61 additions and 33 deletions

View File

@ -1,13 +1,13 @@
apply plugin: 'com.android.application' apply plugin: 'com.android.application'
android { android {
compileSdkVersion 27 compileSdkVersion 28
defaultConfig { defaultConfig {
applicationId "me.xor_hydrogen.i3control" applicationId "com.theedgeofrage.i3control"
minSdkVersion 27 minSdkVersion 27
targetSdkVersion 27 targetSdkVersion 28
versionCode 1 versionCode 2
versionName "1.0" versionName "1.0.1"
vectorDrawables.useSupportLibrary true vectorDrawables.useSupportLibrary true
} }
buildTypes { buildTypes {
@ -20,7 +20,7 @@ android {
dependencies { dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar']) implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.android.support:appcompat-v7:27.1.1' implementation 'com.android.support:appcompat-v7:28.0.0'
implementation 'com.android.support.constraint:constraint-layout:1.1.3' implementation 'com.android.support.constraint:constraint-layout:1.1.3'
implementation 'com.android.volley:volley:1.1.0' implementation 'com.android.volley:volley:1.1.0'
} }

View File

@ -24,6 +24,44 @@ import org.json.JSONException;
import org.json.JSONObject; import org.json.JSONObject;
public class MainActivity extends AppCompatActivity { public class MainActivity extends AppCompatActivity {
class RunnableQuery implements Runnable {
private Handler mHandler;
private boolean mRun = false;
private MainActivity context;
private RunnableQuery(MainActivity context) {
mHandler = new Handler(getMainLooper());
this.context = context;
}
private void start() {
if (mRun) {
return;
}
mRun = true;
mHandler.postDelayed(this, 1000);
}
void stop() {
if (!mRun) {
return;
}
mRun = false;
mHandler.removeCallbacks(this);
}
@Override
public void run() {
if (!mRun) {
return;
}
context.sendRequest("query", context);
mHandler.postDelayed(this, 1000);
}
}
RunnableQuery runnableQuery;
public void sendRequest(String endpoint, final Context context) { public void sendRequest(String endpoint, final Context context) {
RequestQueue queue = Volley.newRequestQueue(context); RequestQueue queue = Volley.newRequestQueue(context);
@ -119,35 +157,25 @@ public class MainActivity extends AppCompatActivity {
findViewById(R.id.nextButton).setOnClickListener(buttonClickListener); findViewById(R.id.nextButton).setOnClickListener(buttonClickListener);
findViewById(R.id.shuffleButton).setOnClickListener(buttonClickListener); findViewById(R.id.shuffleButton).setOnClickListener(buttonClickListener);
class RunnableQuery implements Runnable { this.runnableQuery = new RunnableQuery(this);
private Handler mHandler; this.runnableQuery.start();
private boolean mRun = false; }
RunnableQuery() { @Override
mHandler = new Handler(getMainLooper()); protected void onResume() {
} super.onResume();
this.runnableQuery.start();
}
public void start() { @Override
mRun = true; protected void onStop() {
mHandler.postDelayed(this, 1000); super.onStop();
} this.runnableQuery.stop();
}
void stop() { @Override
mRun = false; protected void onPause() {
mHandler.removeCallbacks(this); super.onPause();
} this.runnableQuery.stop();
@Override
public void run() {
if (!mRun) {
return;
}
((MainActivity) context).sendRequest("query", context);
mHandler.postDelayed(this, 1000);
}
}
RunnableQuery runnableQuery = new RunnableQuery();
runnableQuery.start();
} }
} }