gpt4 book ai didi

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

转载 作者:搜寻专家 更新时间:2023-11-01 08:35:58 26 4
gpt4 key购买 nike

我正在编写一个将使用 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com