我的Ubuntu Server 16.04 LTS正在运行,并且在一天结束时我想从Android设备关闭我的服务器。
我创建了一个php
文件(API.php
),如下所示。
<?php
shell_exec("shutdown -P now");
?>
在Android应用中有一个按钮,OnClickListener
如下所示:
String serverURL = "http://hostIP/API.php";
new LongOperation().execute(serverURL);
LongOperation
类如下:
private class LongOperation extends AsyncTask<String, Void, Void>{
private final HttpClient client = new DefaultHttpClient();
private String content;
private String error = null;
private ProgressDialog progressDialog = new ProgressDialog(MainActivity.this);
@Override
protected void onPreExecute() {
progressDialog.setMessage("Shutting down...");
progressDialog.show();
}
@Override
protected Void doInBackground(String... params) {
try {
HttpGet httpGet = new HttpGet(params[0]);
ResponseHandler<String> responseHandler = new BasicResponseHandler();
content = client.execute(httpGet, responseHandler);
} catch (IOException e){
error = e.getMessage();
cancel(true);
}
return null;
}
@Override
protected void onPostExecute(Void unused) {
progressDialog.dismiss();
Toast.makeText(MainActivity.this, "finished", Toast.LENGTH_SHORT).show();
}
}
此代码在我的localhost上成功运行,但是当我把服务器的ip重新运行时,没有任何反应。
如何成功关闭我的ubuntu服务器?