Best Working Code to set event Listener for Home button of Device,Android

I Found issue of ,the method onKeyDown() will not work (in Latest version of
Android 4.0 and after vertions) ,when user press home button of the Device in LegalPlex Project..

i resolved this issue by using a method of:

1)public void onUserLeaveHint() { // this only executes when Home is <span id="IL_AD7" class="IL_AD">selected</span>.
        // do stuff
        super.onUserLeaveHint();
        System.out.println("HOMEEEEEEEEE");
    }
Note : Place this method on below of onCreate() method,then u wil get..

2) above method will raised issues like,it called when user press back button and also in other cases..
to overcome use this :

@Override
    protected void onPause() {
        super.onPause();
        if(isApplicationSentToBackground(this)){
            SharedVariables.is_application_is_paused=true;
           
                       
            if(mp.isPlaying()){
                SharedVariables.is_mp_music_played=true;
                mp.pause();
            }
           
            if(mp1.isPlaying()){
                SharedVariables.is_mp1_music_played=true;
                mp1.pause();
            }
        }
    }
   
    public boolean isApplicationSentToBackground(final Context context) {
        ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
        List<RunningTaskInfo> tasks = am.getRunningTasks(1);
        if (!tasks.isEmpty()) {
            ComponentName topActivity = tasks.get(0).topActivity;
            if (!topActivity.getPackageName().equals(context.getPackageName())) {
                return true;
            }
        }
        return false;
    }
   
   
    @Override
    protected void onRestart() {
       
            super.onRestart();
            if(SharedVariables.is_application_is_paused)
            {
                SharedVariables.is_application_is_paused=false;   
               
                if(SharedVariables.is_mp1_music_played){                   
                    mp1=MediaPlayer.create(getApplicationContext(), R.raw.sample1);//Background music that plays continuously..
                    mp1.start();
                    mp1.setLooping(true);
                   
                }else   if(SharedVariables.is_mp_music_played){
                    mp=MediaPlayer.create(getApplicationContext(), R.raw.mad_plans_short); //Music that plays when clicked the spin button.
                    mp.start();
                }
               
                SharedVariables.is_mp_music_played=false;
                SharedVariables.is_mp1_music_played=false;
            }
    }

->here case is stopped  music when user press home button ,and play music when application opened.
and give permission in Manifest file:
<uses-permission android:name="android.permission.GET_TASKS" />   

i implemented this in my SlotGame Project....

Comments

  1. is the application developed in phonegap? or a native android app? what does the span id represent?

    How to do this on a native android app

    ReplyDelete
    Replies
    1. Hi,

      This application was developed in Native Android App only.
      don't worry about that span id,its commented one and i didn't use that span id
      in above source code.

      Thanking you,
      @Pavan

      Delete
  2. Hi pavan,

    Thanks for sharing the valuable code. It was very helpful

    ReplyDelete

Post a Comment

Popular posts from this blog

Custom Dialog with no title and full screen in Android

Best working code to set Date limits on Date Picker Dialog,in Android

code to display .pdf,.text,.DOC,.DOCX,.doc extension files exists in SDCard