Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
//License from project: Open Source License 

import java.io.BufferedReader;
import java.io.IOException;

import java.io.InputStreamReader;
import java.util.StringTokenizer;
import android.util.Log;

public class Main {
    public static void killProcess(String packageName) {
        String processId = "";
        try {
            Runtime r = Runtime.getRuntime();
            Process p = r.exec("ps");
            BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream()));
            String inline;
            while ((inline = br.readLine()) != null) {
                if (packageName != null) {
                    if (inline.contains(packageName)) {
                        break;
                    }
                } else {
                    Log.e("PvTorrent_proc", "packageName is null");
                }

            }
            br.close();
            Log.i("PvTorrent_proc", "inline" + inline);
            if (inline != null) {

                StringTokenizer processInfoTokenizer = new StringTokenizer(inline);
                int count = 0;
                while (processInfoTokenizer.hasMoreTokens()) {
                    count++;
                    processId = processInfoTokenizer.nextToken();
                    if (count == 2) {
                        break;
                    }
                }
                Log.i("PvTorrent_proc", "kill process : " + processId);
                r.exec("kill -9 " + processId);
            }
        } catch (IOException ex) {
            Log.e("PvTorrent_proc", "kill" + ex.getStackTrace());
        }
    }
}