java - Clear cache of apps in android programitiically Not for images -
in android application, want make app has button named cleancache , inside code list down applications cache , clear toast message cache cleared. looked @ different questions , answers didn't find interesting. i'd implemented consists of errors.
here mainactivity.java file:
package com.example.ahsankhan.cache; import android.app.activity; import android.content.context; import android.os.bundle; import android.view.view; import android.view.view.onclicklistener; import android.widget.button; import java.io.file; public class mainactivity extends activity { @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); addlisteneronbutton(); } public void addlisteneronbutton() { //select specific button bundle action want button button = (button) findviewbyid(r.id.button1); button.setonclicklistener(new onclicklistener() { @override public void onclick(view view) { @override public void oncreate(bundle *) { super.oncreate(*); setcontentview(r.layout.activity_main); } @override protected void onstop(){ super.onstop(); } //fires after onstop() state @override protected void ondestroy() { super.ondestroy(); try { trimcache(this); } catch (exception e) { // todo auto-generated catch block e.printstacktrace(); } } public static void trimcache(context context) { try { file dir = context.getcachedir(); if (dir != null && dir.isdirectory()) { deletedir(dir); } } catch (exception e) { // todo: handle exception } } public static boolean deletedir(file dir) { if (dir != null && dir.isdirectory()) { string[] children = dir.list(); (int = 0; < children.length; i++) { boolean success = deletedir(new file(dir, children[i])); if (!success) { return false; } } } // directory empty delete return dir.delete(); } } }); } }
activity_main.xml file
<?xml version="1.0" encoding="utf-8"?> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="button - go mkyong.com" /> </linearlayout>
androidmanifest.xml file
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.ahsankhan.cache" > <application android:allowbackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:theme="@style/apptheme" > <activity android:name=".mainactivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.main" /> <category android:name="android.intent.category.launcher" /> </intent-filter> </activity> </application> </manifest>
take @ line:
public void oncreate(bundle *) { super.oncreate(*);
you can't use * variable name. has sane this:
public void oncreate(bundle b) { super.oncreate(b);
Comments
Post a Comment