How to send SMS using Twilio in my android application?

后端 未结 6 1777
既然无缘
既然无缘 2020-12-08 17:15

In my android application I have created one button, when I had pressed on the button I want to send message.So for that I have created one java class and written twilio cod

6条回答
  •  既然无缘
    2020-12-08 17:54

    This is how I solved my need. public class TwilioAsyncTask extends AsyncTask {

            Context context;
            ProgressDialog progressDialog;
    
    
            public TwilioAsyncTask(Context context) {
                this.context = context;
            }
    
            @Override
            protected String doInBackground(String... strings) {
    
                //
                HttpClient httpclient = new DefaultHttpClient();
    
                HttpPost httppost = new HttpPost(
                        "https://api.twilio.com/2010-04-01/Accounts/AC_yourACCOUNT_SID_9b/SMS/Messages");
                String base64EncodedCredentials = "Basic "
                        + Base64.encodeToString(
                        (ACCOUNT_SID + ":" + AUTH_TOKEN).getBytes(),
                        Base64.NO_WRAP);
    
                httppost.setHeader("Authorization",
                        base64EncodedCredentials);
                try {
    
                    int randomPIN = (int) (Math.random() * 9000) + 1000;
                    String randomVeriValue = "" + randomPIN;
    // these are for control in other anctivity used sharepreference 
                    editorTwilio.putString("twilio_veri_no", randomVeriValue);
                    editorTwilio.commit();
    
                    List nameValuePairs = new ArrayList();
                    nameValuePairs.add(new BasicNameValuePair("From",
                            "+148******")); // what number they gave you
                    nameValuePairs.add(new BasicNameValuePair("To",
                            "+90" + phoneNo)); // your phone or our customers
                    nameValuePairs.add(new BasicNameValuePair("Body",
                            "Your verification number is : " + randomVeriValue));
    
                    httppost.setEntity(new UrlEncodedFormEntity(
                            nameValuePairs));
    
                    // Execute HTTP Post Request
                    HttpResponse response = httpclient.execute(httppost);
                    HttpEntity entity = response.getEntity();
                    System.out.println("Entity post is: "
                            + EntityUtils.toString(entity));
                    // Util.showMessage(mParentAct, "Welcome");
    
    
                } catch (ClientProtocolException e) {
    
                } catch (IOException e) {
    
                }
    
                //
                return "Executed";
            }
    
            @Override
            protected void onPostExecute(String result) {
                // execution of result of Long time consuming operation
                //progressDialog.dismiss();
            }
    
    
            @Override
            protected void onPreExecute() {
    
                progressDialog = ProgressDialog.show(context, "", " Wait for ");
    
            }
    
    
            @Override
            protected void onProgressUpdate(String... text) {
                // Things to be done while execution of long running operation is in
                // progress. For example updating ProgessDialog
            }
    
        }
    
    
        And call your Task
    
    
    TwilioAsyncTask task = new TwilioAsyncTask(CountryAndPhone.this);
                            task.execute();
    

提交回复
热议问题