我的应用程序在自定义toast中逐个显示所有包名称,但问题是,我不希望toast显示在应用程序外部,即当应用程序退出时。我该如何结束我的for循环或者以某种方式阻止烤面包机出现在ondestroy?

public class MainActivity extends Activity {

    float x1, x2;
    float y1, y2;

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

        // show first time setup
        SharedPreferences settings = getSharedPreferences("prefs", 0);
        boolean firstRun = settings.getBoolean("firstRun", false);
        if (firstRun == false)// if running for first time
        {
            SharedPreferences.Editor editor = settings.edit();
            editor.putBoolean("firstRun", true);
            editor.commit();
            Intent intent = new Intent(this, Setup1.class);
            startActivity(intent);
        } else {
        }
        // **//

        // NOTIFICATION ACTIVATOR
        // Fire Notification from ReceiverReminder
        Intent intent = new Intent(this, ReceiverReminder.class);
        PendingIntent sender = PendingIntent.getBroadcast(this, 0, intent,
                PendingIntent.FLAG_CANCEL_CURRENT);

        AlarmManager am = (AlarmManager) this
                .getSystemService(Context.ALARM_SERVICE);
        long recurring = (1 * 10000); // in milliseconds
        am.setRepeating(AlarmManager.RTC, Calendar.getInstance()
                .getTimeInMillis(), recurring, sender);
        //

        // Fire Notification from Scheduler Daily
        Intent intent1 = new Intent(this, ReceiverSchedulerDaily.class);
        PendingIntent sender1 = PendingIntent.getBroadcast(this, 0, intent1,
                PendingIntent.FLAG_CANCEL_CURRENT);

        AlarmManager am1 = (AlarmManager) this
                .getSystemService(Context.ALARM_SERVICE);
        long recurring1 = (3 * 60000); // in milliseconds
        am1.setRepeating(AlarmManager.RTC, Calendar.getInstance()
                .getTimeInMillis(), recurring1, sender1);
        //
        // Fire Notification from Scheduler 3 Days
        Intent intent11 = new Intent(this, ReceiverSchedulerThreeDays.class);
        PendingIntent sender11 = PendingIntent.getBroadcast(this, 0, intent11,
                PendingIntent.FLAG_CANCEL_CURRENT);

        AlarmManager am11 = (AlarmManager) this
                .getSystemService(Context.ALARM_SERVICE);
        long recurring11 = (1 * 10000); // in milliseconds
        am11.setRepeating(AlarmManager.RTC, Calendar.getInstance()
                .getTimeInMillis(), recurring11, sender11);
        //
        // Fire Notification from Scheduler 7 Days
        Intent intent111 = new Intent(this, ReceiverSchedulerWeekly.class);
        PendingIntent sender111 = PendingIntent.getBroadcast(this, 0,
                intent111, PendingIntent.FLAG_CANCEL_CURRENT);

        AlarmManager am111 = (AlarmManager) this
                .getSystemService(Context.ALARM_SERVICE);
        long recurring111 = (1 * 10000); // in milliseconds
        am111.setRepeating(AlarmManager.RTC, Calendar.getInstance()
                .getTimeInMillis(), recurring111, sender111);
        // --**--//

    }

    public boolean onTouchEvent(MotionEvent touchevent) {
        switch (touchevent.getAction()) {
        // when user first touches the screen we get x and y coordinate
        case MotionEvent.ACTION_DOWN: {
            x1 = touchevent.getX();
            y1 = touchevent.getY();
            break;
        }
        case MotionEvent.ACTION_UP: {
            x2 = touchevent.getX();
            y2 = touchevent.getY();

            // if left to right sweep event on screen
            if (x1 < x2) {
                Intent intent = new Intent(this, UiNetMon.class);
                startActivity(intent);
                overridePendingTransition(R.anim.slide_right_out,
                        R.anim.slide_right_in);
                finish();
            }

            // if right to left sweep event on screen
            if (x1 > x2) {
                Intent intent = new Intent(this, UiScheduler.class);
                startActivity(intent);
                overridePendingTransition(R.anim.slide_left_in,
                        R.anim.slide_left_out);
                finish();
            }
            break;
        }
        }
        return false;
    }

    public void optimize(View view) {

        // clean all app caches
        PackageManager pm = getPackageManager();
        Method[] methods = pm.getClass().getDeclaredMethods();
        for (Method m : methods) {
            if (m.getName().equals("freeStorageAndNotify")) {
                try {
                    long desiredFreeStorage = Long.MAX_VALUE;
                    m.invoke(pm, desiredFreeStorage, null);
                } catch (Exception e) {
                }
                break;
            }
        }
        //
        // enable, disable wifi
        WifiManager wifiManager = (WifiManager) this
                .getSystemService(Context.WIFI_SERVICE);
        wifiManager.setWifiEnabled(false);
        wifiManager.setWifiEnabled(true);
        //
        // Process Killer and display all package names in toast
        ActivityManager actvityManager = (ActivityManager) getApplicationContext()
                .getSystemService(getApplicationContext().ACTIVITY_SERVICE);
        List<RunningAppProcessInfo> procInfos = actvityManager
                .getRunningAppProcesses();

        for (int pnum = 0; pnum < procInfos.size(); pnum++) {
            // if
            // ((procInfos.get(pnum)).processName.contains("android")//prevent
            // system apps from getting killed
            // || (procInfos.get(pnum)).processName.contains("system")
            // || (procInfos.get(pnum)).processName.contains("huawei")
            // || (procInfos.get(pnum)).processName.contains("adil")) {
            // // Toast.makeText(getApplicationContext(), "system apps",
            // // Toast.LENGTH_SHORT).show();
            // } else {
            actvityManager
                    .killBackgroundProcesses(procInfos.get(pnum).processName);
            LayoutInflater inflater = getLayoutInflater();
            View layout = inflater.inflate(R.layout.custom_toast_layout,
                    (ViewGroup) findViewById(R.id.toast_layout_root));
            TextView text = (TextView) layout.findViewById(R.id.text);
            text.setText("optimized" + procInfos.get(pnum).processName);

            Toast toast = new Toast(getApplicationContext());
            toast.setGravity(Gravity.BOTTOM, 0, 0);
            toast.setDuration(Toast.LENGTH_LONG);
            toast.setView(layout);
            toast.show();
            //
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();

    }
}

最佳答案

你有很多祝酒词,因为它们是在一个循环中创建的。你只需要在onDestroy中取消它们。

...
    mPendingToasts.clear();
    for (int pnum = 0; pnum < procInfos.size(); pnum++) {
        ...
        Toast toast = new Toast(getApplicationContext());
        ...
        mPendingToasts.add(toast);
        toast.show();

    }
}

private ArrayList<Toast> mPendingToasts = new ArrayList<Toast>();
@Override
protected void onDestroy() {
    for(Toast toast:mPendingToasts){
        toast.cancel();
    }
    super.onDestroy();

}

请注意,取消祝酒只会阻止他们出现。循环中的逻辑(即killing proc)无论如何都将被执行。(即显示祝酒词是异步的)

07-27 13:26