@math_man

Проблема с потоками и СМС?

Пытаюсь отправить смс програмно. Но при создания обьекта SMS - ошибка:
E/AndroidRuntime: FATAL EXCEPTION: Thread-25964
                  Process: com.example.pilig.sms3, PID: 30874
                  java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
                      at android.os.Handler.<init>(Handler.java:209)
                      at android.os.Handler.<init>(Handler.java:123)
                      at android.app.Activity.<init>(Activity.java:788)
                      at com.example.pilig.sms3.SMS.<init>(SMS.java:0)
                      at com.example.pilig.sms3.End.sendSMS(End.java:107)
                      at com.example.pilig.sms3.Requester$1.run(Requester.java:59)
                      at java.lang.Thread.run(Thread.java:818)

Класс SMS:
package com.example.pilig.sms3;

import android.app.Activity;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.telephony.SmsManager;

/**
 * Created by pilig on 11.01.2017.
 */

public class SMS extends Activity {
    String phoneNo,  message, id;
    Requester requester;
    SMS(){}

    SMS(String phoneNo, String message, String id, Requester requester) {
        this.requester = requester;
        this.phoneNo = phoneNo;
        this.message = message;
        this.id = id;
        sendSMS();
    }

    public void sendSMS(){
        String SENT="SMS_SENT";
        String DELIVERED="SMS_DELIVERED";
        PendingIntent sentPI= PendingIntent.getBroadcast(getBaseContext(),0,
                new Intent(SENT),0);

        PendingIntent deliveredPI= PendingIntent.getBroadcast(getBaseContext(),0,
                new Intent(DELIVERED),0);
        registerReceiver(new BroadcastReceiver(){
            @Override
            public void onReceive(Context arg0, Intent arg1){
                switch(getResultCode())
                {
                    case Activity.RESULT_OK:
                        requester.setStatus(id, "2");
                        Account.sent++;
                        break;
                    case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
                        break;
                    case SmsManager.RESULT_ERROR_NO_SERVICE:
                        break;
                    case SmsManager.RESULT_ERROR_NULL_PDU:
                        break;
                    case SmsManager.RESULT_ERROR_RADIO_OFF:
                        break;
                }
            }
        },new IntentFilter(SENT));
        registerReceiver(new BroadcastReceiver(){
            @Override
            public void onReceive(Context arg0, Intent arg1){
                switch(getResultCode())
                {
                    case Activity.RESULT_OK:{
                        requester.setStatus(id, "3");
                        Account.get++;
                        Account.list.remove(this);
                        break;
                    }

                    case Activity.RESULT_CANCELED:{
                        requester.setStatus(id, "4");
                        Account.notGet++;
                        Account.list.remove(this);
                        break;
                    }

                }
            }
        },new IntentFilter(DELIVERED));
        SmsManager sms = SmsManager.getDefault();
        sms.sendTextMessage(phoneNo,null, message, sentPI, deliveredPI);

    }
}


Создаю обьект SMS в следующем методе:
public void getSMSToSend()  {
        Thread thread = new Thread(new Runnable() {
            @Override
            public void run() {
                while(flag){
                    String sql = "http://80.252.241.43/telephony/android/get_sms.php?login=" + Account.login + "&password=" + Account.password;
                    //System.out.println(sql);
                    String text = request(sql);
                    System.out.println(text);
                    String phone = text.substring(0, text.indexOf("|"));
                    text = text.substring(text.indexOf("|")+1);
                    String sms = text.substring(0, text.indexOf("|"));
                    String id = text.substring(text.indexOf("|")+1);
                    if(!id.equals("")&&!sms.equals("")&&!phone.equals("")) {
                        <i><b>SMS message = new SMS(phone, sms, id, getThis());</b></i>
                        Account.list.add(message);
                    }
                }
            }
        });
        thread.start();

    }
  • Вопрос задан
  • 232 просмотра
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Похожие вопросы