Lab-stateMachine NetworkCommunication Telephon
Lab-stateMachine NetworkCommunication Telephon
Below is a complete demo that demonstrates how to implement and display the activity of a state
machine in an Android application using Java. We'll use a basic state machine to represent a
login flow (Idle, Logging In, Login Successful, Login Failed) and visualize the transitions
in the UI.
public LoginStateMachine() {
currentState = State.IDLE;
}
xml
Copy code
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="16dp">
<TextView
android:id="@+id/stateTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Current State: IDLE"
android:textSize="18sp"
android:layout_gravity="center_horizontal"
android:layout_marginBottom="16dp"/>
<Button
android:id="@+id/startLoginButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Start Login"/>
<Button
android:id="@+id/loginSuccessButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Login Success"
android:layout_marginBottom="8dp" />
<Button
android:id="@+id/loginFailedButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Login Failed"
android:layout_marginTop="8dp" />
<Button
android:id="@+id/resetButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Reset"
android:layout_marginTop="8dp" />
</LinearLayout>
4. Implement State Transitions in MainActivity
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
updateUI();
startLoginButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
stateMachine.startLogin();
updateUI();
}
});
loginSuccessButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
stateMachine.loginSuccess();
updateUI();
}
});
loginFailedButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
stateMachine.loginFailed();
updateUI();
}
});
resetButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
stateMachine.reset();
updateUI();
}
});
}
6. Explanation
Network Communication
<Button
android:id="@+id/fetchButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Fetch Data" />
<TextView
android:id="@+id/resultTextView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Result will appear here..."
android:textSize="16sp"
android:padding="16dp"
android:gravity="start|top"
android:scrollbars="vertical" />
</LinearLayout>
In the MainActivity.java file, use a background thread to perform the network operation.
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
//import androidx.appcompat.app.AppCompatActivity;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import android.os.Handler;
import android.os.Looper;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
resultTextView = findViewById(R.id.resultTextView);
fetchButton = findViewById(R.id.fetchButton);
fetchButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
fetchData();
}
});
}
6. Explanation
Telephony
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.telephonydemo">
<application
android:allowBackup="true"
android:label="Telephony Demo"
android:theme="@style/Theme.AppCompat">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
<TextView
android:id="@+id/tvTelephonyInfo"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Telephony Information"
android:textSize="18sp"
android:textStyle="bold"
android:layout_marginBottom="16dp" />
<Button
android:id="@+id/btnFetchInfo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Fetch Telephony Info" />
</LinearLayout>
Create a Java file (MainActivity.java) and add the following code to fetch telephony details.
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.Manifest;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.telephony.TelephonyManager;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tvTelephonyInfo = findViewById(R.id.tvTelephonyInfo);
Button btnFetchInfo = findViewById(R.id.btnFetchInfo);
// Initialize TelephonyManager
telephonyManager = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);
btnFetchInfo.setOnClickListener(v -> {
if (ActivityCompat.checkSelfPermission(this,
Manifest.permission.READ_PHONE_STATE) != PackageManager.PERMISSION_GRANTED) {
// Request necessary permissions
ActivityCompat.requestPermissions(this, new
String[]{Manifest.permission.READ_PHONE_STATE}, PERMISSION_REQUEST_CODE);
} else {
fetchTelephonyInfo();
}
});
}
// Display details
tvTelephonyInfo.setText(info.toString());
} catch (SecurityException e) {
Toast.makeText(this, "Permission Denied!", Toast.LENGTH_SHORT).show();
}
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[]
permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (requestCode == PERMISSION_REQUEST_CODE) {
if (grantResults.length > 0 && grantResults[0] ==
PackageManager.PERMISSION_GRANTED) {
fetchTelephonyInfo();
} else {
Toast.makeText(this, "Permission is required to fetch telephony
information", Toast.LENGTH_SHORT).show();
}
}
}
1. Build and run the app on a physical device (not an emulator, as telephony features often require
actual hardware).
2. Click the "Fetch Telephony Info" button.
3. Grant the necessary permissions when prompted.
Output
Notes
1. IMEI Access Restrictions: From Android 10 (API level 29) onward, apps cannot access the IMEI
unless they are system apps or default dialers.
2. Testing: Use a physical device with SIM card inserted, as the emulator may not provide
telephony data.
3. Permissions: Make sure to handle runtime permissions gracefully for a smooth user experience