Advertisement
sidson

Send Ussd

Nov 15th, 2023
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.50 KB | None | 0 0
  1.  
  2.     private void doJob() {
  3.           makeUssdRequest("#176#");
  4.     }
  5.  
  6.     private void makeUssdRequest(String ussd ) {
  7.         showLoaderProgressBar();
  8.         TelephonyManager telephonyManager = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);
  9.  
  10.         if (telephonyManager != null) {
  11.             telephonyManager.sendUssdRequest(ussd, new TelephonyManager.UssdResponseCallback() {
  12.                 @Override
  13.                 public void onReceiveUssdResponse(TelephonyManager telephonyManager, String request,
  14.                                                   CharSequence response) {
  15.                     Log.d("USSD", "USSD Request: " + request);
  16.                     Log.d("USSD", "USSD Response: " + response.toString());
  17.                     dismissLoaderProgressBar();
  18.                     showResponseDialog(response.toString(), MainActivity.this);
  19.                 }
  20.  
  21.                 @Override
  22.                 public void onReceiveUssdResponseFailed(TelephonyManager telephonyManager, String request,
  23.                                                         int failureCode) {
  24.                     Log.d("USSD", "USSD Request: " + request);
  25.                     Log.d("USSD", "USSD Failure Code: " + failureCode);
  26.                     dismissLoaderProgressBar();
  27.                     // Handle failure, if needed
  28.                 }
  29.             }, null);
  30.         }
  31.     }
  32.  
  33.     private void showResponseDialog(String response, MainActivity activity) {
  34.         activity.runOnUiThread(new Runnable() {
  35.             @Override
  36.             public void run() {
  37.  
  38.                 String responseString = response.toString().trim();
  39.                 String[] responseParts = responseString.split(",");
  40.  
  41.                 StringBuilder formattedResponse = new StringBuilder();
  42.                 int count = 0;
  43.                 for (int index = 0; index < responseParts.length; index++) {
  44.                     String part = responseParts[index];
  45.                     formattedResponse.append((index + 1) + ". " + part + "\n");
  46.                     count++;
  47.                 }
  48.  
  49.  
  50.                 // Create an EditText for user input
  51.                 final EditText input = new EditText(activity);
  52.                 input.setInputType(InputType.TYPE_CLASS_TEXT); // Set the input type as needed
  53.  
  54.  
  55.                 // Creating an AlertDialog Builder
  56.                 AlertDialog.Builder builder = new AlertDialog.Builder(activity);
  57.  
  58.                 // Setting dialog title and message
  59.                 int finalCount = count;
  60.                 builder.setTitle("PayTech USSD Response")
  61.                         .setMessage(formattedResponse)
  62.                         .setPositiveButton("OK", (dialog, which) -> {
  63.                             dialog.dismiss();
  64.  
  65.                             if(finalCount > 1){
  66.                                 String userInput = input.getText().toString();
  67.  
  68.                                 makeUssdRequest(userInput);
  69.                             }
  70.  
  71.                         });
  72.  
  73.                 if(count > 1){
  74.                     builder =  builder.setNegativeButton("Cancel", (dialog, which) -> {
  75.                         // Negative button click action if condition is false
  76.                         dialog.dismiss(); // Dismiss the dialog
  77.                     }).setView(input);
  78.                 }
  79.  
  80.                 // Creating the AlertDialog object
  81.                 AlertDialog dialog = builder.create();
  82.  
  83.                 // Show the dialog
  84.                 dialog.show();
  85.             }
  86.         });
  87.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement