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

我正在开发一个需要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);

解决方法

好吧,使用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",thisclassname.this,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.

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

相关文章

Android性能优化——之控件的优化 前面讲了图像的优化,接下...
前言 上一篇已经讲了如何实现textView中粗字体效果,里面主要...
最近项目重构,涉及到了数据库和文件下载,发现GreenDao这个...
WebView加载页面的两种方式 一、加载网络页面 加载网络页面,...
给APP全局设置字体主要分为两个方面来介绍 一、给原生界面设...
前言 最近UI大牛出了一版新的效果图,按照IOS的效果做的,页...