Android Activity Recycling and Operation timeout processing _android

Source: Internet
Author: User

This example for you to share the Android activity recovery and Operation timeout processing for your reference, the specific content as follows

1. Recovery of activity

Handling for multiple activity exits

Key code:

1), New Activity management class:

public class Activitycollector {
  private static list<activity> activitylist = new Arraylist<activity> () ;
  public static void addactivity [activity activity] {
    activitylist.add (activity);
  }
  public static void removeactivity [activity activity] {
    activitylist.remove (activity);
  }

  public static void Finishallbutlast () {activity activity
    = Activitylist.get (Activitylist.size ()-1);
    Removeactivity (activity);

    For (activity activityitem:activitylist) {
      if (!activityitem.isfinishing ()) {
        activityitem.finish ();
      }
    }

    Activitylist.clear ();
    Activitylist.add (activity);
  }

  public static void Finishall () {to (activity
    activity:activitylist) {
      if (!activity.isfinishing ())
        { Activity.finish ();
      }

    Activitylist.clear ();
  }

2, create the base class baseactivity, and make all the activity inherit from the base class. When you create it, you add it to the activity manager, and when you destroy it, you remove it from the activity manager.

public class Baseactivity extends activity {
  @Override
  protected void onCreate (Bundle savedinstancestate) {
    super.oncreate (savedinstancestate);
    Activitycollector.addactivity (this);
  }

  @Override
  protected void OnDestroy () {
    Super.ondestroy ();
    Activitycollector.removeactivity (this);
  }


If you need to destroy all activity, simply call Finishall ()

2, Operation timeout processing

Principle:

1, in the activity of the stop function, according to the app Process Importance_foreground Judge app in the foreground or backstage
2), in the activity of the Onresume function, do a time-out check.

Key code:

Abstract public class Timeoutcheckactivity extends Baseactivity {private Boolean isleave = false;
    @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);
  Pref = Getsharedpreferences (Constant.config_name, context.mode_private);

  /** * callback function to facilitate testing * @return/Abstract protected String gettag (); ......
   Ellipsis .../*** * executes the Onresume () method when the user restores the program to the foreground display, in which case the timeout is judged.
    * * @Override protected void Onresume () {//LOG.I ("Back", Gettag () + ", onresume, whether at the front desk:" + isonforeground ());
    Super.onresume ();
      if (isleave) {isleave = false;
    Timeoutcheck ();
    }} @Override protected void OnStop () {super.onstop ();
        if (!isonforeground ()) {if (!isleave && Isopenalp ()) {Isleave = true;
      Savestarttime ();
    }} public void Timeoutcheck () {Long endtime = System.currenttimemillis (); if (Endtime-getstarttime () >= Constant.timeout_alp * 10{Util.toast (this, "timeout, please verify");
      String ALP = pref.getstring (Constant.alp, NULL); if (ALP = NULL | | alp = = "") {} else {Intent Intent = new Intent (This, Unlockgesturepasswordactivity.clas
        s);
        Intent.putextra ("pattern", ALP); Intent.putextra ("Login", false);
        Gesture verification, no login verification intent.setflags (intent.flag_activity_clear_task);
      Open new Activity Startactivityforresult (intent, Constant.req_compare_pattern_timeout_check); }} public void Savestarttime () {Pref.edit (). Putlong (Constant.start_time, System.currenttimemillis ()). comm
  It ();
    Public long GetStartTime () {long starttime = 0;
    try {starttime = Pref.getlong (constant.start_time, 0);
    }catch (Exception e) {starttime = 0;
  return starttime; The/** * program is run on the front end and is implemented by enumerating the running apps. Prevent repeated timeout detection multiple times to ensure that only one activity enters timeout detection * When the user presses the home key, the program enters the back-end and returns false, and other circumstances cause the call of the stop function of the activity to return true * @return/PU Blic Boolean Isonforeground () {Activitymanager Activitymanager = (Activitymanager) getapplicationcontext (). GetSystemService (Cont Ext.
    Activity_service);

    String PackageName = Getapplicationcontext (). Getpackagename ();
    list<activitymanager.runningappprocessinfo> appprocesses = activitymanager.getrunningappprocesses ();

    if (appprocesses = null) return false; for (Activitymanager.runningappprocessinfo appprocess:appprocesses) {if appProcess.processName.equals (Packagenam e) && appprocess.importance = = ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND) {R
      Eturn true;
  return false;
 }
}


Supplementary Note:

Can be based on the different importance to judge the foreground or backstage, runningappprocessinfo inside the constant imtance is said above the foreground backstage, in fact, Imoportance is the importance of this app process, because the system recycling time, Will recycle the process according to Imoportance. You can look at the document specifically.

public static final int importance_background = 400//background public 
static final int importance_empty = 500//empty process 
public static final int importance_foreground = 100//at the front of the screen that can be captured to the focus can be understood as the activity Lifecycle Onresume (); 
public static final int importance_service = 300//in service public 
static final int importance_visible = 200//on screen front end, get focus can OnStart () understood as the activity life cycle;

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.