应用关闭时仅运行前台服务以减少内存使用

Sur*_*iri 5 java service android broadcastreceiver

我在启动完成时通过广播接收器运行前台服务。它根据需要启动服务,只占用设备内存的一小部分,当我启动应用程序时,它会增加设备内存使用量,但当我关闭应用程序时,即使应用程序已被占用,它仍然占用太多内存关闭,只有前台服务正在运行。我真正想要的是它应该在应用程序关闭后使用与打开应用程序之前使用的内存量相同的内存量。

所以,我做了一些挖掘利用Android探查和我发现的是,当前景的服务,只打开行李箱后开始Application.classBroadcastReceiver.classService.class和其他一些背景的类。当我打开应用程序时,它会打开上述所有课程和其他活动。但是当我关闭应用程序时,它仍然使用设备内存来支持图形。我不知道如何在应用程序关闭后停止该内存使用。

这是我的Android Profiler 的一些屏幕截图

在通过前台通知启动应用程序之前使用了 65MB

请记住,在启动完成后,前台通知是从广播接收器启动的。 在启动应用程序之前

从通知内存启动应用程序后使用了 146 MB 启动应用程序后

浏览活动时内存使用 165 MB 通过活动冲浪

应用程序关闭后内存使用 140 MB 应用关闭后

现在我想知道如何实现使用之前65MB的内存使用量的任务?

这是我的 BroadcastReceiver 和 Service.class 代码。

广播接收器

public class BootCompletedIntentListener extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
    if("android.intent.action.BOOT_COMPLETED".equals(intent.getAction())){
        Intent serviceIntent = new Intent(context,ClipMonitorService.class);
        ContextCompat.startForegroundService(context,serviceIntent);
    }
}
}
Run Code Online (Sandbox Code Playgroud)

服务

public class ClipMonitorService extends Service {
private static final String TAG = "ClipboardManager";

private ExecutorService mThreadPool = Executors.newSingleThreadExecutor();
private ClipboardManager mClipboardManager;
private PrefManager prefManager;

@Override
public void onCreate() {
    super.onCreate();
    prefManager = new PrefManager(this);
}

@Override
public void onDestroy() {
    super.onDestroy();
    if (mClipboardManager != null) {
        mClipboardManager.removePrimaryClipChangedListener(
                mOnPrimaryClipChangedListener);
    }
}

@Override
public IBinder onBind(Intent intent) {
    return null;
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    Intent notificationIntent = new Intent(this, MainActivity.class);
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);

    Intent settingIntent = new Intent(this, SettingActivity.class);
    PendingIntent pendingSettIntent = PendingIntent.getActivity(this, 0, settingIntent, 0);

    RemoteViews remoteViews = new RemoteViews(getPackageName(), R.layout.custom_notification_layout);
    remoteViews.setOnClickPendingIntent(R.id.btn_action, pendingSettIntent);
    remoteViews.setTextViewText(R.id.notif_subtitle, "1 Clips copied Today");


    Notification notification = new NotificationCompat.Builder(this, CHANNEL_ID)
            .setSmallIcon(R.mipmap.ic_launcher_round)
            .setContent(remoteViews)
            .setVisibility(Notification.VISIBILITY_SECRET)
            .setPriority(NotificationCompat.PRIORITY_MIN)
            .setContentIntent(pendingIntent)
            .setColor(getResources().getColor(R.color.colorPrimary))
            .setShowWhen(false)
            .build();

    startForeground(1, notification);
    mClipboardManager =
            (ClipboardManager) getSystemService(CLIPBOARD_SERVICE);
    mClipboardManager.addPrimaryClipChangedListener(
            mOnPrimaryClipChangedListener);

    return START_STICKY;
}

private ClipboardManager.OnPrimaryClipChangedListener mOnPrimaryClipChangedListener =
        new ClipboardManager.OnPrimaryClipChangedListener() {
            @Override
            public void onPrimaryClipChanged() {
                Log.d(TAG, "onPrimaryClipChangeds");

                try {

                    String textToPaste = mClipboardManager.getPrimaryClip().getItemAt(0).getText().toString();

                    if (textToPaste.length() > 200) {
                        if (prefManager.isClipNotifOns()) {
                            mThreadPool.execute(new MakeNotifRunnable(
                                    textToPaste));

                        }

                    }
                } catch (Exception ignored) {

                }


            }
        };

private class MakeNotifRunnable implements Runnable {
    private final CharSequence mTextToWrite;

    public MakeNotifRunnable(CharSequence text) {
        mTextToWrite = text;
    }

    @Override
    public void run() {
        Intent notifIntent = new Intent(getApplicationContext(), PostNewsActivity.class);
        notifIntent.putExtra("post", mTextToWrite);

        NotificationManager notificationManager = (NotificationManager) getApplicationContext().getSystemService(Context.NOTIFICATION_SERVICE);

        int notificationId = 2;
        String channelId = "channel1";
        String channelName = "Clipboard Monitor Notification";
        int importance = 0;
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N) {
            importance = NotificationManager.IMPORTANCE_DEFAULT;
        }

        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
            NotificationChannel mChannel = new NotificationChannel(
                    channelId, channelName, importance);
            notificationManager.createNotificationChannel(mChannel);
        }

        NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(getApplicationContext(), channelId)
                .setSmallIcon(R.mipmap.ic_launcher_round)
                .setContentTitle("Verify copied content")
                .setContentText(mTextToWrite)
                .setAutoCancel(true)
                .setOnlyAlertOnce(true);

        TaskStackBuilder stackBuilder = TaskStackBuilder.create(getApplicationContext());
        stackBuilder.addNextIntent(notifIntent);
        PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(
                0,
                PendingIntent.FLAG_UPDATE_CURRENT
        );
        mBuilder.setContentIntent(resultPendingIntent);

        notificationManager.notify(notificationId, mBuilder.build());
    }
}
}
Run Code Online (Sandbox Code Playgroud)

帮助我减少内存使用量我会很感激你的回答。PS:由于我是 android 开发的新手,我可能上传了太多带有行话的信息。请原谅我。