Check if Sim-Card is available before starting the server

This commit is contained in:
Xcreen
2018-09-27 14:57:36 +02:00
parent 10861274ed
commit 1555730b77
2 changed files with 28 additions and 1 deletions

View File

@@ -1,8 +1,11 @@
package net.xcreen.restsms;
import androidx.fragment.app.Fragment;
import android.content.Context;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.telephony.TelephonyManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
@@ -29,6 +32,29 @@ public class HomeFragment extends Fragment {
toggleServerBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//Check if Device has a Sim-Card
try {
TelephonyManager telephonyManager = (TelephonyManager) getContext().getSystemService(Context.TELEPHONY_SERVICE);
int primarySim = telephonyManager.getSimState();
int secondarySim = TelephonyManager.SIM_STATE_ABSENT;
if (Build.VERSION.SDK_INT >= 26) {
primarySim = telephonyManager.getSimState(0);
secondarySim = telephonyManager.getSimState(1);
}
if (primarySim != TelephonyManager.SIM_STATE_READY) {
if (secondarySim != TelephonyManager.SIM_STATE_READY) {
//Device has not Sim-Card which is ready
Toast.makeText(getContext(), getResources().getText(R.string.invalid_sim), Toast.LENGTH_SHORT).show();
return;
}
}
}
catch (Exception ex){
ex.printStackTrace();
Toast.makeText(getContext(), getResources().getText(R.string.invalid_sim), Toast.LENGTH_SHORT).show();
return;
}
//Check if Server is running
if(appContext.smsServer.isRunning() && !appContext.smsServer.isStopping()){
//Stop Server

View File

@@ -14,4 +14,5 @@
<string name="stop_server">Stop Server</string>
<string name="server_is_stopping">Server is shutting down. Please wait.</string>
<string name="server_failed_to_start">Server failed to start!</string>
<string name="invalid_sim">Sim-Card is missing or not ready to send sms!</string>
</resources>