I am using an open source app for Android. I just need to use it to send notifications to the user, but strangely, it allocates a lot of memory. I checked in Running Services and it is almost 20 MB (if I installed ACTION_BACKGROUND) or 30 MB (if I installed ACTION_FOREGROUND) ...
What should I do to reduce the use of this memory?
I already read this discussion . I don't have a bitmap or webview.
Here is my service:
public class NotificationService extends Service {
static final String ACTION_FOREGROUND = "com.example.android.apis.FOREGROUND";
static final String ACTION_BACKGROUND = "com.example.android.apis.BACKGROUND";
private static final Class<?>[] mSetForegroundSignature = new Class[] {
boolean.class};
private static final Class<?>[] mStartForegroundSignature = new Class[] {
int.class, Notification.class};
private static final Class<?>[] mStopForegroundSignature = new Class[] {
boolean.class};
private Method mSetForeground;
private Method mStartForeground;
private Method mStopForeground;
private Object[] mSetForegroundArgs = new Object[1];
private Object[] mStartForegroundArgs = new Object[2];
private Object[] mStopForegroundArgs = new Object[1];
void invokeMethod(Method method, Object[] args) {
try {
method.invoke(this, args);
} catch (InvocationTargetException e) {
Log.w("ApiDemos", "Unable to invoke method", e);
} catch (IllegalAccessException e) {
Log.w("ApiDemos", "Unable to invoke method", e);
}
}
void startForegroundCompat(int id, Notification notification) {
if (mStartForeground != null) {
mStartForegroundArgs[0] = Integer.valueOf(id);
mStartForegroundArgs[1] = notification;
invokeMethod(mStartForeground, mStartForegroundArgs);
return;
}
mSetForegroundArgs[0] = Boolean.TRUE;
invokeMethod(mSetForeground, mSetForegroundArgs);
}
void stopForegroundCompat(int id) {
if (mStopForeground != null) {
mStopForegroundArgs[0] = Boolean.TRUE;
invokeMethod(mStopForeground, mStopForegroundArgs);
return;
}
mSetForegroundArgs[0] = Boolean.FALSE;
invokeMethod(mSetForeground, mSetForegroundArgs);
}
@Override
public void onCreate() {
try {
mStartForeground = getClass().getMethod("startForeground",
mStartForegroundSignature);
mStopForeground = getClass().getMethod("stopForeground",
mStopForegroundSignature);
return;
} catch (NoSuchMethodException e) {
mStartForeground = mStopForeground = null;
}
try {
mSetForeground = getClass().getMethod("setForeground",
mSetForegroundSignature);
} catch (NoSuchMethodException e) {
throw new IllegalStateException(
"OS doesn't have Service.startForeground OR Service.setForeground!");
}
}
@Override
public void onDestroy() {
stopForegroundCompat(1);
}
@Override
public void onStart(Intent intent, int startId) {
handleCommand(intent);
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
handleCommand(intent);
return START_STICKY;
}
@Override
public void onRebind(Intent intent) {
super.onRebind(intent);
handleCommand(intent);
}
void handleCommand(Intent intent) {
if (intent == null)
return;
if (ACTION_FOREGROUND.equals(intent.getAction())) {
DBHelper db = new DBHelper(this);
String lastTime = db.getLastVisitTime();
if(!lastTime.equals("-1")) {
new Notifications(this).InviteUser();
}
String target = db.getTargetValue();
if(target.equals("")) {
new Notifications(this).TargetlessNotification();
}
db.close();
} else if (ACTION_BACKGROUND.equals(intent.getAction())) {
stopForegroundCompat(1);
}
}
@Override
public IBinder onBind(Intent intent) {
return null;
}
}
PS: I donβt know if it matters or not, but I run this onDestroy service of my application, so it will send a notification to the user at a specific time using AlarmManager. (Therefore, it should not be killed in order to avoid deleting the AlarmManager of my notification.)