java - Android Twilio 如何使用这些示例类?

标签 java android twilio

我正在编写一个将使用 Twilio 的 Android 应用程序,但显然,我是 Android 新手。我只是想看看是否有人可以解释如何使用 Twilio's site 中的这些示例类?

如果我创建一个名为 CallRetriever 的新 Java 类,如何在发送 ACCOUNT_SIDAUTH_TOKEN 时在我的主要 Activity 中调用它或者其他参数?

import java.util.Map;
import java.util.HashMap;

import com.twilio.sdk.TwilioRestClient;
import com.twilio.sdk.TwilioRestException;
import com.twilio.sdk.resource.instance.Account;
import com.twilio.sdk.resource.instance.Call;
import com.twilio.sdk.resource.list.CallList;

public class CallRetriever {

    // The customer's Account Sid
    public static final String ACCOUNT_SID = "AC123";

    // Your own Auth Token
    public static final String AUTH_TOKEN = "456bef";

    public static void main(String[] args) throws TwilioRestException {

        TwilioRestClient client = new TwilioRestClient(ACCOUNT_SID, AUTH_TOKEN);
        Account mainAccount = client.getAccount();
        CallList calls = mainAccount.getCalls();
        for (Call call : calls) {
            System.out.println("From: " + call.getFrom() + " To: " + call.getTo());
        }
    }
}

更新这是代码所在的位置。

import...
public class StartPage extends Activity {
    ...
    private String twilio_account_sid;
    private String twilio_auth_token;
    ...
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        twilio_account_sid = booth_preferences.getString("twilio_account_sid", "");
        twilio_auth_token = getString(R.string.twilio_auth_token);

        twilio_login_button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //Do a post to a PHP page to get user SID
                twilio_web_view.getSettings().setJavaScriptEnabled(true);
                twilio_web_view.setWebViewClient(new WebViewClient() {
                    public boolean shouldOverrideUrlLoading(WebView view, String url){
                        requested urlredirect:
                        view.loadUrl(url);
                        if(url.contains("?")) {
                            String[] url_split = url.split("\\?");
                            if(url_split[1].contains("&")){
                                String[] url_values = url_split[1].split("&");
                                //Get splits, probably not necessary
                            } else {
                                //GET SID from URL
                                String[] url_values = url_split[1].split("=");
                                String type = url_values[0];
                                String value = url_values[1];
                                Log.d("*******", type + "::" + value);
                                edit_preferences.putString("twilio_account_sid", value).apply();
                                twilio_web_view.setVisibility(View.GONE);
                                Toast.makeText(getApplicationContext(), "Logged in to Twilio " + twilio_account_sid, Toast.LENGTH_LONG).show();
                                twilio_login_button.setVisibility(View.GONE);
                                twilio_logout_button.setVisibility(View.VISIBLE);

                                //THIS IS WHERE I NEED THE CLASS TO RUN.
                                //I NEED TO SEND THE NEW SID AND AUTH TOKEN
                                TwilioRestClient client = new TwilioRestClient(twilio_account_sid, getString(R.string.twilio_auth_token));
                                Account mainAccount = client.getAccount();
                                CallList calls = mainAccount.getCalls();
                                for (Call call : calls) {
                                    System.out.println("From: " + call.getFrom() + " To: " + call.getTo());
                                }
                            }
                        }
                        return false; // then it is not handled by default action
                    }
                });

            twilio_web_view.loadUrl("https://www.twilio.com/authorize/CN546428ff3127adbab58eb73db7c7dc93");
                //setContentView(twilio_web_view );
                twilio_web_view.setVisibility(View.VISIBLE);
            }
        });
    }
}

任何帮助都会很棒!

最佳答案

如果假设我有一个 Activity

public class MainActivity extends Activity{

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.Yourlayout);

    CallList calls =  CallRetriever.getAllCalls("ACCOUNT_SID_value","AUTH_TOKEN_value");
    //calls contains the final result
     for (Call call : calls) {
        System.out.println("From: " + call.getFrom() + " To: " + call.getTo());
    }
 } 
}

现在自定义CallRetriever如下所示

public class CallRetriever {

// The customer's Account Sid
public static final String ACCOUNT_SID = "AC123";

// Your own Auth Token
public static final String AUTH_TOKEN = "456bef";

public static CallList getAllCalls(String ACCOUNT_SID,String AUTH_TOKEN) throws TwilioRestException {

    TwilioRestClient client = new TwilioRestClient(ACCOUNT_SID, AUTH_TOKEN);
    Account mainAccount = client.getAccount();
    retrun mainAccount.getCalls();

  }
}

关于java - Android Twilio 如何使用这些示例类?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36627867/

相关文章:

android - 在 IntentService 中使用 LocationManager 似乎不起作用

Android GLES20 big textured quad 非常慢(在 Nexus One 上)

android - 从 FragmentPagerAdapter 动态添加和删除 fragment

java - 如何使用 java tomcat 应用程序在 twilio 中接收和存储传入消息?

Android Twilio 错误 "31003"

java - 为具有自己的 JSON 序列化实现的类编写 Gson TypeAdapter

java - 以编程方式打开黑莓设备选项

java - 在几乎没有负载的情况下以137退出的Docker容器在有负载的情况下运行良好

Java Keytool 在生成 CSR 时剥离 SAN

ios - twilio API : how can I play an audio file to both sides of a voice call and not disconnect the call?