Wednesday, January 9, 2013

Catch Home button pressed in Android


searching about how Catch Home button pressed in Android, lot of blogs, technics and method implemented by different- different developer but not sccess for this home button catching... some of the blogs and post says you can't catch Home button click in android due to security and user safety .
finally i found and implement a method to detect home button click and notify user for that see below code samples
for saving info in android am using SharedPreferences so a Session class structure to save all info

public class Session {

 private SharedPreferences pref;
 private Editor editor;
 private String KEY = "sampletest";

 public Session(Context context) {
  // TODO Auto-generated constructor stub
  pref = context.getApplicationContext().getSharedPreferences(KEY, Context.MODE_PRIVATE);
  editor = pref.edit();
 }

 public void savePreferenceData(String key, String value) {
  editor.putString(key, value);
  editor.commit();
 }

 public String getPreferenceData(String key) {
  return pref.getString(key, "");
 }
 
 public void clearall(){
  editor.clear();
  editor.commit();
 }
 
}


now create a common class structure that having technics to detects Home button clicks
public class Common extends Activity{

 private Session presession;
 private Boolean startactivitys;
 private Boolean UserLeaveHints;
 

 @Override
 protected void onResume() {
  // TODO Auto-generated method stub
  super.onResume();
  startactivitys = false;
  UserLeaveHints = false;
  System.out.println(this+" onResume ");
 }

 
 @Override
 public void startActivity(Intent intent) {
  // TODO Auto-generated method stub
  super.startActivity(intent);
  System.out.println(this+" startActivity ");
  startactivitys = true;
  presession = new Session(this);
  presession.savePreferenceData("onBackPressed", String.valueOf(false));
 }

 @Override
 protected void onStop() {
  // TODO Auto-generated method stub
  super.onStop();
  System.out.println(this+" onStop ");
  presession = new Session(this);
  boolean temp = presession.getPreferenceData("onBackPressed")!=null ? 
    Boolean.parseBoolean(presession.getPreferenceData("onBackPressed")):false;
  System.out.println("previous backpress "+temp);
  if(temp && UserLeaveHints){
   Toast.makeText(this, "app leaving", Toast.LENGTH_SHORT).show();
   System.out.println("++++++++++++++app leaving++++++++++++++++");
  }
  if(!startactivitys && UserLeaveHints){
   Toast.makeText(this, "app leaving", Toast.LENGTH_SHORT).show();
   System.out.println("++++++++++++++app leaving++++++++++++++++");
  }
 }

 @Override
 protected void onUserLeaveHint() {
  // TODO Auto-generated method stub
  super.onUserLeaveHint();
  System.out.println(this+" onUserLeaveHint ");
  UserLeaveHints = true;
 }

 @Override
 public void onBackPressed() {
  // TODO Auto-generated method stub
  super.onBackPressed();
  System.out.println(this+" onBackPressed ");
  presession = new Session(this);
  presession.savePreferenceData("onBackPressed", String.valueOf(true));
 }

 
}
see onStop function having Toast notification, that's show notification to user when user click on home and leaving application safely
One Question arising in mind how use this in your project ?
just simple extent this common class with your activity class instead of extending with activity .. and create Session as mention .
now you test this and provide feedback

No comments:

Post a Comment