sli*_*n77 6 android android-activity android-account
我正在开发一个需要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();
}
Run Code Online (Sandbox Code Playgroud)
当我运行它时,抛出以下异常:
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"/>
Run Code Online (Sandbox Code Playgroud)
问题:我在这里缺少什么?
编辑:
我尝试了使用addAccount的不同方式,这不起作用,没有任何反应,没有错误,没有新活动开始添加Google帐户.顺便说一下,原始版本中的整个try catch块都在AlertDialog/listener中.
AccountManager acm = AccountManager.get();
acm.addAccount("com.google", null, null, null, null, null, null);
Run Code Online (Sandbox Code Playgroud)
好吧,使用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
Run Code Online (Sandbox Code Playgroud)
这可能需要更多的工作来实际使用配置的帐户名称,但是现在,这回答了Q.
可悲的是,这需要获得许可,但我想这就是事情的方式
归档时间: |
|
查看次数: |
7692 次 |
最近记录: |