<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>accountmanager &#8211; Mahdi Tajik</title>
	<atom:link href="http://www.mahditajik.ir/tag/accountmanager/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.mahditajik.ir</link>
	<description>I&#039;m Mahdi Tajik. I am project manager and Full-stack Android developer. welcome to my personal website.</description>
	<lastBuildDate>Sat, 01 Jul 2017 09:46:30 +0000</lastBuildDate>
	<language>fa-IR</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.2.6</generator>

<image>
	<url>http://www.mahditajik.ir/wp-content/uploads/2015/03/cropped-sample-logo-MT-2-32x32.png</url>
	<title>accountmanager &#8211; Mahdi Tajik</title>
	<link>http://www.mahditajik.ir</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>کار با Account-manager اندروید</title>
		<link>http://www.mahditajik.ir/account-manager/</link>
					<comments>http://www.mahditajik.ir/account-manager/#comments</comments>
		
		<dc:creator><![CDATA[mahdi]]></dc:creator>
		<pubDate>Mon, 23 May 2016 07:56:52 +0000</pubDate>
				<category><![CDATA[آموزش]]></category>
		<category><![CDATA[accountmanager]]></category>
		<category><![CDATA[learning]]></category>
		<category><![CDATA[اندروید]]></category>
		<category><![CDATA[حساب کاربری]]></category>
		<guid isPermaLink="false">http://www.mahditajik.ir/?p=1821</guid>

					<description><![CDATA[<p>مطلبی که این جلسه قصد آموزش آن را دارم کار با سیستم مدیریت حساب داخلی اندروید می باشد. می خواهیم در یک پروژه عملی یک حساب کاربری دلخواه بسازیم و بعد از ثبت نام اولیه , تایید از سرور، برنامه با استفاده از حساب کاربری داخلی ایجاد شده بطور خودکار لاگین کند. با توجه به اینکه ...</p>
<p>نوشته <a rel="nofollow" href="http://www.mahditajik.ir/account-manager/">کار با Account-manager اندروید</a> اولین بار در <a rel="nofollow" href="http://www.mahditajik.ir">Mahdi Tajik</a> پدیدار شد.</p>
]]></description>
										<content:encoded><![CDATA[<p style="text-align: justify;"><img decoding="async" loading="lazy" class="aligncenter size-full wp-image-1845" src="http://www.mahditajik.ir/wp-content/uploads/2016/05/learning-tempelate.jpg" alt="learning-tempelate" width="800" height="486" srcset="http://www.mahditajik.ir/wp-content/uploads/2016/05/learning-tempelate.jpg 800w, http://www.mahditajik.ir/wp-content/uploads/2016/05/learning-tempelate-300x182.jpg 300w, http://www.mahditajik.ir/wp-content/uploads/2016/05/learning-tempelate-768x467.jpg 768w" sizes="(max-width: 800px) 100vw, 800px" /></p>
<p style="text-align: justify;">مطلبی که این جلسه قصد آموزش آن را دارم کار با سیستم مدیریت حساب داخلی اندروید می باشد. می خواهیم در یک پروژه عملی یک حساب کاربری دلخواه بسازیم و بعد از ثبت نام اولیه , تایید از سرور، برنامه با استفاده از حساب کاربری داخلی ایجاد شده بطور خودکار لاگین کند. با توجه به اینکه منابع زیادی در این رابطه حتی به انگلیسی موجود نمی باشد، امیدوارم این مطلب برایتان مفید واقع شود. در واقع مشکل از جایی شروع شد که خودم به دنبال پیداکردن جوابی برای آن بودم نهایتاً تجربه شخصی خودم را برایتان به اشتراک می گذارم. البته در آموزش امروز با اصول اولیه حساب کاربری آشنا خواهیم شد و اصل مطلب برای آموزش بعدیست!</p>
<blockquote><p>This class provides access to a centralized registry of the user&#8217;s online accounts. The user enters credentials (username and password) once per account, granting applications access to online resources with &#8220;one-click&#8221; approval.</p></blockquote>
<p>طبق تعریف رسمی اندروید: AccountManager کلاسی است که ثبت نام های آنلاین کاربر را متمرکز کرده و با توجه به اطلاعاتی که از کاربر نگهداری می کند( از قبیل نام کاربری و کلمه عبور)، ارتباط با آن (کاربر ثبت شده) و استفاده از منابع آن را توسط برنامه ها به آسانی فراهم می کند.</p>
<p>برای کار با AccountManager اندروید نیاز به سه آیتم اصلی داریم که هرکدام را ملاحظه خواهید کرد.</p>
<ul>
<li>سرویس ویژه مدیریت حساب</li>
<li>فایل xml حاوی اطلاعات مربوط به حساب کاربری</li>
<li>یک نمونه از کلاس AbstractَAccountAuthenticator</li>
</ul>
<pre class="crayon-plain-tag">public class MyAthenticator extends AbstractAccountAuthenticator {
    
    public MyAthenticator(Context context) {
        super(context);
    }

    @Override
    public Bundle editProperties(AccountAuthenticatorResponse response, String accountType) {
        return null;
    }

    @Override
    public Bundle addAccount(AccountAuthenticatorResponse response, String accountType, String authTokenType, String[] requiredFeatures, Bundle options) throws NetworkErrorException {

        Log.i("MAHDI","on add account");
        return null;
    }

    @Override
    public Bundle confirmCredentials(AccountAuthenticatorResponse response, Account account, Bundle options) throws NetworkErrorException {
        return null;
    }

    @Override
    public Bundle getAuthToken(AccountAuthenticatorResponse response, Account account, String authTokenType, Bundle options) throws NetworkErrorException {
        return null;
    }

    @Override
    public String getAuthTokenLabel(String authTokenType) {
        return null;
    }

    @Override
    public Bundle updateCredentials(AccountAuthenticatorResponse response, Account account, String authTokenType, Bundle options) throws NetworkErrorException {
        return null;
    }

    @Override
    public Bundle hasFeatures(AccountAuthenticatorResponse response, Account account, String[] features) throws NetworkErrorException {
        return null;
    }
}</pre>
<p style="text-align: justify;">در این کلاس مشتق شده متدها بطور خودکار تولید می شوند و توضیح اینکه کار اصلی آن چه است برای آموزش بعدی و فقط این را بدانید که اجباری است. من  فقط در متد addAccount یک لاگ گذاشتم تا ببینید چه موقع فراخوانی می شود و بدانید اتفاقات این کلاس ارتباط مستقیمی به حساب کاربری دارد. مثلا هرجایی از Account-Manager استفاده کنیم و توسط آن اکانتی را اضافه کنید، متد addAccount در این کلاس فراخوانی می شود. ارتباط این کلاس با Account-manager توسط سرویسی است در پایین می بینید.<br />
سرویس را بصورت زیر بسازید:</p>
<pre class="crayon-plain-tag">public class myService extends Service {
    @Nullable
    @Override
    public IBinder onBind(Intent intent) {

        MyAthenticator authenticator = new MyAthenticator(getBaseContext());
        return authenticator.getIBinder();
    }
}</pre>
<p style="text-align: justify;">حال فایل xml در پوشه xml پروژه بسازید. من اسم این را account_data.xml می گذارم. همانطور که مشاهده می کنید نام برنامه، نوع دلخواه حساب کاربری و آیکون حساب کاربری تعریف می شود. (این آیکون همان آیکونی هست که وقتی از تنظیمات وارد حساب کاربری گوشی می شوید، انواع حساب های کاربری موجود  را با آیکن آنها را می بینید!)</p>
<pre class="crayon-plain-tag">&lt;?xml version="1.0" encoding="utf-8"?&gt;
&lt;account-authenticator xmlns:android="http://schemas.android.com/apk/res/android"
    android:accountType="ir.mahditajik.testAppAccountType"
    android:icon="@mipmap/ic_launcher"
    android:smallIcon="@mipmap/ic_launcher"
    android:label="@string/app_name"/&gt;</pre>
<p style="text-align: justify;">سپس لازم است تا سرویس تولید شده را در فایل Manifest  تعریف کرده و فایل account_data.xml را ضمیمه کنیم:</p>
<pre class="crayon-plain-tag">&lt;service android:name=".MyService" android:exported="false"&gt;
    &lt;intent-filter&gt;
        &lt;action android:name="android.accounts.AccountAuthenticator"/&gt;
    &lt;/intent-filter&gt;
    &lt;meta-data
        android:name="android.accounts.AccountAuthenticator"
        android:resource="@xml/account_data"/&gt;
&lt;/service&gt;</pre>
<p style="text-align: justify;">حال تنها کاری که باید انجام دهیم استفاده از دستور accountmanageraddAccountExplicitly در هر جای دلخواه است.</p>
<pre class="crayon-plain-tag">public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        if (fab != null)
            fab.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    AccountManager accountManager = AccountManager.get(MainActivity.this); //this is Activity
                    Account account = new Account("MyAccount", "com.mahdi");

                    boolean success = accountManager.addAccountExplicitly(account, "password", null);
                    accountManager.setPassword(account, "mahditajik");
                    if (success) {
                        Log.d("mahdi", "Account created");

                    } else {
                        Log.d("mahdi", "Account creation failed. Look at previous logs to investigate");
                    }
                }
            });

    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}</pre>
<p style="text-align: justify;">خیلی خوب! شما موفق شدید با اجرای برنامه و لمس fab button حساب کاربری جدیدی از نوع &#8220;com.mahdi&#8221; ایجاد کنید که در آن نام کاربری و کلمه عبور قرار داده شده است. می توانید به تنظیمات گوشی رفته و حساب کاربری تولید شده را مشاهده کنید. امیدوارم آموزش براتون مفید بوده باشدو  در آموزش بعدی نحوه استفاده از این نام کاربری و کلمه عبور برای لاگین خودکار و همچنین اتفاقات و متد های MyAutenticator را بطور مفصل مورد برسی قرار خواهیم داد. درصورتی که نکته نظر یا سوالی داشتید کامنت کنید.</p>
<p>نوشته <a rel="nofollow" href="http://www.mahditajik.ir/account-manager/">کار با Account-manager اندروید</a> اولین بار در <a rel="nofollow" href="http://www.mahditajik.ir">Mahdi Tajik</a> پدیدار شد.</p>
]]></content:encoded>
					
					<wfw:commentRss>http://www.mahditajik.ir/account-manager/feed/</wfw:commentRss>
			<slash:comments>5</slash:comments>
		
		
			</item>
	</channel>
</rss>
