[안드로이드 프로그래밍]램 정리 강좌 및 소스 : 킬 백그라운드 프로세스

프로그래밍/Android 2016.05.26 댓글 Plorence

[안드로이드 프로그래밍]램 정리 강좌 


램 정리 강좌 및 소스입니다. 

이번강좌는 버튼클릭으로 이벤트가 발생하여 램이 정리되는것입니다.


중요한부분은 빨간색 으로 표시하겠습니다.



Activity_main.xml

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="plorence.user.test.MainActivity">


<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="램정리"
android:onClick="clear"
android:id="@+id/clear" />
</LinearLayout>

MainActivity.class

package plorence.user.test;

import android.app.ActivityManager;
import android.os.Bundle;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Toast;

import java.util.List;

public class MainActivity extends AppCompatActivity {

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

public void clear(View v) {
ActivityManager activityManger = (ActivityManager) this
.getSystemService(ACTIVITY_SERVICE);
List<ActivityManager.RunningAppProcessInfo> list = activityManger
.getRunningAppProcesses();
if (list != null)
for (int i = 0; i < list.size(); i++) {
ActivityManager.RunningAppProcessInfo apinfo = list.get(i);

String[] pkgList = apinfo.pkgList;
if (apinfo.importance > ActivityManager.RunningAppProcessInfo.IMPORTANCE_SERVICE) {
for (int j = 0; j < pkgList.length; j++) {
activityManger.killBackgroundProcesses(pkgList[j]);
}
}
}
onCreate(null);
new Handler().postDelayed(new Runnable() {
public void run() {
Toast.makeText(MainActivity.this, "종료",
Toast.LENGTH_SHORT).show();
System.exit(0);
}
}, 5000);
}
}

저위치에


public void clear(View v) {
ActivityManager activityManger = (ActivityManager) this
.getSystemService(ACTIVITY_SERVICE);
List<ActivityManager.RunningAppProcessInfo> list = activityManger
.getRunningAppProcesses();
if (list != null)
for (int i = 0; i < list.size(); i++) {
ActivityManager.RunningAppProcessInfo apinfo = list.get(i);

String[] pkgList = apinfo.pkgList;
if (apinfo.importance > ActivityManager.RunningAppProcessInfo.IMPORTANCE_SERVICE) {
for (int j = 0; j < pkgList.length; j++) {
activityManger.killBackgroundProcesses(pkgList[j]);
}
}
}
onCreate(null);
new Handler().postDelayed(new Runnable() {
public void run() {
Toast.makeText(MainActivity.this, "종료",
Toast.LENGTH_SHORT).show();
System.exit(0);
}
}, 5000);
}
추가해주세요.



manifests.xml

<uses-permission android:name="android.permission.KILL_BACKGROUND_PROCESSES" />



댓글