我想出了两种方法来解决这个问题,但它们达不到预期。
我使用“进程”执行“ ps -ef”
我可以通过这种方法获取所有行,并且可以通过运行来过滤它们 命令。但是如果我有许多相同的命令过程。这是行不通的。
我使用JNA来获取PID
Field field = null;
Integer pid = -1;
try {
Class clazz = Class.forName("java.lang.UNIXProcess");
field = clazz.getDeclaredField("pid");
field.setAccessible(true);
pid = (Integer) field.get(process);
} catch (Throwable e) {
e.printStackTrace();
}
这种方式只能获取运行窗口的PID。这不是真正的进程PID。
我该怎么办?
谢谢!
答案 0 :(得分:1)
Java 9引入了一些“不错”的变化,其中之一是包含Process
的本机PID-有关更多详细信息,请参见Process#pid
import java.io.IOException;
import java.io.InputStream;
public class Test {
public static void main(String[] args) throws IOException, InterruptedException {
ProcessBuilder pb = new ProcessBuilder("/Applications/Xcode.app/Contents/MacOS/Xcode");
pb.redirectErrorStream(true);
Process p = pb.start();
// Yes, I'm a bad developer, but I just want to demonstrate
// the use of the PID method :/
new Thread(new Consumer(p.getInputStream())).start();
System.out.println("PID = " + p.pid());
p.waitFor();
System.out.println("Exit with " + p.exitValue());
}
public static class Consumer implements Runnable {
private InputStream is;
public Consumer(InputStream is) {
this.is = is;
}
@Override
public void run() {
try {
int value = -1;
while ((value = is.read()) != -1) {
// I'm ignoring it for brevity
}
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
}
您还可以通过ProcessHandle
方法为Process
获取对 import im.delight.android.webview.AdvancedWebView;
public class MainActivity extends AppCompatActivity implements AdvancedWebView.Listener {
String ShowOrHideWebViewInitialUse = "show";
private AdvancedWebView mWebView;
private ProgressBar spinner;
private Boolean exit = false;
String currentUrl;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (!amIconnected()){
Toast.makeText(getApplicationContext(), "No Internet!", Toast.LENGTH_SHORT).show();
Intent intent= new Intent(this, Main2Activity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(intent);
}else{
mWebView =(AdvancedWebView) findViewById(R.id.webView);
mWebView.setWebChromeClient(new WebChromeClient() {});
mWebView.setListener(this, this);
spinner = (ProgressBar)findViewById(R.id.progressBar1);
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.getSettings().setDomStorageEnabled(true);
mWebView.clearCache(true);
mWebView.clearHistory();
mWebView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
mWebView.loadUrl("https://www.example.com");
}
}
@SuppressLint("NewApi")
@Override
protected void onResume() {
super.onResume();
}
@Override
protected void onPause() {
super.onPause();
}
@Override
protected void onDestroy() {
super.onDestroy();
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
super.onActivityResult(requestCode, resultCode, intent);
mWebView.onActivityResult(requestCode, resultCode, intent);
}
public void onBackPressed() {
if (exit) {
finish(); // finish activity
}else {
Toast.makeText(this, "Press Back again to Exit.",
Toast.LENGTH_SHORT).show();
exit = true;
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
exit = false;
}
}, 3* 1000);
}
}
@Override
public void onPageStarted(String url, Bitmap favicon) {
// show loader while page is loading
if (ShowOrHideWebViewInitialUse.equals("show")) {
mWebView.setVisibility(mWebView.VISIBLE);
spinner.setVisibility(View.VISIBLE);
ShowOrHideWebViewInitialUse = "hide";
}
currentUrl = url;
Log.i("OnpageStarted", "url is " + url);
// disable click while loading
mWebView.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
return true;
}
});
}
@Override
public void onPageFinished(String url) {
// hide loader after page is loaded
if (ShowOrHideWebViewInitialUse.equals("hide")){
spinner.setVisibility(View.GONE);
mWebView.setVisibility(mWebView.VISIBLE);
ShowOrHideWebViewInitialUse = "show";
currentUrl = url;
}
// enable touch back once page completely loaded
mWebView.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
return false;
}
});
currentUrl = url;
}
@Override
public void onPageError(int errorCode, String description, String failingUrl) {
Toast.makeText(getApplicationContext(), "Something went wrong!", Toast.LENGTH_SHORT).show();
Intent intent= new Intent(MainActivity.this,Main2Activity.class);
startActivity(intent);
}
@Override
public void onDownloadRequested(String url, String suggestedFilename, String mimeType, long contentLength, String contentDisposition, String userAgent) { }
@Override
public void onExternalPageRequest(String url) { }
private boolean amIconnected(){
ConnectivityManager connectivityManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
return activeNetworkInfo != null && activeNetworkInfo.isAvailable() && activeNetworkInfo.isConnected();
}
的{{3}}的引用,