我正在开发一个需要Google帐户才能使用某些选项的应用程序。 当没有检测到帐户时,选项被禁用,但我通过弹出提示用户添加一个,如果用户单击是,则应该开始活动。 它可以正常显示全局的“添加帐户”页面,但我想跳过那个不必要的额外步骤。毕竟,如果需要Google帐户,为什么要向某人提供添加Exchange帐户的选项,这只是令人困惑。所以我想默认使用新的Google帐户设置页。
爪哇
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, null, null, null, null);
答案 0 :(得分:6)
好吧,使用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
这可能需要更多的工作才能真正使用配置的帐户名称,但是现在,它可以回答问题。
可悲的是,这需要获得许可,但我想这就是事情的原因
答案 1 :(得分:4)
您实际上是在尝试使用私有API - 添加Google帐户活动的类名可能会更改,或者在不同的Android版本上可能已经有所不同。它位于其中一个Google服务包中,您当然不应将其名称添加到清单中。总之,这是一个黑客,不要这样做。 AccountManager.addAcount("com.google",...)
不适合您(您需要MANAGE_ACCOUNTS
权限)?