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

前端之家收集整理的这篇文章主要介绍了以编程方式在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.

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

原文链接:https://www.f2er.com/android/308799.html

猜你在找的Android相关文章