如何启动 Android 应用程序与 GMail 帐户对话?

发布于 2024-11-16 16:32:23 字数 2838 浏览 1 评论 0原文

我正在开发一个已经存在的应用程序,但我开发只是为了学习。我正在开发 Android 版 GMail 客户端应用程序。我尝试使用 Content Observer,但无法找到 GMail Inbox 的内容提供商 URI。我正在尝试其他方式,将 ua 设置为客户端。我的客户端已设置,但我不知道如何实例化该客户端以访问 GMail 服务器。

以下是我的代码:

GMailInbox.java

package com.tyco.gmailApp;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;

public class GmailInbox extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        //GmailObserver go = new GmailObserver();
        //this.getApplicationContext().getContentResolver().registerContentObserver(Uri.parse("content://imap.gmail.com"), true, go);

        Intent myService = new Intent(this,GMailService.class);
        myService.putExtra("user", "[email protected]");
        myService.putExtra("password", "brooklyn");
        startService(myService);
    }
}

GMailService.java

package com.tyco.gmailApp;


import java.util.Properties;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.view.inputmethod.InputMethodSession;
import android.view.inputmethod.InputMethod.SessionCallback;
public class GMailService extends Service
{

    private String mailhost = "imaps.gmail.com"; 
    private String user; 
    private String password; 

    @Override
    public IBinder onBind(Intent intent){
        return null;
    }

    @Override
    public void onCreate(){
        super.onCreate();


    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }

    @Override
    public void onStart(Intent intent, int startId) {
        super.onStart(intent, startId);
        this.user = intent.getStringExtra("user"); 
        this.password = intent.getStringExtra("password");; 
        Properties props = new Properties();
        props = System.getProperties();
        props.setProperty("mail.store.protocol", "imaps");
        props.setProperty("mail.imaps.host", mailhost); 
        props.put("mail.imaps.auth", "true"); 
        props.put("mail.imaps.port", "993"); 
        props.put("mail.imaps.socketFactory.port", "993"); 
        props.put("mail.imaps.socketFactory.class", 
                  "javax.net.ssl.SSLSocketFactory"); 
        props.put("mail.imaps.socketFactory.fallback", "false");
        props.put("mail.imaps.socketFactory.fallback", "false");
        props.setProperty("mail.imaps.quitwait", "false"); 


// I Don't Know what to do here



    }




}

我不知道将所有信息收集到属性对象后要做什么。请建议一些东西。

问候, 拉胡尔·杰斯瓦尔

I am developing an app which is already existing but I am developing just to learn. I am developing a GMail client app for Android. I tried with Content Observer but was unable to find the Content Provider URI for GMail Inbox. I am trying other way by setting ua a client. My client is set up, but i don't know how to instantiate this client to hit the GMail server.

Following are my code:

GMailInbox.java

package com.tyco.gmailApp;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;

public class GmailInbox extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        //GmailObserver go = new GmailObserver();
        //this.getApplicationContext().getContentResolver().registerContentObserver(Uri.parse("content://imap.gmail.com"), true, go);

        Intent myService = new Intent(this,GMailService.class);
        myService.putExtra("user", "[email protected]");
        myService.putExtra("password", "brooklyn");
        startService(myService);
    }
}

GMailService.java

package com.tyco.gmailApp;


import java.util.Properties;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.view.inputmethod.InputMethodSession;
import android.view.inputmethod.InputMethod.SessionCallback;
public class GMailService extends Service
{

    private String mailhost = "imaps.gmail.com"; 
    private String user; 
    private String password; 

    @Override
    public IBinder onBind(Intent intent){
        return null;
    }

    @Override
    public void onCreate(){
        super.onCreate();


    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }

    @Override
    public void onStart(Intent intent, int startId) {
        super.onStart(intent, startId);
        this.user = intent.getStringExtra("user"); 
        this.password = intent.getStringExtra("password");; 
        Properties props = new Properties();
        props = System.getProperties();
        props.setProperty("mail.store.protocol", "imaps");
        props.setProperty("mail.imaps.host", mailhost); 
        props.put("mail.imaps.auth", "true"); 
        props.put("mail.imaps.port", "993"); 
        props.put("mail.imaps.socketFactory.port", "993"); 
        props.put("mail.imaps.socketFactory.class", 
                  "javax.net.ssl.SSLSocketFactory"); 
        props.put("mail.imaps.socketFactory.fallback", "false");
        props.put("mail.imaps.socketFactory.fallback", "false");
        props.setProperty("mail.imaps.quitwait", "false"); 


// I Don't Know what to do here



    }




}

I don't know what to do after collecting all info into properties object. Please suggest something.

Regards,
Rahul Jaiswal

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

枫以 2024-11-23 16:32:23

添加此代码以连接邮件服务器

Session session = Session.getDefaultInstance(props, null);
    try{
        Store store = session.getStore("imaps");
        store.connect(mailhost, mailid,password);    
        Folder folder = store.getFolder("INBOX");
        folder.open(Folder.READ_ONLY);
        int count=folder.getUnreadMessageCount();
        Message  message[] = folder.getMessages();
        for (int i=0, n=message.length; i<n; i++) {
                System.out.println("subject " +message[i].getSubject());
        }
      }

Add this code to connect with mail server

Session session = Session.getDefaultInstance(props, null);
    try{
        Store store = session.getStore("imaps");
        store.connect(mailhost, mailid,password);    
        Folder folder = store.getFolder("INBOX");
        folder.open(Folder.READ_ONLY);
        int count=folder.getUnreadMessageCount();
        Message  message[] = folder.getMessages();
        for (int i=0, n=message.length; i<n; i++) {
                System.out.println("subject " +message[i].getSubject());
        }
      }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文