Android中的服务太多缓存

时间:2016-07-19 07:20:28

标签: java android service countdown

我有一个带有countDown的应用程序,它运行Service和一些SharedPreferences。问题是:为什么我的应用程序会占用我这么多高速缓存,高达56mb?

XML

<Button
    android:text="Start"
    android:layout_width="97dp"
    android:layout_height="wrap_content"
    tools:layout_editor_absoluteX="24dp"
    tools:layout_editor_absoluteY="447dp"
    android:id="@+id/btnStart"
    app:layout_constraintLeft_toLeftOf="@+id/activity_amplitudination"
    android:layout_marginLeft="24dp"
    android:layout_marginStart="24dp"
    tools:layout_constraintLeft_creator="0"
    app:layout_constraintTop_toBottomOf="@+id/progressBar"
    android:layout_marginTop="8dp"
    tools:layout_constraintTop_creator="0"
    app:layout_constraintBottom_toBottomOf="@+id/activity_amplitudination"
    android:layout_marginBottom="16dp"
    tools:layout_constraintBottom_creator="0"
    app:layout_constraintVertical_bias="1.0" />



<Button
    android:text="Reset"
    android:layout_width="99dp"
    android:layout_height="wrap_content"
    tools:layout_editor_absoluteX="145dp"
    tools:layout_editor_absoluteY="447dp"
    android:id="@+id/btnResetProgram"
    app:layout_constraintLeft_toRightOf="@+id/btnStart"
    android:layout_marginLeft="8dp"
    android:layout_marginStart="8dp"
    tools:layout_constraintLeft_creator="0"
    app:layout_constraintTop_toTopOf="@+id/activity_amplitudination"
    android:layout_marginTop="16dp"
    tools:layout_constraintTop_creator="0"
    app:layout_constraintRight_toLeftOf="@+id/btnFinishAmplitudination"
    android:layout_marginRight="8dp"
    android:layout_marginEnd="8dp"
    tools:layout_constraintRight_creator="0"
    app:layout_constraintBottom_toBottomOf="@+id/btnStart"
    tools:layout_constraintBottom_creator="0"
    app:layout_constraintVertical_bias="1.0" />


<ProgressBar
    android:id="@+id/progressBar"
    android:layout_width="321dp"
    android:layout_height="275dp"
    android:indeterminate="false"
    android:progressDrawable="@drawable/circular_progress_bar"
    android:background="@drawable/circular_progress_bar"
    style="?android:attr/progressBarStyleHorizontal"
    android:max="100"
    android:progress="100"
    tools:layout_editor_absoluteX="31dp"
    tools:layout_editor_absoluteY="54dp"
    app:layout_constraintLeft_toLeftOf="@+id/activity_amplitudination"
    android:layout_marginLeft="16dp"
    android:layout_marginStart="16dp"
    tools:layout_constraintLeft_creator="0"
    app:layout_constraintTop_toTopOf="@+id/activity_amplitudination"
    android:layout_marginTop="16dp"
    tools:layout_constraintTop_creator="0"
    app:layout_constraintRight_toRightOf="@+id/activity_amplitudination"
    android:layout_marginRight="16dp"
    android:layout_marginEnd="16dp"
    tools:layout_constraintRight_creator="0"
    app:layout_constraintBottom_toBottomOf="@+id/activity_amplitudination"
    android:layout_marginBottom="16dp"
    tools:layout_constraintBottom_creator="0"
    app:layout_constraintVertical_bias="0.19" />

