Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/227.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
以编程方式启动';添加谷歌账户';Android中的活动_Android_Android Activity_Android Account - Fatal编程技术网

以编程方式启动';添加谷歌账户';Android中的活动

以编程方式启动';添加谷歌账户';Android中的活动,android,android-activity,android-account,Android,Android Activity,Android Account,我正在开发一个应用程序,它需要一个特定选项的谷歌帐户。 当未检测到任何帐户时,选项将被禁用,但我通过弹出窗口询问用户添加一个帐户,如果用户单击“是”,活动应开始。 显示全局“添加帐户”页面工作正常,但我想跳过这一不必要的额外步骤。毕竟,如果需要一个谷歌账户,为什么要让某人选择添加一个交换账户呢?这只是让人困惑。因此,我想默认为新的谷歌帐户设置页面 Java try { Intent intent = new Intent(); intent.addFlags(Intent.FLA

我正在开发一个应用程序,它需要一个特定选项的谷歌帐户。 当未检测到任何帐户时,选项将被禁用,但我通过弹出窗口询问用户添加一个帐户,如果用户单击“是”,活动应开始。 显示全局“添加帐户”页面工作正常,但我想跳过这一不必要的额外步骤。毕竟,如果需要一个谷歌账户,为什么要让某人选择添加一个交换账户呢?这只是让人困惑。因此,我想默认为新的谷歌帐户设置页面

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"/>   

您实际上是在尝试使用一个私有API——添加Google帐户活动的类名可能会改变,或者在不同的Android版本上已经不同了。它位于一个Google服务包中,您当然不应该将其名称添加到您的清单中。简言之,这是一个黑客,不要这样做。
AccountManager.addAcount(“com.google”,…)
是否为您工作(您需要
MANAGE\u ACCOUNTS
权限)?

好的,使用AccountManager方式的问题是我在方法调用中根本没有使用活动上下文,或者没有正确使用。鉴于在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
这可能需要更多的工作才能实际使用配置的帐户名,但现在,这回答了问题


遗憾的是,这需要许可,但我想事情就是这样的

我知道你是对的,但我还是喜欢我的方式。我尝试了addAccount方法,但坦率地说,它对我不起作用。除此之外,这需要额外的许可,在我看来这是毫无意义的。我可以明确地理解addAccount方法的这种权限,因为这会以静默方式添加一个帐户,但addAccount所做的只是调用一个新的活动,该活动处理自己的工作,应用程序本身实际上不管理/修改/创建任何内容。但假设我改变主意了,你能举一个应该有效的例子吗?我用com.google type和rest null尝试了一下,我看不出你的意思,但你实际上是在添加一个帐户,尽管有用户交互,所以这有点道理。我知道它可能会吓坏一些用户。我得回去查一下,但是IIRC,只要你有权限,“com.google”和其他
null
,就可以了。你得到了什么错误。你是对的,Android开发者网站明确指出启动新活动需要权限。我通过将intent.setClassName(com.google.android.gsf,…)更改为(com.google.android.gsf.login)来规避我的问题。我的线索是在控制台中,当我使用额外的步骤“全局添加帐户”页面时,我试图找出发生了什么。现在我遇到了一个新问题,它要求我输入密码,并立即显示无法与谷歌服务器通信的错误。当我使用addAccount方法时,我没有收到任何错误…但它仍然不起作用…thx!PS:我希望你不介意我把这个问题标记为已回答,因为它还没有完全回答。我正朝着正确的方向前进,你真的帮了我的忙,但我仍然无法通过全局添加帐户页面,这就是这个问题试图解决的:)基本上我自己回答了,但还不是很清楚……到底是什么问题,它是如何“不起作用”的?至于这个问题,如果你找到了答案,你可以自己添加一个答案,然后接受它。这将使这个问题从没有答案的名单上消失。我感觉自己像个d***,但我想我必须把我自己的答案标记为现在的答案。谢谢你的耐心和帮助@Nikolay Elenkov!“thisclassname”是实际的类吗?
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