0

问题是我正在重新启动我的服务,但我的内存仍然被占用。这是我主要活动的后退按钮。我调用 stopservice startservice 方法

@Override
public void onBackPressed() {
    stopService(new Intent(this, AUTOAlarmService.class));
    startService(new Intent(this, AUTOAlarmService.class));
    finish();
}

-- 服务重启,但内存没有重置。该服务位于 50MB 的内存上,并且已经运行了几个小时。该方法会重置它,但不会释放内存。是否有一些我遗漏的模糊方法可以用来释放内存?

public class AUTOAlarmService extends Service {
    public static final String DEFAULTNAME = "DefaultFile";
    Random ran = new Random(50000);
    ImageButton iblAUTOMATIONITEM, ibAUTOSEARCHSETTINGS, ibFINSHAUTOITEM;
    Button ibAUTOLOCATION, ibAUTOCATEGORY;
    EditText etNAME, etKEYWORDS;
    int numz;
    static final int uniqueID[] = { 1985, 1986, 1987, 1988, 1989, 1990, 1991,
            1992, 1993, 1994, 2000 };
    StringBuffer NPOCHECKING = new StringBuffer();
    private PowerManager.WakeLock mWakeLock0, mWakeLock1, mWakeLock2,
            mWakeLock3, mWakeLock4, mWakeLock5, mWakeLock6, mWakeLock7,
            mWakeLock8, mWakeLock9;
String THELocation = null;
String Checkn = null;
String THESEARCHSTRING = null;
String SearchKEYWORDS;
String THEName = null;
String KeyFront = "&query=";
String me = "NEWAUTO";
String SET = null;
String RUNNER = "AUTORUNNER-1";

@Override
public void onCreate() {
}

@Override
public IBinder onBind(Intent intent) {
    return null;`enter code here`
}



@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    SharedPreferences Search = getSharedPreferences(DEFAULTNAME, 0);
    String C0 = Search.getString("NICKCHECKER0", "none");
    String C1 = Search.getString("NICKCHECKER1", "none");
    String C2 = Search.getString("NICKCHECKER2", "none");
    String C3 = Search.getString("NICKCHECKER3", "none");
    String C4 = Search.getString("NICKCHECKER4", "none");
    String C5 = Search.getString("NICKCHECKER5", "none");
    String C6 = Search.getString("NICKCHECKER6", "none");
    String C7 = Search.getString("NICKCHECKER7", "none");
    String C8 = Search.getString("NICKCHECKER8", "none");
    String C9 = Search.getString("NICKCHECKER9", "none");
    String me0 = Search.getString("NickT0", "XJASZZZZZZZZZZZ");
    String me1 = Search.getString("NickT1", "XJASZZZZZZZZZZZ");
    String me2 = Search.getString("NickT2", "XJASZZZZZZZZZZZ");
    String me3 = Search.getString("NickT3", "XJASZZZZZZZZZZZ");
    String me4 = Search.getString("NickT4", "XJASZZZZZZZZZZZ");
    String me5 = Search.getString("NickT5", "XJASZZZZZZZZZZZ");
    String me6 = Search.getString("NickT6", "XJASZZZZZZZZZZZ");
    String me7 = Search.getString("NickT7", "XJASZZZZZZZZZZZ");
    String me8 = Search.getString("NickT8", "XJASZZZZZZZZZZZ");
    String me9 = Search.getString("NickT9", "XJASZZZZZZZZZZZ");
    Map<Thread, StackTraceElement[]> mee = Thread.getAllStackTraces();
    String C = mee.toString();
    if (C0.contentEquals("me0") == true && C.contains(me0) != true) {
        NICKX0();
        Sleeper();
    }
    if (C1.contentEquals("me1") == true && C.contains(me1) != true) {
        NICKX1();
        Sleeper();
    }
    if (C2.contentEquals("me2") == true && C.contains(me2) != true) {
        NICKX2();
        Sleeper();
    }
    if (C3.contentEquals("me3") == true && C.contains(me3) != true) {
        NICKX3();
        Sleeper();
    }
    if (C4.contentEquals("me4") == true && C.contains(me4) != true) {
        NICKX4();
        Sleeper();
    }
    if (C5.contentEquals("me5") == true && C.contains(me5) != true) {
        NICKX5();
        Sleeper();
    }
    if (C6.contentEquals("me6") == true && C.contains(me6) != true) {
        NICKX6();
        Sleeper();
    }
    if (C7.contentEquals("me7") == true && C.contains(me7) != true) {
        NICKX7();
        Sleeper();
    }
    if (C8.contentEquals("me8") == true && C.contains(me8) != true) {
        NICKX8();
        Sleeper();
    }
    if (C9.contentEquals("me9") == true && C.contains(me9) != true) {
        NICKX9();
        Sleeper();
    }
    return START_NOT_STICKY;
}