<TextView
    android:layout_width="189dp"
    android:layout_height="60dp"
    tools:layout_editor_absoluteX="108dp"
    tools:layout_editor_absoluteY="168dp"
    android:id="@+id/textShowTimer"
    android:textAppearance="@style/TextAppearance.AppCompat.Display2"
    android:text="00:00:00"
    app:layout_constraintLeft_toLeftOf="@+id/activity_amplitudination"
    android:layout_marginLeft="16dp"
    android:layout_marginStart="16dp"
    tools:layout_constraintLeft_creator="0"
    app:layout_constraintTop_toTopOf="@+id/activity_amplitudination"
    android:layout_marginTop="16dp"
    tools:layout_constraintTop_creator="0"
    app:layout_constraintRight_toRightOf="@+id/activity_amplitudination"
    android:layout_marginRight="16dp"
    android:layout_marginEnd="16dp"
    tools:layout_constraintRight_creator="0"
    app:layout_constraintBottom_toBottomOf="@+id/progressBar"
    tools:layout_constraintBottom_creator="0"
    app:layout_constraintHorizontal_bias="0.57"
    app:layout_constraintVertical_bias="0.6" />

<CheckBox
    android:text="13hours"
    android:layout_width="103dp"
    android:layout_height="34dp"
    tools:layout_editor_absoluteX="265dp"
    tools:layout_editor_absoluteY="339dp"
    android:id="@+id/chkBox13"
    app:layout_constraintLeft_toLeftOf="@+id/activity_amplitudination"
    android:layout_marginLeft="16dp"
    android:layout_marginStart="16dp"
    tools:layout_constraintLeft_creator="0"
    app:layout_constraintTop_toBottomOf="@+id/progressBar"
    android:layout_marginTop="8dp"
    tools:layout_constraintTop_creator="0"
    app:layout_constraintRight_toRightOf="@+id/activity_amplitudination"
    android:layout_marginRight="16dp"
    android:layout_marginEnd="16dp"
    tools:layout_constraintRight_creator="0"
    app:layout_constraintBottom_toTopOf="@+id/chkBox15"
    android:layout_marginBottom="8dp"
    tools:layout_constraintBottom_creator="0"
    app:layout_constraintHorizontal_bias="1.0"
    app:layout_constraintVertical_bias="0.58000004" />

<CheckBox
    android:text="15hours"
    android:layout_width="103dp"
    android:layout_height="32dp"
    tools:layout_editor_absoluteX="265dp"
    tools:layout_editor_absoluteY="383dp"
    android:id="@+id/chkBox15"
    app:layout_constraintLeft_toLeftOf="@+id/activity_amplitudination"
    android:layout_marginLeft="16dp"
    android:layout_marginStart="16dp"
    tools:layout_constraintLeft_creator="0"
    app:layout_constraintRight_toRightOf="@+id/activity_amplitudination"
    android:layout_marginRight="16dp"
    android:layout_marginEnd="16dp"
    tools:layout_constraintRight_creator="0"
    app:layout_constraintBottom_toBottomOf="@+id/activity_amplitudination"
    android:layout_marginBottom="96dp"
    tools:layout_constraintBottom_creator="0"
    app:layout_constraintHorizontal_bias="1.0" />

<TextView
    android:layout_width="123dp"
    android:layout_height="38dp"
    tools:layout_editor_absoluteX="245dp"
    tools:layout_editor_absoluteY="5dp"
    android:id="@+id/txtViewLoyaltyPointsAmplitudination"
    app:layout_constraintLeft_toLeftOf="@+id/activity_amplitudination"
    android:layout_marginLeft="16dp"
    android:layout_marginStart="16dp"
    tools:layout_constraintLeft_creator="0"
    app:layout_constraintTop_toTopOf="@+id/activity_amplitudination"
    android:layout_marginTop="16dp"
    tools:layout_constraintTop_creator="0"
    app:layout_constraintRight_toRightOf="@+id/activity_amplitudination"
    android:layout_marginRight="16dp"
    android:layout_marginEnd="16dp"
    tools:layout_constraintRight_creator="0"
    app:layout_constraintBottom_toTopOf="@+id/progressBar"
    android:layout_marginBottom="24dp"
    tools:layout_constraintBottom_creator="0"
    app:layout_constraintHorizontal_bias="1.0"
    app:layout_constraintVertical_bias="0.47000003" />

