Headertab

Drop Down MenusCSS Drop Down MenuPure CSS Dropdown Menu

Thursday, 30 June 2016

Detecting when your Android app goes background / foreground

Hello friends
If you want to  know your application background or foreground then you follow below instructions.
i am use third party lib.

First you add android studio gradle dependency lib...

build.gradle

apply plugin: 'com.android.application'

android {
    compileSdkVersion 23
    buildToolsVersion "23.0.2"

    defaultConfig {
        applicationId "com.example.user.audiomusic"
        minSdkVersion 15
        targetSdkVersion 23
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    testCompile 'junit:junit:4.12'
    compile 'com.android.support:appcompat-v7:23.4.0'
    compile 'com.sjl:Foredroid:1.0.0'
}

BaseActivity.java

package com.example.user.audiomusic.parentActivity;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;

import com.example.user.audiomusic.MyApplication;
import com.example.user.audiomusic.R;
import com.sjl.foreground.Foreground;

public abstract class BaseActivity extends AppCompatActivity implements Foreground.Listener {

    private Foreground.Binding listenerBinding;

    //protected abstract void startAnotherActivity();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
       setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

        listenerBinding = Foreground.get(getApplication()).addListener(this);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        listenerBinding.unbind();
    }

    @Override
    public void onBecameForeground() {
        MyApplication.getInstance().startService();
        Log.i(Foreground.TAG, getClass().getName() + " became foreground");
    }

    @Override
    public void onBecameBackground() {
        MyApplication.getInstance().musicSrv.pauseMusic();
        Log.i(Foreground.TAG, getClass().getName() + " went background");
    }

}


actvity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.user.audiomusic.MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!" />

    <Button
        android:layout_margin="10dp"
        android:textSize="20dp"
        android:layout_gravity="center_horizontal"
        android:text="First"
        android:id="@+id/second"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <Button
        android:layout_margin="10dp"
        android:textSize="20dp"
        android:layout_gravity="center_horizontal"
        android:text="Second"
        android:id="@+id/third"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    <Button
        android:layout_margin="10dp"
        android:textSize="20dp"
        android:layout_gravity="center_horizontal"
        android:text="Third"
        android:id="@+id/fourth"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
</LinearLayout>

MainActvity.java

package com.example.user.audiomusic;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;

import com.example.user.audiomusic.parentActivity.BaseActivity;


public class MainActivity extends BaseActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);



        findViewById(R.id.second).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MainActivity.this,SecondActivity.class);
                startActivity(intent);

            }
        });

        findViewById(R.id.third).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MainActivity.this,ThirdActivity.class);
                startActivity(intent);

            }
        });

        findViewById(R.id.fourth).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MainActivity.this,FourthActivity.class);
                startActivity(intent);

            }
        });
    }
 
    @Override
    protected void onDestroy() {
        super.onDestroy();
        Log.e("MainActvity","onDestroy");
    }
}



second_actvity.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:orientation="vertical"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.user.audiomusic.SecondActivity">
<TextView
    android:textSize="20dp"
    android:text="I am 2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />


    <Button
        android:layout_gravity="center_horizontal"
        android:textSize="25dp"
        android:id="@+id/sec_next"
        android:text="Next"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
</LinearLayout>


SecondActvity.java

package com.example.user.audiomusic;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;

import com.example.user.audiomusic.parentActivity.BaseActivity;


public class SecondActivity extends BaseActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_second);
        findViewById(R.id.sec_next).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(SecondActivity.this,FourthActivity.class));
            }
        });    }
 
}


third_actvity.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.user.audiomusic.ThirdActivity">

</RelativeLayout>

ThirdActvity.java
 package com.example.user.audiomusic;

import android.os.Bundle;
import android.util.Log;

import com.example.user.audiomusic.parentActivity.BaseActivity;


public class ThirdActivity extends BaseActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_third);
    }
 
}

FourthActivity.java

package com.example.user.audiomusic;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;

import com.example.user.audiomusic.parentActivity.BaseActivity;

public class FourthActivity extends BaseActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_fourth);
        findViewById(R.id.fourth_next).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(FourthActivity.this,ThirdActivity.class));
            }
        });
    }
}


actvity_fourth.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:orientation="vertical"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.user.audiomusic.FourthActivity">
    <TextView
        android:textSize="20dp"
        android:text="I am 4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />


    <Button
        android:layout_gravity="center_horizontal"
        android:textSize="25dp"
        android:id="@+id/fourth_next"
        android:text="Next"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
</LinearLayout>


Thank you

FullSourceCode
Live Sample