Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- private void doJob() {
- makeUssdRequest("#176#");
- }
- private void makeUssdRequest(String ussd ) {
- showLoaderProgressBar();
- TelephonyManager telephonyManager = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);
- if (telephonyManager != null) {
- telephonyManager.sendUssdRequest(ussd, new TelephonyManager.UssdResponseCallback() {
- @Override
- public void onReceiveUssdResponse(TelephonyManager telephonyManager, String request,
- CharSequence response) {
- Log.d("USSD", "USSD Request: " + request);
- Log.d("USSD", "USSD Response: " + response.toString());
- dismissLoaderProgressBar();
- showResponseDialog(response.toString(), MainActivity.this);
- }
- @Override
- public void onReceiveUssdResponseFailed(TelephonyManager telephonyManager, String request,
- int failureCode) {
- Log.d("USSD", "USSD Request: " + request);
- Log.d("USSD", "USSD Failure Code: " + failureCode);
- dismissLoaderProgressBar();
- // Handle failure, if needed
- }
- }, null);
- }
- }
- private void showResponseDialog(String response, MainActivity activity) {
- activity.runOnUiThread(new Runnable() {
- @Override
- public void run() {
- String responseString = response.toString().trim();
- String[] responseParts = responseString.split(",");
- StringBuilder formattedResponse = new StringBuilder();
- int count = 0;
- for (int index = 0; index < responseParts.length; index++) {
- String part = responseParts[index];
- formattedResponse.append((index + 1) + ". " + part + "\n");
- count++;
- }
- // Create an EditText for user input
- final EditText input = new EditText(activity);
- input.setInputType(InputType.TYPE_CLASS_TEXT); // Set the input type as needed
- // Creating an AlertDialog Builder
- AlertDialog.Builder builder = new AlertDialog.Builder(activity);
- // Setting dialog title and message
- int finalCount = count;
- builder.setTitle("PayTech USSD Response")
- .setMessage(formattedResponse)
- .setPositiveButton("OK", (dialog, which) -> {
- dialog.dismiss();
- if(finalCount > 1){
- String userInput = input.getText().toString();
- makeUssdRequest(userInput);
- }
- });
- if(count > 1){
- builder = builder.setNegativeButton("Cancel", (dialog, which) -> {
- // Negative button click action if condition is false
- dialog.dismiss(); // Dismiss the dialog
- }).setView(input);
- }
- // Creating the AlertDialog object
- AlertDialog dialog = builder.create();
- // Show the dialog
- dialog.show();
- }
- });
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement