Hello. There is a shop selling ICQ numbers of different lengths. The bottom line is that the Android user would use the QR code to download the application, press the button that sends SMS and in return receive the UIN and password. The problem is that the number to which the SMS is sent, as well as the text of the message are in 2 text files on the remote server. This is done to me for convenience. Here I provide the code that activates my application, but for some reason it does not work. If you assign values ​​to the lines of the number and message, then everything works fine, but you don’t want to read these values ​​from the files. Into the values ​​are well derived, but this method did not fit me.

Full activation code

package ru.example.uinpass; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.Reader; import java.net.URL; import java.net.URLConnection; import java.nio.charset.Charset; import ru.example.uinpass.R; import android.app.Activity; import android.app.PendingIntent; import android.content.Intent; import android.content.IntentFilter; import android.os.Bundle; import android.telephony.gsm.SmsManager; import android.util.Log; import android.view.View; import android.widget.Button; import android.widget.Toast; import android.content.BroadcastReceiver; import android.content.Context; import android.net.ConnectivityManager; import android.net.NetworkInfo; public class MainActivity extends Activity { Button btnSend; @Override public void onCreate(final Bundle savedInstanceState) { super.onCreate(savedInstanceState); this.setContentView(R.layout.activity_main); final Button btnSend = (Button)this.findViewById(R.id.btnSend); btnSend.setOnClickListener(new View.OnClickListener() { public void onClick(final View v) { new Thread(new Runnable() { @Override public void run() { try { final URL myURL = new URL("http://адрес-сайта.ру/number.txt"); final URLConnection connection = myURL.openConnection(); connection.setDoInput(true); final Reader reader = new BufferedReader(new InputStreamReader(connection.getInputStream(), Charset.forName("UTF-8"))); final URL myURL2 = new URL("http://адрес-сайта.ру/message.txt"); final URLConnection connection2 = myURL2.openConnection(); connection2.setDoInput(true); final Reader reader2 = new BufferedReader(new InputStreamReader(connection2.getInputStream(), Charset.forName("UTF-8"))); try { final char [] buffer1 = new char[1024]; final StringBuilder msg = new StringBuilder(); final char [] buffer2 = new char[1024]; final StringBuilder msg2 = new StringBuilder(); int readCount; do { readCount = reader.read(buffer1); msg.append(buffer1); } while ( readCount >= buffer1.length); int readCount2; do { readCount2 = reader2.read(buffer2); msg2.append(buffer2); } while (readCount2 >= buffer2.length); MainActivity.this.runOnUiThread(new Runnable() { @Override public void run() { String phoneNumber = new String(msg); String message = new String(msg2); Send(phoneNumber, message); } }); } catch (final IOException ex) { Log.d("IOException", ex.getMessage()); } finally { reader.close(); reader2.close(); } } catch (final Exception ex) { Log.d("Some exception", ex.getMessage()); } } }).start(); } }); } private void Send(String phoneNumber, String message) { { String SENT = "SMS_SENT"; String DELIVERED = "SMS_DELIVERED"; PendingIntent sentPI = PendingIntent.getBroadcast(this, 0, new Intent(SENT), 0); PendingIntent deliveredPI = PendingIntent.getBroadcast(this, 0, new Intent(DELIVERED), 0); registerReceiver(new BroadcastReceiver(){ @Override public void onReceive(Context arg0, Intent arg1) { switch (getResultCode()) { case Activity.RESULT_OK: Toast.makeText(getBaseContext(), "Ваш запрос отправляется", Toast.LENGTH_LONG).show(); break; case SmsManager.RESULT_ERROR_GENERIC_FAILURE: Toast.makeText(getBaseContext(), "Ошибка отправки", Toast.LENGTH_LONG).show(); break; case SmsManager.RESULT_ERROR_NO_SERVICE: Toast.makeText(getBaseContext(), "Ошибка отправки, попробуйте позднее", Toast.LENGTH_LONG).show(); break; case SmsManager.RESULT_ERROR_NULL_PDU: Toast.makeText(getBaseContext(), "Ошибка отправки, попробуйте позднее", Toast.LENGTH_LONG).show(); break; case SmsManager.RESULT_ERROR_RADIO_OFF: Toast.makeText(getBaseContext(), "Ошибка отправки, попробуйте позднее", Toast.LENGTH_LONG).show(); break; } } }, new IntentFilter(SENT)); registerReceiver(new BroadcastReceiver(){ @Override public void onReceive(Context arg0, Intent arg1) { switch (getResultCode()) { case Activity.RESULT_OK: Toast.makeText(getBaseContext(), "Спасибо! Смотрите UIN и пароль во входящем смс!", Toast.LENGTH_LONG).show(); break; case Activity.RESULT_CANCELED: Toast.makeText(getBaseContext(), "Ошибка доставки", Toast.LENGTH_LONG).show(); break; } } }, new IntentFilter(DELIVERED)); SmsManager sms = SmsManager.getDefault(); sms.sendTextMessage(phoneNumber, null, message, sentPI, deliveredPI); } } } 

Of course, in the manifesto sending and receiving SMS, as well as access to the Internet allowed. Please tell the beginner in detail what the problem is.

  • 3
    1) Where is the code being tested? 2) How do you know that it does not work? 3) why write so bad code? :( - DroidAlex
  • on the phone. just gives a toast send error (generic_failure). I have been studying for a week, I wrote as best I could. and how to? - Rammsteinik

1 answer 1

The answer is .

Regarding the code: the names of the methods, at least with a small letter, indents of the same width, the curly braces do not carry the initial brackets in java-style code. In short .... well.

  • About the brackets, there is already a matter of taste, I write on java, but I am terribly enraged that the brackets are not from a new line, I do not know why. - andreich
  • one
    if you want to help you, you’ll have to write the way everyone writes, tobish by JCC - Vladyslav Matviienko