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

时间:2016-04-14 15:40:44

标签: java android twilio

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

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

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

任何帮助都会很棒!

1 个答案:

答案 0 :(得分:3)

如果我有一个public final static UUID chara = UUID.fromString("custom 128bit UUID"); private void broadcastUpdate(final String action, final BluetoothGattCharacteristic characteristic) { final Intent intent = new Intent(action); if(chara.equals(characteristic.getUuid())){ if(characteristic.getValue()!=null){ final int data2 = characteristic.getIntValue(BluetoothGattCharacteristic.FORMAT_UINT32, 0); intent.putExtra("Right",String.valueOf(data2));} } sendBroadcast(intent); }

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