Posted on Wed ,30/12/2009 by Ryan Alford
I was looking for a way to open the Contacts Activity, chose a Contact, then have that Contact returned to my application. I found a number of code snippets, but they all used depreciated API calls. So I decided to make this post with the updated API calls for Android 2.0.
First, you will need to add this permission to your AndroidManifest.xml file:
<uses-permission android:name="android.permission.READ_CONTACTS"/>
Next, add this constant as a class level variable:
private static final int PICK_CONTACT = 3;
Now, you will add the code to open the Contacts Activity. This is done by using an Intent:
// I did this from a button click
public void btnAddContacts_Click(View view){
Intent intent = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI);
startActivityForResult(intent, PICK_CONTACT);
}
Now, you will override the “onActivityResult” activity method, and get the Contact information:
@Override
public void onActivityResult(int reqCode, int resultCode, Intent data){
super.onActivityResult(reqCode, resultCode, data);
switch(reqCode){
case (PICK_CONTACT):
if (resultCode == Activity.RESULT_OK){
Uri contactData = data.getData();
Cursor c = managedQuery(contactData, null, null, null, null);
if (c.moveToFirst()){
// other data is available for the Contact. I have decided
// to only get the name of the Contact.
String name = c.getString(c.getColumnIndexOrThrow(ContactsContract.Contacts.DISPLAY_NAME));
Toast.makeText(getApplicationContext(), name, Toast.LENGTH_SHORT).show();
}
}
}
}
And that is all you need to do.
Categorized under :Android
Posted on Tue ,22/12/2009 by Ryan Alford
Recently, I needed to add a color picker to a web application. I thought I would try the new AJAX toolkit, but the toolkit was full of issues so I had to resort to doing it with javascript. I downloaded a color picker that somebody had done(and donated for their development).
When I went to add the external javascript file and the other associated files, my AJAX page would no longer show up. Anything inside of an UpdatePanel would not show up. I did some searching and found that using an external javascript file can cause AJAX to fail.
I originally had this code which was giving me issues.
<asp:Content ID="Content1" ContentPlaceHolderID="MainContent" runat="server">
<asp:ScriptManagerProxy ID="ScriptManagerProxy1" runat="server" />
<script src="../Javascript/jscolor/jscolor.js" type="text/javascript" />
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<%-- some controls --%>
</asp:UpdatePanel>
</asp:Content>
I used the ScriptReference in the ScriptManagerProxy to add a reference to the javascript file:
<asp:Content ID="Content1" ContentPlaceHolderID="MainContent" runat="server">
<asp:ScriptManagerProxy ID="ScriptManagerProxy1" runat="server">
<Scripts>
<asp:ScriptReference Path="~/Javascript/jscolor/jscolor.js" />
</Scripts>
</asp:ScriptManagerProxy>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<%-- some controls --%>
</asp:UpdatePanel>
</asp:Content>
And that worked. The controls inside the UpdatePanel now show up.
Tags : AJAX, ASP.Net, external, javacript
Categorized under :.Net
Posted on Thu ,17/12/2009 by Ryan Alford
I made a post almost 3 months ago about a .Net Twitter API that I had done. Since then, I have made some modifications to the API. I have added a couple of more features. I also included various classes from the System.Web namespace so that I wouldn’t need a reference to that namespace. This allows the API to be used on a mobile device with a very small footprint(the System.Web.dll file is 5MB in size, my .dll is 52KB).
I am missing a few features from my API. Most notably the new “Retweet” functionality and the “Lists” functionality. I am also missing some of the account methods. Other than that, I believe I have most, if not all, of the other methods.
This API supports both Basic Auth and OAuth. I would advise to use OAuth, since Twitter will be depreciating Basic Auth in June 2010. To learn more about how OAuth works, check out my other blog post where I try to explain OAuth.
Remember that this API is for the Desktop OAuth only. Feel free to download it and make changes to allow OAuth from a web application(yes, they are different “workflows”). Basic Auth should work for both Desktop and Web applications.
I give credit to Shannon Whitley for his original OAuth code. I made some modifications to it, but the base code of the OAuth is his.
Here is a code snippet on using the API for OAuth authorization.
// creats instance and sets Consumer and ConsumerSecret values
TwitEclipseAPI twit = new TwitEclipseAPI();
twit.OAuthConsumerKey = "yourConsumerKey";
twit.OAuthConsumerSecret = "yourConsumerSecret";
// makes request to get unauthorized request token
// The method will concatenate the request token to Twitter's Desktop OAuth
// url (http://twitter.com/oauth/authorize)
string url = twit.OAuthGetUnauthorizedRequestToken();
// opens the user's default browser and browses to Twitter's OAuth page
Process.Start(url);
// You will need to get the PIN from the user
// I just created a popup and have the user enter the PIN
// into a textbox in the popup
frmPinPopup popup = new frmPinPopup();
popup.ShowDialog();
string PIN = popup.PIN;
popup.Dispose();
// Gets the Token and TokenSecret that will be needed for all
// subsequent requests. You will need to save these values to keep
// from forcing the user to authorize your application everytime they
// open your application.
twit.OAuthRequestAccessToken(PIN);
// You can check for success by checking the OAuthAccessToken
// and OAuthAccessTokenSecret values. If they are populated, then
// it was successful. If they are empty, then it failed.
if (!string.IsNullOrEmpty(twit.OAuthAccessToken) && !string.IsNullOrEmpty(twit.OAuthAccessTokenSecret))
{
MessageBox.Show("Authorization Successful");
}
Each method of the API will check the OAuthAccessToken and OAuthAccessTokenSecret values to determine whether it needs to do the request using OAuth or Basic Auth.
I have put the code on CodePlex. It will contain both the .dll file, and the source code. I felt it was easier to keep up with using CodePlex.
If you have code questions, you can post here. However, if you are on Google Wave, you can go here. If you would like Google Wave, I have about 20 invitations that I can send out. Using Google Wave would be much better since we could exchange code using a code snippet tool.
Tags : .Net, C#, Desktop, OAuth, Twitter
Categorized under :.Net