Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
535 views
in Technique[技术] by (71.8m points)

process - How can I kill processes in Android?

I have this code below:

package com.example.killall;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
//import android.widget.TextView;
import android.app.ActivityManager;

public class MainKill extends Activity {

private Button BprocessesKill ;
//private TextView processesKill;

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

    final ActivityManager am=(ActivityManager)     getSystemService("SYSTEM_ACTIVITY");

    BprocessesKill=(Button) this.findViewById(R.id.BkillProcesses);
    //processesKill=(TextView) this.findViewById(R.id.killProcesses);



    BprocessesKill.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View arg0) {
            am.killBackgroundProcesses(getPackageName());

        }
    });

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main_kill, menu);
    return true;
}

}

All I want to do is simply to press the button and kill all background processes.. the problem I have with this code is that when I am pressing the button it shows me the message : Unfortunately KillAll(that's my app's name) has stopped. What should I change?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
  1. The message you're getting indicates that your app has crashed. You need to look at its LogCat to find out why. This is documented in the Android Developer Guide.
  2. Please state your reason for killing all background processes, because I can't think of any appropriate reason to do this. People persist in claiming that "task killers" or "app killers" improve performance, but this attitude ignores the real problem: poorly-written apps. As long as we continue to claim that task killers help, users will continue to use apps that leave unnecessary services, etc. running. Forcing users to use task killers is like dealing with a termite problem by killing one termite at a time as you see them. The real answer is to exterminate all the termites.

In short, anyone who shows you how to kill all background processes is doing you a disservice and the Android community a disservice.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...