【发布时间】:2018-09-18 03:21:36
【问题描述】:
首先抱歉,如果之前已经提出过此类问题。
我想制作一个应用程序,它可以创建一个GET request 或简单的Open A URL 来告诉服务器应用程序现在已关闭,不需要输出,只需加载 URL。
我已经尝试过的:
我使用了
Volley,但不幸的是在这种情况下它不是一个解决方案。我也尝试过让
AsyncTask成为朋友,但它很奇怪,它也不起作用
只有一行简单的代码行得通:
URL url = new URL("URL HERE");
InputStream stream = url.openStream();
但是导致NetworkException。我只想在应用关闭后从服务中执行一个 URL。
示例代码将不胜感激
提前致谢
更新部分
public class Service extends android.app.Service {
public int isExecuted = 0;
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onCreate() {
super.onCreate();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.e("rrLOG", "Service Started");
checkClosing();
return Service.START_NOT_STICKY;
}
@Override
public void onTaskRemoved(Intent rootIntent) {
super.onTaskRemoved(rootIntent);
Log.e("rrLOG", "Service Stopped");
updateServer(UPDATE_OFFLINE);
stopSelf();
}
@Override
public void onDestroy() {
super.onDestroy();
Log.e("rrLOG", "Service Destroyed");
updateServer(UPDATE_OFFLINE);
stopSelf();
}
public void checkClosing(){
new Timer().scheduleAtFixedRate(new TimerTask() {
@Override
public void run() {
if (Helper.isAppRunning(Service.this, getPackageName())) {
Log.i("rrLOG", "App Is Running");
} else {
Log.i("rrLOG", "App Is Not Running");
updateServer(UPDATE_OFFLINE);
stopSelf();
}
}
}, 0, 500);//put here time 1000 milliseconds=1 second
}
static class RequestTask extends AsyncTask<String, String, String> {
@Override
protected String doInBackground(String... uri) {
HttpClient httpclient = new DefaultHttpClient();
HttpResponse response;
String responseString = null;
try {
response = httpclient.execute(new HttpGet(uri[0]));
StatusLine statusLine = response.getStatusLine();
if(statusLine.getStatusCode() == HttpStatus.SC_OK){
ByteArrayOutputStream out = new ByteArrayOutputStream();
response.getEntity().writeTo(out);
responseString = out.toString();
out.close();
} else{
//Closes the connection.
response.getEntity().getContent().close();
throw new IOException(statusLine.getReasonPhrase());
}
} catch (ClientProtocolException e) {
//TODO Handle problems..
} catch (IOException e) {
//TODO Handle problems..
}
return responseString;
}
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
//Do anything with response..
}
}
public void updateServer(final String urlJsonArry) {
Log.i("rrLOG", urlJsonArry);
new RequestTask().execute(urlJsonArry);
}
}
UPDATE2 部分
public class Service extends android.app.Service {
public int isExecuted = 0;
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onCreate() {
super.onCreate();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.e("rrLOG", "Service Started");
checkClosing();
return Service.START_NOT_STICKY;
}
@Override
public void onTaskRemoved(Intent rootIntent) {
//super.onTaskRemoved(rootIntent);
Log.e("rrLOG", "Service Stopped");
updateServer(UPDATE_OFFLINE);
}
@Override
public void onDestroy() {
//super.onDestroy();
Log.e("rrLOG", "Service Destroyed");
updateServer(UPDATE_OFFLINE);
}
public void checkClosing(){
new Timer().scheduleAtFixedRate(new TimerTask() {
@Override
public void run() {
if (Helper.isAppRunning(Service.this, getPackageName())) {
Log.i("rrLOG", "App Is Running");
} else {
Log.i("rrLOG", "App Is Not Running");
updateServer(UPDATE_OFFLINE);
}
}
}, 0, 500);//put here time 1000 milliseconds=1 second
}
public class NetworkTask extends AsyncTask<Void,Void,String> {
@Override
protected String doInBackground(Void... voids) {
Log.i("rrLOG", String.valueOf(isExecuted));
URL url = null; //get URL from your uri object
try {
url = new URL(UPDATE_OFFLINE);
} catch (MalformedURLException e) {
e.printStackTrace();
}
try {
InputStream stream = url.openStream();
} catch (IOException e) {
e.printStackTrace();
}
Log.i("rrLOG", String.valueOf(url));
return String.valueOf(url);
}
@Override
protected void onPostExecute(String result) {
Log.i("rrLOG", "onPostExecute");
super.onPostExecute(result);
stopSelf();
}
}
public void updateServer(String urlJsonArry) {
Log.i("rrLOG", urlJsonArry);
if(isExecuted == 0) {
new NetworkTask().execute();
isExecuted = 1;
}
}
}
【问题讨论】:
-
尝试使用 Intentservice 或 JobScheduler
-
任何与此建议相关的示例代码?
-
您可以发布您的服务代码
-
是的,当然,检查更新部分
标签: android service android-asynctask android-volley