2013-05-04 91 views
0

我想從我的android應用程序的Gmail帳戶功能添加導入聯繫人。所以第一個問題是從gmail獲取訪問令牌。我發現有GoogleAuthUtil類可以幫助我。
這裏是我的代碼:使用GoogleAuthUtil導入Gmail聯繫人

private void importContactsFromGmail() { 
    showProgressDialog(); 
    GetTokenTask getTokenTask = new GetTokenTask(); 
    getTokenTask.execute(); 
    String token = ""; 
    try { 
     token = getTokenTask.get(); 
    } catch (Exception e) { 
     e.printStackTrace(); 
    } 
    System.out.println(token); 
    hideProgressDialog(); 
} 

private class GetTokenTask extends AsyncTask<Void, Void, String> { 

    @Override 
    protected String doInBackground(Void... params) { 
     String token = ""; 
     try { 
      token = GoogleAuthUtil.getToken(activity, <My_gmail_account>, "https://www.google.com/m8/feeds/"); 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
     return token; 
    } 
} 

現在呼籲GoogleAuthUtil.getToken後,我的應用程序完全凍結(在logcat中沒有錯誤)。我完全卡住了,我需要你的幫助。
我的代碼有什麼問題?也許我應該以其他方式導入聯繫人?

+0

什麼設備,你運行的? – 2013-05-09 05:56:03

+0

Bersh,你可以請給我的源獲取android中的Gmail聯繫人?我需要這個。 – Noundla 2013-06-10 07:18:47

回答

1

不確定這是否相關,但在主線程上調用.get()方法不正確,因爲是阻塞方法。

如果以這種方式使用AsyncTask會怎麼樣?

public class MainActivity extends Activity { 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     new GetTokenTask().execute(); 
    } 

    static class GetTokenTask extends AsyncTask<Void, Void, String> { 

     @Override 
     protected String doInBackground(Void... unused) { 
      String token = ""; 
      try { 
       token = GoogleAuthUtil.getToken(activity, <My_gmail_account>, "https://www.google.com/m8/feeds/"); 
      } catch (Exception e) { 
       e.printStackTrace(); 
      } 

      return token; 
     } 

     @Override 
     protected void onPostExecute(String token) { 
      Toast.makeText(MainActivity.this, token, Toast.LENGTH_SHORT).show(); 
     } 
    } 

} 

(我寫無需編譯它,也許它需要進行調整)

0

在Android設備上,Gmail聯繫人在本地同步到設備上,並可通過公開的Contacts Provider獲得,因此您無需使用Google API來提取已有的內容。整個training series專門用於檢索聯繫人列表。

請注意,聯繫人培訓系列確實假定您已經瞭解了內容提供商,因此也可以閱讀the basics of Content Providers

+0

是的,但用戶可以因某種原因禁用同步。在這種情況下,我想讓用戶有可能從Gmail導入聯繫人,避免與手機同步 – Bersh 2013-05-04 14:34:39

相關問題