如何在android中以编程方式添加自定义帐户?

编程入门 行业动态 更新时间:2024-10-24 12:33:55
本文介绍了如何在android中以编程方式添加自定义帐户?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在尝试为我的应用创建一个帐户,在那里我可以将我的联系人与我的帐户(如 facebook、viber、whatsapp 等)联系起来.我希望我的帐户也能在设置的帐户部分中可见.有任何想法吗?我用谷歌搜索了很多,但找不到正确的答案从哪里开始.请帮忙.我试图创建一个帐户如下.这导致我出错.

I am trying to create an account for my app, where I will be able to have my contacts against my account like facebook, viber, whatsapp etc. I want my account to be visible in the account section of the settings also. Any ideas? I have googled a lot, but couldn't find a right answer where to start. Please help. What I have tried to create an account is as below. Which leads me to an error.

Account account = new Account("Title", "com.package.nom"); String password = "password"; AccountManager accountManager = (AccountManager) MainPanel.this.getSystemService( ACCOUNT_SERVICE); accountManager.addAccountExplicitly(account, password, null);

推荐答案

您需要设置多个组件才能以编程方式创建帐户.您需要:

You need to setup multiple components to be able to create an account programmatically. You need:

  • 帐户身份验证器
  • 提供访问 AccountAuthenticator 的服务
  • 一些权限

身份验证器是一个对象,它将在帐户类型和有权管理它的权限(即 linux 用户)之间进行映射.

The authenticator is an object that will make the mapping between the account type and the autority (i.e. the linux-user) that have rights to manage it.

声明身份验证器是在 xml 中完成的:

Declaring an authenticator is done in xml :

  • 创建一个文件res/xml/authenticator.xml

具有以下内容:

<?xml version="1.0" encoding="utf-8"?> <account-authenticator xmlns:android="schemas.android/apk/res/android" android:accountType="company.demo.account.DEMOACCOUNT" android:icon="@drawable/ic_launcher" android:smallIcon="@drawable/ic_launcher" android:label="@string/my_custom_account"/>

注意 accountType :它必须在您创建帐户时在代码中重用.设置"应用将使用图标和标签来显示该类型的帐户.

Note the accountType : it must be reused in code when you create the Account. The icons and label will be used by the "Settings" app to display the accounts of that type.

实现 AccountAuthenticator

您必须扩展 AbstractAccountAuthenticator 才能做到这一点.这将被第三方应用用于访问帐户数据.

You must extends AbstractAccountAuthenticator to do that. This will be use by third party app to access Account data.

以下示例不允许任何第三方应用访问,因此每个方法的实现都是微不足道的.

The following sample don't allow any access to 3rd-party app and so the implementation of each method is trivial.

public class CustomAuthenticator extends AbstractAccountAuthenticator { public CustomAuthenticator(Context context) { super(context); } @Override public Bundle addAccount(AccountAuthenticatorResponse accountAuthenticatorResponse, String s, String s2, String[] strings, Bundle bundle) throws NetworkErrorException { return null; //To change body of implemented methods use File | Settings | File Templates. } @Override public Bundle editProperties(AccountAuthenticatorResponse accountAuthenticatorResponse, String s) { return null; //To change body of implemented methods use File | Settings | File Templates. } @Override public Bundle confirmCredentials(AccountAuthenticatorResponse accountAuthenticatorResponse, Account account, Bundle bundle) throws NetworkErrorException { return null; //To change body of implemented methods use File | Settings | File Templates. } @Override public Bundle getAuthToken(AccountAuthenticatorResponse accountAuthenticatorResponse, Account account, String s, Bundle bundle) throws NetworkErrorException { return null; //To change body of implemented methods use File | Settings | File Templates. } @Override public String getAuthTokenLabel(String s) { return null; //To change body of implemented methods use File | Settings | File Templates. } @Override public Bundle updateCredentials(AccountAuthenticatorResponse accountAuthenticatorResponse, Account account, String s, Bundle bundle) throws NetworkErrorException { return null; //To change body of implemented methods use File | Settings | File Templates. } @Override public Bundle hasFeatures(AccountAuthenticatorResponse accountAuthenticatorResponse, Account account, String[] strings) throws NetworkErrorException { return null; //To change body of implemented methods use File | Settings | File Templates. } }

暴露账户类型的服务

创建一个服务来操作该类型的帐户:

public class AuthenticatorService extends Service { @Override public IBinder onBind(Intent intent) { CustomAuthenticator authenticator = new CustomAuthenticator(this); return authenticator.getIBinder(); } }

在清单中声明服务:

<service android:name="company.demo.account.AuthenticatorService" android:exported="false"> <intent-filter> <action android:name="android.accounts.AccountAuthenticator"/> </intent-filter> <meta-data android:name="android.accounts.AccountAuthenticator" android:resource="@xml/authenticator"/> </service>

这里,过滤器和引用声明验证器的 xml 资源的元数据是关键点.

Here, the filter and the meta-data referring to the xml resource declaring the authenticator are the key points.

请务必在您的清单中声明以下权限

In your manifest be sure to declare the following permissions

<uses-permission android:name="android.permission.AUTHENTICATE_ACCOUNTS"/> <uses-permission android:name="android.permission.GET_ACCOUNTS"/> <uses-permission android:name="android.permission.MANAGE_ACCOUNTS"/>

(本文中提供的示例代码并非全部需要,但您可能会有更多关于帐户管理的代码,最后所有这些代码都会很有用)

(not all required for the sample code presented in this post, but you will probably have some more code about account management and at the end all of them will be useful)

现在一切准备就绪,您可以使用以下代码创建一个帐户.请注意 addAccountExplicitly 返回的 boolean 通知您成功或失败.

Now that everything is ready you create an account with the following code. Note the boolean returned by addAccountExplicitly informing you about the success or failure.

AccountManager accountManager = AccountManager.get(this); //this is Activity Account account = new Account("MyAccount","company.demo.account.DEMOACCOUNT"); boolean success = accountManager.addAccountExplicitly(account,"password",null); if(success){ Log.d(TAG,"Account created"); }else{ Log.d(TAG,"Account creation failed. Look at previous logs to investigate"); }

最后的提示

不要将您的应用安装在外部存储设备上

如果您的应用安装在外部存储设备上,那么当卸载 sdcard 时,Android 很有可能会删除您的帐户数据(因为该帐户的身份验证器将无法再访问).因此,为了避免这种损失(在每次重新启动时!!!),您必须安装仅在内部存储上声明身份验证器的应用程序:

If your app is installed on external storage, there are good chance that Android delete your Account data when sdcard is unmounted (since the authenticator for that account will not be accessible anymore). So to avoid this loss (on every reboot !!!) you must install the App declaring the authenticator on internal storage only :

<manifest xmlns:android="schemas.android/apk/res/android" android:installLocation="internalOnly" ...

遇到问题

仔细阅读日志,AccountManger 正在输出许多日志来帮助您调试代码.

Read the logs carefully, The AccountManger is outputing many logs to help you to debug your code.

更多推荐

如何在android中以编程方式添加自定义帐户?

本文发布于:2023-11-28 07:30:47,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1641481.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:自定义   中以   帐户   方式   如何在

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!