Advertisement
Guest User

Untitled

a guest
May 22nd, 2019
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.99 KB | None | 0 0
  1. package com.tmobile.pr.mytmobile;
  2.  
  3. import android.app.Activity;
  4. import android.content.Context;
  5. import android.os.AsyncTask;
  6. import android.os.Bundle;
  7. import android.telephony.AccessNetworkConstants;
  8. import android.telephony.CellInfo;
  9. import android.telephony.NetworkScanRequest;
  10. import android.telephony.RadioAccessSpecifier;
  11. import android.telephony.TelephonyManager;
  12. import android.telephony.TelephonyScanManager;
  13. import android.util.Log;
  14. import android.view.View;
  15. import android.widget.ArrayAdapter;
  16. import android.widget.Button;
  17. import android.widget.Spinner;
  18. import android.widget.TextView;
  19. import android.widget.Toast;
  20.  
  21. import java.util.List;
  22.  
  23. public class MainActivity extends Activity implements View.OnClickListener {
  24.  
  25. private static final String TAG = "MainActivity";
  26.  
  27. private TextView textView;
  28.  
  29.  
  30. @Override
  31. protected void onCreate(Bundle savedInstanceState) {
  32. super.onCreate(savedInstanceState);
  33. setContentView(R.layout.activity_main);
  34. Button buttonMain = findViewById(R.id.button_main);
  35. buttonMain.setOnClickListener(this);
  36. textView = findViewById(R.id.tv1);
  37.  
  38. Spinner spinner = findViewById(R.id.spinner1);
  39. ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
  40. R.array.networklist_array, android.R.layout.simple_spinner_item);
  41. adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
  42. spinner.setAdapter(adapter);
  43.  
  44. }
  45.  
  46. public void onClick(View view) {
  47.  
  48. Log.d(TAG, "onClick: MainActivity");
  49.  
  50. Toast.makeText(this, "Click context", Toast.LENGTH_SHORT).show();
  51.  
  52. NetworkScanRequest networkScanRequest;
  53. RadioAccessSpecifier[] radioAccessSpecifiers;
  54.  
  55. Spinner spinner = findViewById(R.id.spinner1);
  56. String selectedText=spinner.getSelectedItem().toString();
  57. String[] networkOptions=getResources().getStringArray(R.array.networklist_array);
  58. int networkSelection=AccessNetworkConstants.AccessNetworkType.EUTRAN;
  59. if(networkOptions[0].contentEquals(selectedText))
  60. {
  61. networkSelection=AccessNetworkConstants.AccessNetworkType.EUTRAN;
  62. }
  63. else if(networkOptions[1].contentEquals(selectedText))
  64. networkSelection=AccessNetworkConstants.AccessNetworkType.UTRAN;
  65. else if(networkOptions[2].contentEquals(selectedText))
  66. networkSelection=AccessNetworkConstants.AccessNetworkType.GERAN;
  67. else if(networkOptions[3].contentEquals(selectedText))
  68. networkSelection=AccessNetworkConstants.AccessNetworkType.IWLAN;
  69. else if(networkOptions[4].contentEquals(selectedText))
  70. networkSelection=AccessNetworkConstants.AccessNetworkType.CDMA2000;
  71.  
  72. radioAccessSpecifiers = new RadioAccessSpecifier[0];
  73. radioAccessSpecifiers[0] = new RadioAccessSpecifier(
  74. networkSelection,
  75. null,
  76. null);
  77.  
  78. TelephonyManager tManager = (TelephonyManager) getApplicationContext().getSystemService(Context.TELEPHONY_SERVICE);
  79.  
  80. networkScanRequest = new NetworkScanRequest(
  81. NetworkScanRequest.SCAN_TYPE_PERIODIC,
  82. radioAccessSpecifiers,
  83. /**
  84. * Search periodicity (in seconds).
  85. * Expected range for the input is [5s - 300s]
  86. * This value must be less than or equal to mMaxSearchTime
  87. */
  88. 30,
  89. /**
  90. * Maximum duration of the periodic search (in seconds).
  91. * Expected range for the input is [60s - 3600s] //[1m - 1hr]
  92. * If the search lasts this long, it will be terminated.
  93. */
  94. 300,
  95. /**
  96. * Indicates whether the modem should report incremental
  97. * results of the network scan to the client.
  98. * FALSE – Incremental results are not reported.
  99. * TRUE (default) – Incremental results are reported
  100. */
  101. true,
  102. /**
  103. * Indicates the periodicity with which the modem should
  104. * report incremental results to the client (in seconds).
  105. * Expected range for the input is [1s - 10s]
  106. * This value must be less than or equal to mMaxSearchTime
  107. */
  108. 3,
  109. null);
  110.  
  111. tManager.requestNetworkScan(networkScanRequest, AsyncTask.SERIAL_EXECUTOR, new ResponseCallback());
  112.  
  113. }
  114.  
  115. private class ResponseCallback extends TelephonyScanManager.NetworkScanCallback {
  116. private List<CellInfo> mCellInfoResults;
  117. private int mOnError;
  118.  
  119. @Override
  120. public void onResults(List<CellInfo> cellInfoResults) {
  121. this.mCellInfoResults = cellInfoResults;
  122. MainActivity.this.runOnUiThread(new Runnable() {
  123. @Override
  124. public void run() {
  125. for (CellInfo cellInfo : ResponseCallback.this.mCellInfoResults) {
  126. Log.d(TAG, "run: onResults");
  127. textView.append(" " + cellInfo.toString() + " ");
  128. }
  129. }
  130. });
  131. }
  132.  
  133. @Override
  134. public void onError(int error) {
  135. mOnError = error;
  136. MainActivity.this.runOnUiThread(new Runnable() {
  137. @Override
  138. public void run() {
  139. Log.d(TAG, "run: onError");
  140. textView.append(" Error: " + mOnError);
  141. }
  142. });
  143. }
  144.  
  145. @Override
  146. public void onComplete() {
  147. MainActivity.this.runOnUiThread(new Runnable() {
  148. @Override
  149. public void run() {
  150. Log.d(TAG, "run: onComplete");
  151. textView.append(" onComplete ");
  152. }
  153. });
  154. }
  155. }
  156. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement