2017-03-01 81 views
-2

其我的活动代码!如何退出时清除所有的应用程序缓存(如图像和数据) 可以帮助我如何做到这一点? 当用户关闭我的应用程序时,所有缓存将自动删除。 对我很重要。代码清除应用程序缓存退出android

package app.tabb; 
import co.ronash.pushe.Pushe; 

import android.content.Context; 
import android.content.DialogInterface; 
import android.content.Intent; 
import android.graphics.Paint; 
import android.location.LocationManager; 
import android.os.Bundle; 
import android.support.annotation.IdRes; 
import android.support.design.widget.CollapsingToolbarLayout; 
import android.support.design.widget.FloatingActionButton; 
import android.support.design.widget.NavigationView; 
import android.support.v4.app.Fragment; 
import android.support.v4.app.FragmentManager; 
import android.support.v4.app.FragmentTransaction; 
import android.support.v4.content.ContextCompat; 
import android.support.v4.view.GravityCompat; 
import android.support.v4.widget.DrawerLayout; 
import android.support.v7.app.ActionBar; 
import android.support.v7.app.ActionBarDrawerToggle; 
import android.support.v7.app.AlertDialog; 
import android.support.v7.app.AppCompatActivity; 
import android.support.v7.widget.Toolbar; 
import android.util.Log; 
import android.view.GestureDetector; 
import android.view.Menu; 
import android.view.MenuItem; 
import android.view.MotionEvent; 
import android.view.View; 
import android.view.inputmethod.InputMethodManager; 
import android.widget.ImageView; 
import android.widget.RelativeLayout; 
import android.widget.TextView; 
import android.widget.Toast; 

import com.google.android.gms.ads.AdRequest; 
import com.google.android.gms.ads.InterstitialAd; 
import com.nostra13.universalimageloader.core.ImageLoader; 

import java.io.File; 
import java.util.ArrayList; 

import app.tabb.data.AppConfig; 
import app.tabb.data.DatabaseHandler; 
import app.tabb.data.SharedPref; 
import app.tabb.fragment.FragmentCategory; 
import app.tabb.utils.Tools; 
import uk.co.chrisjenx.calligraphy.CalligraphyContextWrapper; 



public class ActivityMain extends AppCompatActivity { 

    //for ads 
    private InterstitialAd mInterstitialAd; 

    private ImageLoader imgloader = ImageLoader.getInstance(); 

    public ActionBar actionBar; 
    public Toolbar toolbar; 
    private int cat[]; 
    private FloatingActionButton fab; 
    private NavigationView navigationView; 
    private DatabaseHandler db; 
    private SharedPref sharedPref; 
    private RelativeLayout nav_header_lyt; 

    static ActivityMain activityMain; 


    ArrayList<String> slides; 
    ImageView imageView; 
    Custom_ViewFlipper viewFlipper; 
    GestureDetector mGestureDetector; 




    @Override 
    protected void attachBaseContext(Context newBase) { 
     super.attachBaseContext(CalligraphyContextWrapper.wrap(newBase)); 
    } 


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

     if (!imgloader.isInited()) Tools.initImageLoader(this); 
     fab = (FloatingActionButton) findViewById(R.id.fab); 
     db = new DatabaseHandler(this); 
     sharedPref = new SharedPref(this); 
     Pushe.initialize(this,true); 

     prepareAds(); 
     initToolbar(); 
     initDrawerMenu(); 
     prepareImageLoader(); 
     cat = getResources().getIntArray(R.array.id_category); 

     // first drawer view 
     onItemSelected(R.id.nav_featured); 
     actionBar.setTitle(getString(R.string.title_nav_featured)); 

     fab.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View view) { 
       Intent i = new Intent(ActivityMain.this, ActivitySearch.class); 
       startActivity(i); 
      } 
     }); 

     // for system bar in lollipop 
     Tools.systemBarLolipop(this); 
     viewFlipper = (Custom_ViewFlipper) findViewById(R.id.vf); 

     slides = new ArrayList<>(); 
     slides.add("http://www.bargnegar.ir/banner/1.jpg?"); 
     slides.add("http://www.bargnegar.ir/banner/2.jpg?"); 
     slides.add("http://www.bargnegar.ir/banner/3.jpg?"); 
     slides.add("http://www.bargnegar.ir/banner/4.jpg?"); 
     slides.add("http://www.bargnegar.ir/banner/5.jpg?"); 
     int i = 0; 
     while(i<slides.size()) 
     { 
      imageView = new ImageView(this); 
      imageView.setScaleType(ImageView.ScaleType.CENTER_CROP); 
      ImageLoader.getInstance().displayImage(slides.get(i),imageView); 
      viewFlipper.addView(imageView); 
      i++; 

     } 

     Paint paint = new Paint(); 
     paint.setColor(ContextCompat.getColor(this,R.color.colorPrimary)); 
     viewFlipper.setPaintCurrent(paint); 
     paint = new Paint(); 

     paint.setColor(ContextCompat.getColor(this,android.R.color.white)); 
     viewFlipper.setPaintNormal(paint); 

     viewFlipper.setRadius(6); 
     viewFlipper.setMargin(4); 


     CustomGestureDetector customGestureDetector = new CustomGestureDetector(); 
     mGestureDetector = new GestureDetector(ActivityMain.this, customGestureDetector); 

     viewFlipper.setOnTouchListener(new View.OnTouchListener() { 
      @Override 
      public boolean onTouch(View view, MotionEvent motionEvent) { 
       mGestureDetector.onTouchEvent(motionEvent); 
       return true; 
      } 
     }); 


    } 



    class CustomGestureDetector extends GestureDetector.SimpleOnGestureListener { 
     @Override 
     public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) { 

      // Swipe left (next) 
      if (e1.getX() > e2.getX()) { 
       viewFlipper.setInAnimation(ActivityMain.this, R.anim.left_in); 
       viewFlipper.setOutAnimation(ActivityMain.this, R.anim.left_out); 

       viewFlipper.showNext(); 
      }else if (e1.getX() < e2.getX()) { 
       viewFlipper.setInAnimation(ActivityMain.this, R.anim.right_in); 
       viewFlipper.setOutAnimation(ActivityMain.this, R.anim.right_out); 

       viewFlipper.showPrevious(); 
      } 

      viewFlipper.setInAnimation(ActivityMain.this, R.anim.left_in); 
      viewFlipper.setOutAnimation(ActivityMain.this, R.anim.left_out); 

      return super.onFling(e1, e2, velocityX, velocityY); 
     } 

    } 









    private void initToolbar() { 
     toolbar = (Toolbar) findViewById(R.id.toolbar); 
     setSupportActionBar(toolbar); 
     actionBar = getSupportActionBar(); 
     actionBar.setDisplayHomeAsUpEnabled(true); 
     actionBar.setHomeButtonEnabled(true); 

     Tools.setActionBarColor(this, actionBar); 
    } 

    private void initDrawerMenu() { 
     DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout); 
     ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close) { 
      @Override 
      public void onDrawerOpened(View drawerView) { 
       updateFavoritesCounter(navigationView, R.id.nav_favorites, db.getFavoritesSize()); 
       showInterstitial(); 
       super.onDrawerOpened(drawerView); 
      } 
     }; 
     drawer.setDrawerListener(toggle); 
     toggle.syncState(); 

     navigationView = (NavigationView) findViewById(R.id.nav_view); 
     navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() { 
      @Override 
      public boolean onNavigationItemSelected(MenuItem item) { 
       actionBar.setTitle(item.getTitle().toString()); 
       return onItemSelected(item.getItemId()); 
      } 
     }); 

     // navigation header 
     View nav_header = navigationView.getHeaderView(0); 
     nav_header_lyt = (RelativeLayout) nav_header.findViewById(R.id.nav_header_lyt); 
     nav_header_lyt.setBackgroundColor(Tools.colorBrighter(sharedPref.getThemeColorInt())); 
     (nav_header.findViewById(R.id.menu_nav_setting)).setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View view) { 
       Intent i = new Intent(getApplicationContext(), ActivitySetting.class); 
       startActivity(i); 
      } 
     }); 

     (nav_header.findViewById(R.id.menu_nav_map)).setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View view) { 
       Intent i = new Intent(getApplicationContext(), ActivityMaps.class); 
       startActivity(i); 
      } 
     }); 
    } 

    @Override 
    public void onBackPressed() { 
     DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout); 
     if (!drawer.isDrawerOpen(GravityCompat.START)) { 
      drawer.openDrawer(GravityCompat.START); 
     } else { 
      doExitApp(); 
     } 
    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     getMenuInflater().inflate(R.menu.menu_activity_main, menu); 
     return true; 
    } 

    @Override 
    public boolean onOptionsItemSelected(MenuItem item) { 
     int id = item.getItemId(); 
     if (id == R.id.action_settings) { 
      Intent i = new Intent(getApplicationContext(), ActivitySetting.class); 
      startActivity(i); 
     } else if (id == R.id.action_rate) { 
      Tools.rateAction(ActivityMain.this); 
     } else if (id == R.id.action_about) { 
      Tools.aboutAction(ActivityMain.this); 
     } 

     return super.onOptionsItemSelected(item); 
    } 

    public boolean onItemSelected(int id) { 
     // Handle navigation view item clicks here. 
     Fragment fragment = null; 
     Bundle bundle = new Bundle(); 
     switch (id) { 
      //sub menu 
      case R.id.nav_all: 
       fragment = new FragmentCategory(); 
       bundle.putInt(FragmentCategory.TAG_CATEGORY, -1); 
       break; 
      // favorites 
      case R.id.nav_favorites: 
       fragment = new FragmentCategory(); 
       bundle.putInt(FragmentCategory.TAG_CATEGORY, -2); 
       break; 

      case R.id.nav_atolie: 
       fragment = new FragmentCategory(); 
       bundle.putInt(FragmentCategory.TAG_CATEGORY, cat[0]); 
       break; 
      case R.id.nav_arayesh: 
       fragment = new FragmentCategory(); 
       bundle.putInt(FragmentCategory.TAG_CATEGORY, cat[1]); 
       break; 
      case R.id.nav_behdashti: 
       fragment = new FragmentCategory(); 
       bundle.putInt(FragmentCategory.TAG_CATEGORY, cat[2]); 
       break; 
      case R.id.nav_safar: 
       fragment = new FragmentCategory(); 
       bundle.putInt(FragmentCategory.TAG_CATEGORY, cat[3]); 
       break; 
      case R.id.nav_peyk: 
       fragment = new FragmentCategory(); 
       bundle.putInt(FragmentCategory.TAG_CATEGORY, cat[4]); 
       break; 
      case R.id.nav_bank: 
       fragment = new FragmentCategory(); 
       bundle.putInt(FragmentCategory.TAG_CATEGORY, cat[5]); 
       break; 
      case R.id.nav_pooshak: 
       fragment = new FragmentCategory(); 
       bundle.putInt(FragmentCategory.TAG_CATEGORY, cat[6]); 
       break; 
      case R.id.nav_sukht: 
       fragment = new FragmentCategory(); 
       bundle.putInt(FragmentCategory.TAG_CATEGORY, cat[7]); 
       break; 
      case R.id.nav_khodro: 
       fragment = new FragmentCategory(); 
       bundle.putInt(FragmentCategory.TAG_CATEGORY, cat[8]); 
       break; 
      case R.id.nav_food: 
       fragment = new FragmentCategory(); 
       bundle.putInt(FragmentCategory.TAG_CATEGORY, cat[9]); 
       break; 
      case R.id.nav_featured: 
       fragment = new FragmentCategory(); 
       bundle.putInt(FragmentCategory.TAG_CATEGORY, cat[10]); 
       break; 
      case R.id.nav_zanjirei: 
       fragment = new FragmentCategory(); 
       bundle.putInt(FragmentCategory.TAG_CATEGORY, cat[11]); 

       break; 
      case R.id.nav_ghanadi: 
       fragment = new FragmentCategory(); 
       bundle.putInt(FragmentCategory.TAG_CATEGORY, cat[12]); 

       break; 
      case R.id.nav_coffeshop: 
       fragment = new FragmentCategory(); 
       bundle.putInt(FragmentCategory.TAG_CATEGORY, cat[13]); 
       break; 

      case R.id.nav_ketring: 
       fragment = new FragmentCategory(); 
       bundle.putInt(FragmentCategory.TAG_CATEGORY, cat[14]); 
       break; 

      case R.id.nav_tourismcenter: 
       fragment = new FragmentCategory(); 
       bundle.putInt(FragmentCategory.TAG_CATEGORY, cat[15]); 
       break; 

      case R.id.nav_shop: 
       fragment = new FragmentCategory(); 
       bundle.putInt(FragmentCategory.TAG_CATEGORY, cat[16]); 
       break; 

      case R.id.nav_hospital: 
       fragment = new FragmentCategory(); 
       bundle.putInt(FragmentCategory.TAG_CATEGORY, cat[17]); 
       break; 

      case R.id.nav_mobl: 
       fragment = new FragmentCategory(); 
       bundle.putInt(FragmentCategory.TAG_CATEGORY, cat[18]); 
       break; 

      case R.id.nav_mobile: 
       fragment = new FragmentCategory(); 
       bundle.putInt(FragmentCategory.TAG_CATEGORY, cat[19]); 
       break; 

      case R.id.nav_sport: 
       fragment = new FragmentCategory(); 
       bundle.putInt(FragmentCategory.TAG_CATEGORY, cat[20]); 

       break; 

      case R.id.nav_hotels: 
       fragment = new FragmentCategory(); 
       bundle.putInt(FragmentCategory.TAG_CATEGORY, cat[21]); 
       break; 

      case R.id.nav_khanegi: 
       fragment = new FragmentCategory(); 
       bundle.putInt(FragmentCategory.TAG_CATEGORY, cat[22]); 
       break; 

      default: 
       break; 


      /* IMPORTANT : cat[index_array], index is start from 0 
      */ 
     } 


     if (fragment != null) { 
      fragment.setArguments(bundle); 
      FragmentManager fragmentManager = getSupportFragmentManager(); 
      FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction(); 
      fragmentTransaction.replace(R.id.frame_content, fragment); 
      fragmentTransaction.commit(); 
      //initToolbar(); 
     } 
     DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout); 
     drawer.closeDrawer(GravityCompat.START); 
     return true; 
    } 

    private void hideKeyboard() { 
     View view = this.getCurrentFocus(); 
     if (view != null) { 
      InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); 
      imm.hideSoftInputFromWindow(view.getWindowToken(), 0); 
     } 
    } 

    private long exitTime = 0; 

    public void doExitApp() { 
     if ((System.currentTimeMillis() - exitTime) > 2000) { 
      Toast.makeText(this, R.string.press_again_exit_app, Toast.LENGTH_SHORT).show(); 
      exitTime = System.currentTimeMillis(); 
     } else { 
      finish(); 
     } 
    } 

    private void prepareImageLoader() { 
     Tools.initImageLoader(this); 
    } 

    private void showDialogGPS() { 
     AlertDialog.Builder builder = new AlertDialog.Builder(this); 
     builder.setCancelable(false); 
     builder.setTitle("مکان یابی خودکار"); 
     builder.setMessage("سرویس مکان یابی دیوایس شما فعال نیست . جهت استفاده بهتر از برنامه لطفا آن را فعال کنید."); 
     builder.setInverseBackgroundForced(true); 
     builder.setPositiveButton("فعال کردن", new DialogInterface.OnClickListener() { 
      public void onClick(DialogInterface dialog, int which) { 
       startActivity(
         new Intent(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS)); 
      } 
     }); 
     builder.setNegativeButton("نادیده گرفتن", new DialogInterface.OnClickListener() { 
      public void onClick(DialogInterface dialog, int which) { 
       dialog.dismiss(); 
      } 
     }); 
     AlertDialog alert = builder.create(); 
     alert.show(); 
    } 

    @Override 


    protected void onResume() { 
     LocationManager mlocManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE); 
     boolean enabled = mlocManager.isProviderEnabled(LocationManager.GPS_PROVIDER); 

     if(!enabled) { 
      showDialogGPS(); 
     } 

     if (!imgloader.isInited()) Tools.initImageLoader(this); 
     updateFavoritesCounter(navigationView, R.id.nav_favorites, db.getFavoritesSize()); 
     if (actionBar != null) { 
      Tools.setActionBarColor(this, actionBar); 
      // for system bar in lollipop 
      Tools.systemBarLolipop(this); 
     } 
     if (nav_header_lyt != null) { 
      nav_header_lyt.setBackgroundColor(Tools.colorBrighter(sharedPref.getThemeColorInt())); 
     } 
     super.onResume(); 
    } 


    private void updateFavoritesCounter(NavigationView nav, @IdRes int itemId, int count) { 
     TextView view = (TextView) nav.getMenu().findItem(itemId).getActionView().findViewById(R.id.counter); 
     view.setText(String.valueOf(count)); 
    } 

    private void prepareAds() { 
     // Create the InterstitialAd and set the adUnitId. 
     mInterstitialAd = new InterstitialAd(this); 
     // Defined in res/values/strings.xml 
     mInterstitialAd.setAdUnitId(getString(R.string.interstitial_ad_unit_id)); 
     //prepare ads 
     AdRequest adRequest2 = new AdRequest.Builder().build(); 
     mInterstitialAd.loadAd(adRequest2); 
    } 

    /** 
    * show ads 
    */ 
    public void showInterstitial() { 
     // Show the ad if it's ready 
     if (AppConfig.ADS_MAIN_INTERSTITIAL && mInterstitialAd != null && mInterstitialAd.isLoaded()) { 
      mInterstitialAd.show(); 
     } 
    } 


    public static ActivityMain getInstance() { 
     return activityMain; 
    } 

    public static void animateFab(final boolean hide) { 
     FloatingActionButton f_ab = (FloatingActionButton) activityMain.findViewById(R.id.fab); 
     int moveY = hide ? (2 * f_ab.getHeight()) : 0; 
     f_ab.animate().translationY(moveY).setStartDelay(100).setDuration(400).start(); 
    } 
} 

回答