Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Authenticator Launch Login Screen When No Account Present

I've built an Authenticator, I've built a SyncAdapter (both can be manually executed through the settings on the emulator).

How do I get my application to launch the login screen (addAccount method) when the app launches, if no account is found?

Here is my Manifest.xml...

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.lateral.myapp"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="14"/>

    <uses-permission android:name="android.permission.INTERNET"/>
    <uses-permission android:name="android.permission.GET_ACCOUNTS"/>
    <uses-permission android:name="android.permission.MANAGE_ACCOUNTS"/>
    <uses-permission android:name="android.permission.AUTHENTICATE_ACCOUNTS"/>
    <uses-permission android:name="android.permission.USE_CREDENTIALS"/>    
    <uses-permission android:name="android.permission.READ_SYNC_STATS" />
    <uses-permission android:name="android.permission.READ_SYNC_SETTINGS" />
    <uses-permission android:name="android.permission.WRITE_SYNC_SETTINGS" />

    <application android:icon="@drawable/icon" android:label="@string/app_name" android:name=".myappApplication">
        <activity android:name=".ui.EventListActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN"/>

                <category android:name="android.intent.category.LAUNCHER"/>
            </intent-filter>
        </activity>

        <service android:name=".authenticator.AccountAuthenticatorService" android:exported="true" android:process=":auth">
            <intent-filter>
                <action android:name="android.accounts.AccountAuthenticator"/>
            </intent-filter>
            <meta-data android:name="android.accounts.AccountAuthenticator" android:resource="@xml/authenticator"/>
        </service>

        <service android:name="sync.EventsSyncAdapterService" android:exported="true" android:process=":events">
            <intent-filter>
                <action android:name="android.content.SyncAdapter" />
            </intent-filter>
            <meta-data android:name="android.content.SyncAdapter" android:resource="@xml/sync_events" />
        </service>

        <activity android:excludeFromRecents="true" android:name=".authenticator.myappAuthenticatorActivity">
            <!--
                No intent-filter here! This activity is only ever launched by
                someone who explicitly knows the class name
            -->
        </activity>
    </application>

</manifest>
like image 817
Rabbott Avatar asked Jun 12 '12 06:06

Rabbott


People also ask

Why can't I add an account to Microsoft authenticator?

If you don't have enough authentication methods on your account to get a strong authentication token, you can't add an account. You'll see an error in the Authenticator app that says “To set up Microsoft Authenticator, you'll need to go to aka.ms/mfasetup on a web browser.”

How do I add an account to the Authenticator app?

Open the Authenticator app, select Add account from the Customize and control icon in the upper-right, select Other account (Google, Facebook, etc.), and then select OR ENTER CODE MANUALLY. Enter an Account name (for example, Amazon) and type the Secret key from Step 1, and then select Finish.

What is Authenticator app on Android?

Google Authenticator is a free security app that can protect your accounts against password theft. It's easy to set up and can be used in a process called two-factor authentication (2FA) offered on popular services like Gmail, Facebook, Twitter, Instagram, and more.


2 Answers

Doesn't going through the AccountManager work? Something like:

 AccountManager accountManager = AccountManager.get(this);
 Account[] accounts = accountManager.getAccountsByType("myCustomAccount");
 if (accounts.length == 0) {
    accountManager.addAccount("myCustomAccount", null, null, null, this,
                               null, null);
  }
like image 186
Nikolay Elenkov Avatar answered Sep 27 '22 16:09

Nikolay Elenkov


In your 'EventListActivity' onCreate, check to see if there are any accounts. If there aren't, open the Authenticator activity. If you want to make it do the check and show Authenticator, before EventListActivity's UI shows, all you have to do is add the code before 'setContentView' in the onCreate method.

like image 39
Zaid Daghestani Avatar answered Sep 27 '22 17:09

Zaid Daghestani