2013-12-17 52 views
-2

我有這樣的代碼:如何直接從我的應用程序打開Gmail?

Intent dialogIntent = new Intent(android.content.Intent.ACTION_SEND); 
dialogIntent.setType("plain/text"); 
dialogIntent.putExtra(android.content.Intent.EXTRA_EMAIL, ""); 
dialogIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
getApplication().startActivity(dialogIntent); 

這將打開硬盤,Gmail和SKYPE的對話。我需要直接打開Gmail,而不出現對話框。

已經嘗試過,它不起作用。我從服務中這樣做。

回答

1

添加到您的意圖:

dialogIntent.setClassName("com.google.android.gm", "com.google.android.gm.ConversationListActivity"); 

注:這不是正式支持,並在未來的版本可能會斷裂。沒有文檔或官方的啓動Gmail活動的方式。

編輯:發現了另一個方法,嘗試,看看它的工作原理:

final PackageManager pm = getPackageManager(); 
final List<ResolveInfo> matches = pm.queryIntentActivities(intent, 0); 
ResolveInfo best = null; 
for (final ResolveInfo info : matches) 
    if (info.activityInfo.packageName.endsWith(".gm") || 
     info.activityInfo.name.toLowerCase().contains("gmail")) best = info; 
if (best != null) 
    intent.setClassName(best.activityInfo.packageName, best.activityInfo.name); 
+0

已經嘗試過,而且它不工作我,我從一個服務做這個 – Afermin

+0

它可能不會工作,以及Android的不同版本會進一步渾濁。並不是說這不是一個好的答案,但是@whatissleep提到它並沒有得到官方的支持。你可能根本無法做到這一點。 – RossC

相關問題