编程启动“添加谷歌帐户”活动的Andr​​oid [英] Programmatically starting the 'Add Google Account' activity in Android

查看:307
本文介绍了编程启动“添加谷歌帐户”活动的Andr​​oid的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开发,需要一个谷歌帐户的某些选项的应用程序。 选项​​被禁用时,检测到没有户口,但我presenting由通过一个弹出询问是否添加一个用户,如果用户点击是,该活动的启动时间。 它的做工精细,显示全球的添加帐户页面,但我想跳过不必要的额外步骤。毕竟,为什么present有人用选项如果需要一个谷歌帐户添加Exchange帐户,这只是混乱。所以我想默认为新的谷歌帐户设置页面。

的Java

 尝试{
    意向意图=新的意图();
    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(意向);
    //} 其他 {
        //getApplicationContext().startActivity(new意图(Settings.ACTION_ADD_ACCOUNT).addFlags(Intent.FLAG_ACTIVITY_NEW_TASK));
    //}
}赶上(ActivityNotFoundException E){
    e.printStackTrace();
}
 

当我运行此,下面exeception被抛出:

05-29 18:24:50.741:W / System.err的(10875):android.content.ActivityNotFoundException:无法找到明确的活动类{com.google.android.gsf / com.google.android.gsf。 login.AccountIntroActivity};有你在你的Andr​​oidManifest.xml宣布这项活动?

AndroidManifest.xml中

 <活动
        机器人:名称=com.google.android.gsf.login.AccountIntroActivity/>
 

问:什么我在这里丢失

编辑:

我试着用addAccount,这也不行,什么都不会发生,没有错误抛出一种不同的方式,没有新的活动开始增加的谷歌帐户。顺便说一句,在原始版本中的整个try catch块是在AlertDialog /监听器。

 的AccountManager ACM = AccountManager.get();
acm.addAccount(com.google,NULL,NULL,NULL,NULL,NULL,NULL);
 

解决方案

好了,使用的AccountManager方式的问题是,在活动方面没有在方法调用正确使用我的人,还是不行。由于它是在一个DialogInterface使用的事实,这个作品:

 私人无效弹出(){
     AlertDialog.Builder helpBuilder =新AlertDialog.Builder(本);
     helpBuilder.setTitle(添加Gmail帐户);
     helpBuilder.setMessage(这些选项依赖于一个Gmail帐户,但你
     似乎不配置之一。你想现在配置一)?;

     helpBuilder.setPositiveButton(是,
     新DialogInterface.OnClickListener(){
         // @覆盖
         公共无效的onClick(DialogInterface对话,诠释它){
             // try / catch块在这里
             的AccountManager ACM = AccountManager.get(getApplicationContext());
             acm.addAccount(com.google,NULL,NULL,NULL,thisclassname.this,
             NULL,NULL);
            }
     });

     helpBuilder.setNegativeButton(否,新DialogInterface.OnClickListener(){
        @覆盖
        公共无效的onClick(DialogInterface对话,诠释它){
             //关闭对话框,返回活动
         }
     });

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

这可能需要一些更多的工作能够真正使用配置的帐户名,但现在,这个回答的Q值。

可悲的是,这需要一个许可,但我想这只是事情如何

I am developing an application which needs a Google account for certain options. Options are disabled when no account is detected, but I am presenting the user to add one by asking via a popup, if user clicks yes, the activity should start. It's working fine to display the global "Add account" page, but I want to skip that uncalled for extra step. After all, why present someone with the option to add a Exchange account if a Google account is needed, that's just confusing. So I want to default to the new Google account setup page.

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();
}

When I run this, the following exeception is thrown:

05-29 18:24:50.741: W/System.err(10875): android.content.ActivityNotFoundException: Unable to find explicit activity class {com.google.android.gsf/com.google.android.gsf.login.AccountIntroActivity}; have you declared this activity in your AndroidManifest.xml?

Androidmanifest.xml

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

QUESTION: what am I missing here?

EDIT:

I tried a different way using addAccount, this doesn't work, nothing happens, no errors are thrown, no new activity starts to add the Google account. By the way, the entire try catch block in the original version is in a AlertDialog/ listener.

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

解决方案

Ok, the problem using the AccountManager way was that the Activity context not being used by me in the method call at all, or not correctly. Given the fact it was used in a DialogInterface, this works:

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

This probably needs some more work to be able to actually use the configured account name, but for now, this answers the Q.

Sadly, this requires a permission, but I guess that's just how things are

这篇关于编程启动“添加谷歌帐户”活动的Andr​​oid的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