通过AsyncTask在后台发送短信不起作用

时间:2015-07-11 01:06:14

标签: android android-asynctask smsmanager

点击按钮,我想将短信发送到从后台数据库获取的数字。如果我直接点击SmsManager点击它就可以了。但是通过AsyncTask它不起作用。请帮帮我。这是我的代码..

public class SendSmsActivity extends Activity {
    Button buttonSend;
    EditText textPhoneNo;
    Spinner level;
    EditText textSMS;


    private ArrayList studentInfo = new ArrayList();

    private DatabaseHelper mHelper;
    private SQLiteDatabase dataBase;
    private String levelSelected,sms;



    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.send_sms);

        buttonSend = (Button) findViewById(R.id.buttonSend);
        level = (Spinner) findViewById(R.id.spinnersms);
        textSMS = (EditText) findViewById(R.id.editTextSMS);
        String levelSelected = level.getSelectedItem().toString();
        String sms = textSMS.getText().toString();

        buttonSend.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {

                String levelSelected = level.getSelectedItem().toString();
                String sms = textSMS.getText().toString();
                //getphonenum(levelSelected, sms);

                new MySmsTask().execute(levelSelected, sms);



            }
        });
    }

    private void getphonenum(String levelSelected, String sms){

        mHelper = new DatabaseHelper(this);
        dataBase = mHelper.getWritableDatabase();
        Cursor mCursor = dataBase.rawQuery("SELECT Phone FROM student where level= '" + levelSelected + "'", null);
        studentInfo.clear();

        if (mCursor.moveToLast()) {
            do {

                String phoneNumber = (mCursor.getString(mCursor.getColumnIndex(DatabaseHelper.Phone)));
                Log.d("DEBUG", phoneNumber);



                try {
                    SmsManager smsManager = SmsManager.getDefault();
                    smsManager.sendTextMessage(phoneNumber, null, sms, null, null);
                    Toast.makeText(getApplicationContext(), "SMS Sent!",
                            Toast.LENGTH_LONG).show();
                } catch (Exception e) {
                    Toast.makeText(getApplicationContext(),
                            "SMS faild, please try again later!",
                            Toast.LENGTH_LONG).show();
                    e.printStackTrace();
                }
            }
            while (mCursor.moveToPrevious());
        }


    }

    private class MySmsTask extends AsyncTask<String, String, Integer> {


        @Override
        protected Integer doInBackground(String... params) {

            Toast.makeText(getBaseContext(), "Senescent "  + params, Toast.LENGTH_SHORT).show();

            getphonenum(levelSelected, sms);
            return null;

        }

        @Override
        protected void onPostExecute(Integer result) {
            Toast.makeText(getBaseContext(), "Sent "  + " messages", Toast.LENGTH_SHORT).show();
        }

        @Override
        protected void onPreExecute() {}


    }

}

1 个答案:

答案 0 :(得分:0)

你不能在doInBackground中直接使用Toast 使用处理程序来显示Toast消息可能是问题出现的原因。

请检查您的方法getphonenum(levelSelected,sms)是否被调用

private Handler handler = new Handler();

@Override 
protected Integer doInBackground(String... params) {       
    handler.post(new Runnable() {
                    @Override
                    public void run() {
                        try {
                             Toast.makeText(getBaseContext(), "Senescent " + params, Toast.LENGTH_SHORT).show();
                        } catch (JSONException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }

                    }

                });
    getphonenum(levelSelected, sms);
    return null;

}