gpt4 book ai didi

android - 以编程方式启动 Android 中的 'Add Google Account' Activity

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:41:26 26 4
gpt4 key购买 nike

我正在开发一个需要 Google 帐户才能使用某些选项的应用程序。当没有检测到帐户时,选项被禁用,但我通过弹出窗口询问用户添加一个,如果用户单击是, Activity 应该开始。显示全局“添加帐户”页面工作正常,但我想跳过不需要的额外步骤。毕竟,如果需要 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: 无法找到明确的 Activity 类 {com.google.android.gsf/com.google.android.gsf。登录.AccountIntroActivity};您是否已在 AndroidManifest.xml 中声明此 Activity ?

Androidmanifest.xml

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

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

编辑:

我尝试了使用 addAccount 的另一种方式,这不起作用,没有任何反应,没有抛出任何错误,没有新的 Activity 开始添加 Google 帐户。顺便说一句,原始版本中的整个 try catch block 都在 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

这可能需要做更多的工作才能实际使用配置的帐户名称,但现在,这回答了问题。

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

关于android - 以编程方式启动 Android 中的 'Add Google Account' Activity ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10803132/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com