繁体   English   中英

以编程方式在Android中启动“添加Google帐户”活动

[英]Programmatically starting the 'Add Google Account' activity in Android

我正在开发一个需要Google帐户才能使用某些选项的应用程序。 当没有检测到帐户时,选项被禁用,但我通过弹出提示用户添加一个,如果用户单击是,则应该开始活动。 它可以正常显示全局的“添加帐户”页面,但我想跳过那个不必要的额外步骤。 毕竟,如果需要Google帐户,为什么要向某人提供添加Exchange帐户的选项,这只是令人困惑。 所以我想默认使用新的Google帐户设置页面。

Java的

try {
    Intent intent = new Intent();
    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
    intent.setClassName( "com.google.android.gsf", "com.google.android.gsf.login.AccountIntroActivity");

    //if(getApplicationContext().getPackageManager().resolveActivity(intent, PackageManager.MATCH_DEFAULT_ONLY) != null) {
        getApplicationContext().startActivity(intent);
    //} else {
        //getApplicationContext().startActivity(new Intent(Settings.ACTION_ADD_ACCOUNT).addFlags(Intent.FLAG_ACTIVITY_NEW_TASK));
    //}
} catch ( ActivityNotFoundException e) {
    e.printStackTrace();
}

当我运行它时,抛出以下异常:

05-29 18:24:50.741:W / System.err(10875):android.content.ActivityNotFoundException:无法找到显式活动类{com.google.android.gsf / com.google.android.gsf.login.AccountIntroActivity }; 你有没有在AndroidManifest.xml中声明这个活动?

AndroidManifest.xml中

    <activity 
        android:name="com.google.android.gsf.login.AccountIntroActivity"/>   

问题:我在这里缺少什么?

编辑:

我尝试了使用addAccount的不同方式,这不起作用,没有任何反应,没有错误,没有新活动开始添加Google帐户。 顺便说一下,原始版本中的整个try catch块都在AlertDialog / listener中。

AccountManager acm = AccountManager.get();
acm.addAccount("com.google", null, null, null, null, null, null);           

好吧,使用AccountManager方式的问题是我在方法调用中根本没有使用Activity上下文,或者没有正确使用。 鉴于它在DialogInterface中使用的事实,这适用:

private void popup() {
     AlertDialog.Builder helpBuilder = new AlertDialog.Builder(this);
     helpBuilder.setTitle("Add Gmail account");
     helpBuilder.setMessage("These options rely on a Gmail account, but you 
     don't seem to have one configured. Would you like to configure one now?");

     helpBuilder.setPositiveButton("Yes",
     new DialogInterface.OnClickListener() {
         //@Override
         public void onClick(DialogInterface dialog, int which) {
             //try/ catch block was here
             AccountManager acm = AccountManager.get(getApplicationContext());
             acm.addAccount("com.google", null, null, null, thisclassname.this, 
             null, null);
            }
     });

     helpBuilder.setNegativeButton("No", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
             // close the dialog, return to activity
         }
     });    

     AlertDialog helpDialog = helpBuilder.create();
     helpDialog.show();
}//end method

这可能需要更多的工作来实际使用配置的帐户名称,但是现在,这回答了Q.

可悲的是,这需要获得许可,但我想这就是事情的方式

您实际上是在尝试使用私有API - 添加Google帐户活动的类名可能会更改,或者在不同的Android版本上可能已经有所不同。 它位于其中一个Google服务包中,您当然不应将其名称添加到清单中。 总之,这是一个黑客,不要这样做。 AccountManager.addAcount("com.google",...)适合您(您需要MANAGE_ACCOUNTS权限)吗?

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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