简体   繁体   中英

java.awt.Desktop class

I am using a Google API for android. Since Google API/G Suite Quickstart for android refers to their java examples, I am trying to implement this:

GoogleAuthorizationCodeFlow flow =
            new GoogleAuthorizationCodeFlow.Builder(
                    ReadMail.HTTP_TRANSPORT, ReadMail.JSON_FACTORY, clientSecrets, ReadMail.SCOPES)
                    .setDataStoreFactory(ReadMail.DATA_STORE_FACTORY)
                    .setAccessType("offline")
                    .build();


    AuthorizationCodeInstalledApp authCodeInstalledApp = new AuthorizationCodeInstalledApp(flow, new LocalServerReceiver());
    Credential credential = authCodeInstalledApp.authorize("user");

The problem, I am encountering now is that Android just support a subset of JDK classes. Therefore java.awt.Desktop isn't supported. But I really need this class, since AuthorizationCodeInstalledApp 's authorize() will soon or later call its intern function browse() . This function needs the Desktop class.

Is there a way to get that class for android? Or is there another workaround to authenticate with Google?

I solved it by myself now. Instead of trying to get the Desktop class from java.awt.Desktop, I just overwrote the on Authorization method:

AuthorizationCodeInstalledApp ab = new AuthorizationCodeInstalledApp(flow, new LocalServerReceiver()){
        protected void onAuthorization(AuthorizationCodeRequestUrl authorizationUrl) throws IOException {
                String url = (authorizationUrl.build());
                Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
                main_Activity.startActivity(browserIntent);
            }
        };

The reason I did this is because authorize() will call onAuthorization() which will call browse(), which checks if Desktop is supported or not. So by rewriting the onAuthorization() method, I won't need that class anymore. My rewritten class will just start a new Browserwindow with that authorization-URL in your android device.

I hope, I was able to help anyone, who encounteres this problem.

Dnsiv 答案正常工作,但我也必须将 FLAG_ACTIVITY_NEW_TASK 添加到我的 Intent 中。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM