0

My android application crashes while sending Message to a list of 50 people. Anyone knows why this is happening then please do let me know.

  public class SendSMS extends AsyncTask<Void, Void, Void>{

private ArrayList<SearchResults>  list;
private String message;


public SendSMS(ArrayList<SearchResults> obj,String msg){
    this.list=obj;
    this.message=msg;
}

@Override
protected Void doInBackground(Void...params) {
    // TODO Auto-generated method stub


    String sms=message;
    SmsManager smsManager = SmsManager.getDefault();
    ArrayList<String> parts = smsManager.divideMessage(sms);

     for (int i=0;i<list.size();i++){


        Log.d("ListPhoneNumbers....", list.get(i).getPhoneNumber());




        smsManager.sendMultipartTextMessage(list.get(i).getPhoneNumber(), null, parts, null, null);
    }
    Toast.makeText(getApplicationContext(), "SMS Sent!",
            Toast.LENGTH_LONG).show();









    return null;
}




     }
4

2 回答 2

0

您应该在问题中添加更多详细信息。尤其是:

日志猫

涉及的一段代码

编辑:试试这个

public class SendSMS extends AsyncTask < Void, Void, Void > {

    private ArrayList < SearchResults > list;
    private String message;
    private Object context;


    public SendSMS(ArrayList < SearchResults > obj, String msg, Object context) {
        this.list = obj;
        this.message = msg;
        this.context = context;
    }

    @Override
    protected void doInBackground(Void... params) {

        String sms = message;
        SmsManager smsManager = SmsManager.getDefault();
        ArrayList < String > parts = smsManager.divideMessage(sms);

        for (int i = 0; i < list.size(); i++) {

            Log.d("ListPhoneNumbers....", list.get(i).getPhoneNumber());

            smsManager.sendMultipartTextMessage(list.get(i).getPhoneNumber(), null, parts, null, null);
        }

    }

    @Override
    protected void onPostExecute(Void result) {
        Toast.makeText(this.context, "SMS Sent!",
            Toast.LENGTH_LONG).show();
    }

}

您以这种方式从主 UI 调用:

(new SendSMS(/*PARAMS*/)).execute(this);
于 2013-07-12T12:31:31.337 回答
0

您的 Toast 消息应该在 runonui 线程中。您不能直接在 Async Task 中更新 UI ..

于 2013-07-12T12:40:18.990 回答