我正在使用Arduino和一个窗口传感器,因此我需要询问窗口是否关闭发送通知,15分钟后再次询问一小时。我的代码有问题,因为没有执行onPostExecute,因此不显示消息。希望你们可以帮助我,因为我的tesis。
代码:
public class ServicePush extends Service{
private String windowstate, url;
private AsyncTask task;
public ServicePush() {
}
@Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
return null;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
// TODO Auto-generated method stub
SharedPreferences prefs = getSharedPreferences("Configuraciones", Context.MODE_PRIVATE);
url = prefs.getString("IPArduinoYun", "10.40.4.3");
url = "http://" + url + "/arduino/216";
System.out.println("before first try");
try {
System.out.println("first try");
GetArduinoData();
Thread.sleep(15000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("before while");
while (windowstate != "Open"){
try {
System.out.println("While");
GetArduinoData();
Thread.sleep(15000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println("End");
return START_STICKY;
}
public void GetArduinoData(){
task = new ReadJSON().execute(url);
}
private class ReadJSON extends AsyncTask<String, Void, String> {
protected String doInBackground(String... urls) {
System.out.println("doInBackground");
return readJSONFeed(urls[0]);
}
protected void onPostExecute(String result) {
try {
JSONObject jsonObject = new JSONObject(result);
windowstate = "";
if (jsonObject.getString("Window").equals("0")) {
windowstate = "Close";
SendNotification();
}
if (jsonObject.getString("Window").equals("1")) {
windowstate = "Open";
}
Toast.makeText(getBaseContext(), windowstate, Toast.LENGTH_SHORT).show();
System.out.println("onPostExecute");
task.cancel(true);
} catch (Exception e) {
Log.d("ReadJSON", e.getLocalizedMessage());
}
}
}
public String readJSONFeed(String URL) {
StringBuilder stringBuilder = new StringBuilder();
HttpClient httpClient = new DefaultHttpClient();
HttpGet httpGet = new HttpGet(URL);
try {
HttpResponse response = httpClient.execute(httpGet);
StatusLine statusLine = response.getStatusLine();
int statusCode = statusLine.getStatusCode();
if (statusCode == 200) {
HttpEntity entity = response.getEntity();
InputStream inputStream = entity.getContent();
BufferedReader reader = new BufferedReader(
new InputStreamReader(inputStream));
String line;
while ((line = reader.readLine()) != null) {
stringBuilder.append(line);
}
inputStream.close();
} else {
Log.d("JSON", "Error downloading");
}
} catch (Exception e) {
Log.d("readJSONFeed", e.getLocalizedMessage());
}
return stringBuilder.toString();
}
@Override
public void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
Toast.makeText(this, "Service destroyed", Toast.LENGTH_SHORT).show();
}
public void SendNotification(){
Intent i = new Intent(this, MainActivity.class);
PendingIntent pi = PendingIntent.getActivity(this, 0, i, 0);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_warning_black_24dp)
.setContentTitle("Recordatorio")
.setContentText("Debe abrir las ventanas para poder ventilar");
mBuilder.setContentIntent(pi);
mBuilder.setDefaults(Notification.DEFAULT_SOUND);
mBuilder.setAutoCancel(true)
.setVibrate(new long[]{1000, 1000, 1000, 1000, 1000});
NotificationManager mNotificationManager = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(0, mBuilder.build());
}
答案 0 :(得分:0)
所以..最后我找到了解决方案,我的教授在asynctask上使用sleep,并在onPostExecute中调用的新函数中使用了一个标志,创建了一个新的Asynctask.Hope这可以帮助需要它的人
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
// TODO Auto-generated method stub
SharedPreferences prefs = getSharedPreferences("Configuraciones", Context.MODE_PRIVATE);
url = prefs.getString("IPArduinoYun", "10.40.4.3");
url = "http://" + url + "/arduino/216";
GetArduinoData();
return START_STICKY;
}
public void GetArduinoData(){
new ReadJSON().execute(url);
}
public void GetDataReady(){
if (windowstate != "Open"){
//do something
} else {
//do something
}
}
private class ReadJSON extends AsyncTask<String, Void, String> {
protected String doInBackground(String... urls) {
try {
Thread.sleep(10000);
} catch (InterruptedException e) {
e.printStackTrace();
}
return readJSONFeed(urls[0]);
}
protected void onPostExecute(String result) {
try {
JSONObject jsonObject = new JSONObject(result);
windowstate = "";
if (jsonObject.getString("Window").equals("0")) {
windowstate = "Closed";
SendNotification();
}
if (jsonObject.getString("Window").equals("1")) {
windowstate = "Open";
}
} catch (Exception e) {
Log.d("ReadJSON", e.getLocalizedMessage());
}
GetDataReady();
}
}