Skip to content

Commit

Permalink
Improve service status handling + set interval to 90 secs
Browse files Browse the repository at this point in the history
  • Loading branch information
dic1911 committed May 5, 2020
1 parent 29e50c1 commit 9d6908c
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 8 deletions.
Binary file added app/release/PseudoSpeedTest_dic1911.apk
Binary file not shown.
Binary file removed app/release/app-release.apk
Binary file not shown.
37 changes: 30 additions & 7 deletions app/src/main/java/moe/dic1911/test4speed/MainActivity.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package moe.dic1911.test4speed;

import android.app.ActivityManager;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
Expand All @@ -19,13 +20,21 @@ public class MainActivity extends AppCompatActivity {

private Button btn_run, btn_svc;
private NetworkService svc;
private boolean svcStarted;
private static boolean svcStarted;
private static Intent svcIntent;

@Override
protected void onResume() {
super.onResume();
svcStarted = isServiceRunning(NetworkService.class);
btn_svc.setText(svcStarted ? getString(R.string.stop_svc) : getString(R.string.start_svc));
}

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
svcStarted = false;
svcStarted = isServiceRunning(NetworkService.class);
btn_run = findViewById(R.id.btn_run);
btn_svc = findViewById(R.id.btn_svc);
btn_run.setOnClickListener(new View.OnClickListener() {
Expand All @@ -35,17 +44,31 @@ public void onClick(View v) {
}
});

// not working rn
btn_svc.setEnabled(!svcStarted);
if (svcIntent == null) svcIntent = new Intent(getApplicationContext(), NetworkService.class);
//btn_svc.setEnabled(!svcStarted);
btn_svc.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (!svcStarted) {
getApplicationContext().startService(new Intent(getApplicationContext(), NetworkService.class));
svcStarted = true;
btn_svc.setEnabled(!svcStarted);
getApplicationContext().startService(svcIntent);
//btn_svc.setEnabled(!svcStarted);
} else {
getApplicationContext().stopService(svcIntent);
}
svcStarted = !svcStarted;
btn_svc.setText(svcStarted ? getString(R.string.stop_svc) : getString(R.string.start_svc));
}
});
}

// credit: Peter Mortensen and geekQ from StackOverflow
private boolean isServiceRunning(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())) {
return true;
}
}
return false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

public class NetworkService extends Service {

private static final int interval = 2 * 1000; // 2 min
private static final int interval = 90 * 1000; // 1.5 min
private static final String CHANNEL_ID = "default";
private static android.app.Notification noti;

Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/values-zh-rTW/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@
<string name="app_desc">這只是一個簡單的App可以戳一下SpeedTest讓電信業者暫時解除限速,啟用服務之後要停用的話請使用Android的\"強制停止\"</string>
<string name="start">執行</string>
<string name="start_svc">啟動服務</string>
<string name="stop_svc">停止服務</string>
</resources>
1 change: 1 addition & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@
<string name="app_desc">This is a simple app that make dummy request to SpeedTest for unleash your mobile data speed.\nUse "Force Stop" to stop the service for now.</string>
<string name="start">Run Once</string>
<string name="start_svc">Start service</string>
<string name="stop_svc">Stop service</string>
</resources>

0 comments on commit 9d6908c

Please sign in to comment.