I want to stop the code after 15 seconds of execut

2020-05-09 22:23发布

Sends multiple duplicate text messages. I want to send a text message to each contact once? I want to stop the code after 15 seconds of execution.

public class Async_sendSMS extends AsyncTask<Void, Void, Void>
        private Context contextTask;

        public Async_sendSMS(Context context) {
            contextTask=context;



        @Override
        protected Void doInBackground(Void... params) {
            try {
                    Thread.sleep(5000);
                    Cursor phones = contextTask.getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,null,null, null);
                    while (phones.moveToNext()){
                        String name = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
                        String phoneNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));

                        /**Send sms to all device contact numbers continuously.**/
                        sendSMS(contextTask,phoneNumber,"hello "+name+" "+contextTask.getResources().getString(R.string.msg));

                    }
                    phones.close();

            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            return null;
        }

        @Override
        protected void onPostExecute(Void result) {

            super.onPostExecute(result);
        }   

    }

}

0条回答
登录 后发表回答