How to create Time out app in Android studio

Create Time out android App

Welcome to android development ground. Today am going to show you how to create timed android app. This app is essential because some login and privacy app needs timing. If the app is in idle state  without being touched automatically it will logout. Lets start. The below app is compatible in and androidx.

1. Install android studio in your pc if you don’t have any. Open android Studio and start a new app.

2. Normally if you create an app in Android studio there will be MainActivity java and activity_main Xml so add the below code in java and Xml respectively.

3. Create another Java class and name it BaseActivity java and the codes below.


package com.tekzat.shoplogin;
import android.content.Intent;
import android.os.Bundle;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
public class BaseActivity extends AppCompatActivity implements LogoutListener{
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ((MyApp) getApplication()).registerSessionListener(this);
        ((MyApp) getApplication()).startUserSession();
    }
    @Override
    public void onUserInteraction() {
        super.onUserInteraction();
        ((MyApp) getApplication()).onUserInteracted();
    }
    @Override
    public void onSessionLogout() {
        finish();
        startActivity(new Intent(this, LoginActivity.class));
    }
}

check how to add Swipe to Refresh in your android app


4. 
Create another Java class and name it Welcome java and the codes below.

package com.tekzat.shoplogin;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
public class Welcome extends BaseActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_welcome);
    }
}

5. Create a Listener java activity and add below codes

package com.tekzat.shoplogin;
public interface LogoutListener {
    void onSessionLogout();
}

6. Go to Manifest and add this code as see here. There will be no mistake.

<?xml version=”1.0″ encoding=”utf-8″?>
<manifest xmlns:android=”http://schemas.android.com/apk/res/android”
    package=”com.tekzat.shoplogin”>
    <uses-permission android:name=”android.permission.INTERNET” />
    <application
        android:name=”.MyApp”
        android:allowBackup=”true”
        android:icon=”@mipmap/ic_launcher”
        android:label=”@string/app_name”
        android:roundIcon=”@mipmap/ic_launcher_round”
        android:supportsRtl=”true”
        android:theme=”@style/AppTheme”>
        <activity android:name=”.BaseActivity”/>
        <activity android:name=”.LoginActivity”/>
        <activity android:name=”.Welcome”/>
        <activity android:name=”.RegistrationActivity” />
        <activity android:name=”.ForgetPasswordActivity” />
        <activity android:name=”.MainActivity”>
            <intent-filter>
                <action android:name=”android.intent.action.MAIN” />
                <action android:name=”android.intent.action.VIEW” />
                <category android:name=”android.intent.category.LAUNCHER” />
            </intent-filter>
        </activity>
    </application>
</manifest>


7. Create another Java activity name MyApp or anything see codes below

package com.tekzat.shoplogin;
import android.app.Application;
import java.util.Timer;
import java.util.TimerTask;
public class MyApp extends Application {
    private Timer timer;
    private LogoutListener listener;
    public void startUserSession() {
        cancelTimer();
        timer = new Timer();
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                listener.onSessionLogout();
            }
        }, 10000);
    }
    private void cancelTimer() {
        if(timer !=null) timer.cancel();
    }
    public void registerSessionListener(LogoutListener listener) {
        this.listener = listener;
    }
    public void onUserInteracted() {
        startUserSession();
    }
}

You can attach the app to any part of the code you want to log out. You can also time the app according to this equation.

If you are having problem with the code, contact me.

seeausb@gmail.com.