-- 因此,这些线程检查服务器的数据并在数据是用户未查看的数据时显示通知。它们一直运行,直到用户选择关闭它们。当我调用 stopservice 时,我应该杀死这些线程吗?

private void NICKX0() {
        Thread AUTOMATIONTIMER = new Thread(new Runnable() {
            @SuppressLint("Wakelock")
            @SuppressWarnings({ "rawtypes", "deprecation" })
            @Override
            public void run() {
                PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
                mWakeLock0 = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,
                        "Wake Log");
                SharedPreferences Search = getSharedPreferences(DEFAULTNAME, 0);
                SharedPreferences.Editor SearchE = Search.edit();
                int tester02 = ran.nextInt(50000);
                String Checkn0 = Search.getString("NICKCHECKER0", null);
                String THESEARCHSTRING0 = Search.getString("NCSEARCHER0", null);
                String THELocation0 = Search.getString("NICKLOCATION0", null);
                String Tester0 = Search.getString(Checkn0, null) + "<<"
                        + tester02 + ">>";
                String Tester00 = Tester0.replace("AUTORUNNER-1",
                        "AUTORUNNER-0");
                SearchE.putString(Checkn0, Tester0);
                SearchE.commit();
                int IndexS0, IndexF0;
                int looper = 0;
                String local0 = THELocation0;
                String checknn0 = null;
                String AUTOSET0 = null;
                String AUTONAME0 = null;
                try {
                    mWakeLock0.acquire();
                    while (looper <= 10
                            && Tester0.contentEquals(Search.getString(Checkn0,
                                    null)) == true
                            || Tester00.contentEquals(Search.getString(Checkn0,
                                    null)) == true) {
                        System.gc();
                        while (Tester0.contentEquals(Search.getString(Checkn0,
                                null)) == true && looper <= 50) {
                            try {
                                Thread.sleep(2000);
                            } catch (InterruptedException e) {
                                e.printStackTrace();
                            }
                            NPOCHECKING.append(Search.getString(
                                    "AUTOMATIONNPOITEMSLIST", null));
                            // //getid
                            checknn0 = Search.getString(Checkn0, null);
                            IndexS0 = checknn0.indexOf("xjaszsetS[") + 10;
                            IndexF0 = checknn0.indexOf("]xjaszsetF");
                            AUTOSET0 = checknn0.substring(IndexS0, IndexF0);
                            IndexS0 = checknn0.indexOf("xjasznameS[") + 11;
                            IndexF0 = checknn0.indexOf("]xjasznameF");
                            AUTONAME0 = checknn0.substring(IndexS0, IndexF0);
                            RUNCHECKER CHECKER = new RUNCHECKER();
                            try {
                                String returned = CHECKER.GETCHECKDATA(
                                        THESEARCHSTRING0, NPOCHECKING);
                                if (returned.contains("--XJASZ1985--") == true) {
                                    SharedPreferences prime = getSharedPreferences(
                                            DEFAULTNAME, 0);
                                    SharedPreferences.Editor primeE = prime
                                            .edit();
                                    primeE.putString(Checkn0 + "Data", returned
                                            + "--XJASZ1--" + local0
                                            + "--XJASZ2--");
                                    primeE.commit();
                                    looper = 0;
                                    if (AUTOSET0.contentEquals("NICKX0") == true) {
                                        try {
                                            Class classRunning0 = Class
                                                    .forName("com.site.example.Running0");
                                            Intent intentRunning0 = new Intent(
                                                    AUTOAlarmService.this,
                                                    classRunning0);
                                            PendingIntent pi0 = PendingIntent
                                                    .getActivity(
                                                            getBaseContext(),
                                                            0, intentRunning0,
                                                            0);
                                            String body0 = "You have results for "
                                                    + AUTONAME0;
                                            String title0 = "Results Found";
                                            Notification n0 = new Notification(
                                                    R.drawable.ccnotification,
                                                    body0,
                                                    System.currentTimeMillis());
                                            n0.setLatestEventInfo(
                                                    AUTOAlarmService.this,
                                                    title0, body0, pi0);
                                            n0.flags = Notification.FLAG_AUTO_CANCEL;
                                            n0.sound = Uri
                                                    .parse("android.resource://"
                                                            + getPackageName()
                                                            + "/"
                                                            + R.raw.posting);
                                            n0.defaults = Notification.DEFAULT_LIGHTS
                                                    | Notification.DEFAULT_VIBRATE;
                                            numz = 0;
                                            Thread.currentThread().setName(
                                                    "NickT0");
                                            String meeee = Thread
                                                    .currentThread().getName()
                                                    .toString();
                                            SearchE.putString("NickT0", meeee);
                                            SearchE.commit();
                                            NotificationManager nm0 = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
                                            nm0.notify(uniqueID[numz], n0);
                                        } catch (Exception e) {
                                            e.printStackTrace();
                                        }
                                    }
                                }
                            } catch (Exception e1) {
                                e1.printStackTrace();
                                looper++;
                                try {
                                    Thread.sleep(5000);
                                } catch (InterruptedException e) {
                                    e.printStackTrace();
                                }
                            }
                            if (looper == 0) {
                                try {
                                    Thread.sleep(15000);// /////////////////////////////////
                                } catch (InterruptedException e) {
                                    e.printStackTrace();
                                }
                            } else if (looper == 11) {
                                try {
                                    looper = 99;
                                    checknn0 = Search.getString(Checkn0, null);
                                    IndexS0 = checknn0.indexOf("xjasznameS[") + 11;
                                    IndexF0 = checknn0.indexOf("]xjasznameF");
                                    AUTONAME0 = checknn0.substring(IndexS0,
                                            IndexF0);
                                    Class classRunning10 = Class
                                            .forName("com.site.example.Home");
                                    Intent intentRunning10 = new Intent(
                                            AUTOAlarmService.this,
                                            classRunning10);
                                    PendingIntent pi10 = PendingIntent
                                            .getActivity(getBaseContext(), 0,
                                                    intentRunning10, 0);
                                    String body10 = AUTONAME0
                                            + " doesn't seem to be working?";
                                    String title10 = "The Results";
                                    Notification n10 = new Notification(
                                            R.drawable.ccnotification, body10,
                                            System.currentTimeMillis());
                                    n10.setLatestEventInfo(
                                            AUTOAlarmService.this, title10,
                                            body10, pi10);
                                    n10.flags = Notification.FLAG_AUTO_CANCEL;
                                    n10.sound = Uri.parse("android.resource://"
                                            + getPackageName() + "/"
                                            + R.raw.error);
                                    n10.defaults = Notification.DEFAULT_LIGHTS
                                            | Notification.DEFAULT_VIBRATE;
                                    numz = 10;
                                    NotificationManager nm10 = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
                                    nm10.notify(uniqueID[numz], n10);
                                } catch (ClassNotFoundException e) {
                                    e.printStackTrace();
                                }
                            }
                        }
                        try {
                            Thread.sleep(10000);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                    mWakeLock0.release();
                } catch (Exception e) {
                    e.printStackTrace();
                } finally {
                    try {
                        mWakeLock0.release();
                    } catch (RuntimeException e) {
                        e.printStackTrace();
                    }
                    Thread.currentThread().setName("none0");
                    ender();
                }
            }
        });
        AUTOMATIONTIMER.start();
    }

-- 倒数第二个是 NICKX 方法之一的构成。这些方法被放入一个运行自动搜索的循环中,直到用户选择一个选项来关闭线程。那时它会跳出循环并杀死线程。如果 ender() 方法运行时没有其他线程在运行,则服务将被终止。我知道它可能很难阅读,但我花了一天时间编写它,每种方法都运行完美。问题是它在服务超时时堆积内存。我想我会为狗屎和傻笑添加 ender() 。

private void ender() {
    try {
        Thread.sleep(250);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
    SharedPreferences Search = getSharedPreferences(DEFAULTNAME, 0);
    String me0 = Search.getString("NickT0", "XJASZZZZZZZZZZZ");
    String me1 = Search.getString("NickT1", "XJASZZZZZZZZZZZ");
    String me2 = Search.getString("NickT2", "XJASZZZZZZZZZZZ");
    String me3 = Search.getString("NickT3", "XJASZZZZZZZZZZZ");
    String me4 = Search.getString("NickT4", "XJASZZZZZZZZZZZ");
    String me5 = Search.getString("NickT5", "XJASZZZZZZZZZZZ");
    String me6 = Search.getString("NickT6", "XJASZZZZZZZZZZZ");
    String me7 = Search.getString("NickT7", "XJASZZZZZZZZZZZ");
    String me8 = Search.getString("NickT8", "XJASZZZZZZZZZZZ");
    String me9 = Search.getString("NickT9", "XJASZZZZZZZZZZZ");
    Map<Thread, StackTraceElement[]> mee = Thread.getAllStackTraces();
    String C = mee.toString();
    if (C.contains(me0) == true) {
    } else if (C.contains(me1) == true) {
    } else if (C.contains(me2) == true) {
    } else if (C.contains(me3) == true) {
    } else if (C.contains(me4) == true) {
    } else if (C.contains(me5) == true) {
    } else if (C.contains(me6) == true) {
    } else if (C.contains(me7) == true) {
    } else if (C.contains(me8) == true) {
    } else if (C.contains(me9) == true) {
    } else {
        stopService(new Intent(this, AUTOAlarmService.class));
    }
}

-- Ok ender() 在线程结束时被调用。它检查是否有任何其他线程在运行。如果没有,它会杀死破坏线程的服务。它工作正常。我想我会添加它只是为了基本上展示该服务如何从头到尾运行。

我可以在清除我的服务内存方面获得帮助吗?我可以使用一些晦涩难懂的方法吗?我一直在梳理 Google android 方法,但找不到我要找的东西。

4

0 回答 0