<Button
    android:text="Finish"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    tools:layout_editor_absoluteX="268dp"
    tools:layout_editor_absoluteY="447dp"
    android:id="@+id/btnFinishAmplitudination"
    app:layout_constraintLeft_toLeftOf="@+id/chkBox15"
    tools:layout_constraintLeft_creator="0"
    app:layout_constraintTop_toBottomOf="@+id/progressBar"
    android:layout_marginTop="8dp"
    tools:layout_constraintTop_creator="0"
    app:layout_constraintRight_toRightOf="@+id/activity_amplitudination"
    android:layout_marginRight="16dp"
    android:layout_marginEnd="16dp"
    tools:layout_constraintRight_creator="0"
    app:layout_constraintBottom_toBottomOf="@+id/activity_amplitudination"
    android:layout_marginBottom="16dp"
    tools:layout_constraintBottom_creator="0"
    app:layout_constraintHorizontal_bias="0.22"
    app:layout_constraintVertical_bias="1.0" />

主要类

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

    receiver = new TimerStatusReceiver();

}

@Override
protected void onPause() {
    super.onPause();
    LocalBroadcastManager.getInstance(this).unregisterReceiver(receiver);
}
@Override
protected void onResume() {
    super.onResume();
    mProgressBar.setProgress(100);
    txtShowTimer.setText("00:00:00");

    LocalBroadcastManager.getInstance(this).registerReceiver(receiver, new IntentFilter(CountdownTimerService.TIME_INFO));

}
public void startService(View view) {
    Intent intent = new Intent(this, CountdownTimerService.class);
    //intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    startService(intent);
}

public void stopService(View view) {
    Intent intent = new Intent(this, CountdownTimerService.class);
    //intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
    stopService(intent);
}

private class TimerStatusReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        if (intent != null && intent.getAction().equals(CountdownTimerService.TIME_INFO)) {
            if (intent.hasExtra("VALUE")) {

                txtShowTimer.setText(intent.getStringExtra("VALUE"));

                long millisInHour = intent.getLongExtra("LONG",0);
                long secondsInHour = millisInHour/1000;

                int progress = (int) (secondsInHour);
                mProgressBar.setProgress(progress);


                 if (secondsInHour == 0) {


                    mProgressBar.setProgress(100);

                }

            }
        }
    }
}

服务类

public class CountdownTimerService extends Service {

private CounterClass timer;

@Override
public void onCreate() {
    super.onCreate();
   preferences=getSharedPreferences("program",Context.MODE_PRIVATE);

}

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

公共类CounterClass扩展了CountDownTimer {

    public CounterClass(long millisInFuture, long countDownInterval) {
        super(millisInFuture, countDownInterval);
    }
    @Override
    public void onTick(long millisUntilFinished) {
         millis = millisUntilFinished;
        hms = String.format("%02d:%02d:%02d", TimeUnit.MILLISECONDS.toHours(millis),
                TimeUnit.MILLISECONDS.toMinutes(millis) - TimeUnit.HOURS.toMinutes(TimeUnit.MILLISECONDS.toHours(millis)),
                TimeUnit.MILLISECONDS.toSeconds(millis) - TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(millis)));


        Intent timerInfoIntent = new Intent(TIME_INFO);
        timerInfoIntent.putExtra("VALUE", hms);
        timerInfoIntent.putExtra("LONG", millis);

        LocalBroadcastManager.getInstance(CountdownTimerService.this).sendBroadcast(timerInfoIntent);


        setNotification();

    }
    @Override
    public void onFinish() {


             Intent timerInfoIntent = new Intent(TIME_INFO);
             timerInfoIntent.putExtra("VALUE", "00:00:00");
             LocalBroadcastManager.getInstance(CountdownTimerService.this).sendBroadcast(timerInfoIntent);
             stopForeground(true);
        if (millis > 1000 && millis < 3000) {
            showLoyaltyPoints();
        }


        int storedPreference = preferences.getInt("intMillisDrived", 0);
        amplitudinationProgramFinished(storedPreference);



    }
}


@Override
public int onStartCommand(Intent intent, int flags, int startId) {

    timer = new CounterClass(10000, 1000);
    timer.start();


    return super.onStartCommand(intent, flags, startId);
}

enter code here

0 个答案:

没有答案