I want to stop the code after 15 seconds of execution

天涯浪子 提交于 2020-05-09 17:14:59

问题


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);
        }   

    }

}

来源:https://stackoverflow.com/questions/61162939/i-want-to-stop-the-code-after-15-seconds-of-execution

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!