2016-11-28 48 views
-3

我正在与Arduino和一个窗口传感器,所以我需要问,如果窗口关闭发送通知,并在15分钟后再次问一个小时。我的代码有问题,因为onPostExecute未执行,因此消息未显示。希望你们能帮助我,因为是为了我的睾丸。Asynctask与服务

代码:

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

感谢您的答复普拉温,在onPostExecuted代码不显示...这里输出: I/System.out的:虽然 I/System.out的:doInBackground I/System.out的:虽然 I/System.out:doInBackground I/System.out:While I/System.out:doInBackground –

+0

你为什么取消'onPostExecute()'中的任务?你可以尝试评论它吗? –

回答

0

So..finally我发现我的教授的解决方案使用上的AsyncTask的睡眠和使用新功能的标志是所谓的onPostExecute创建一个新的AsyncTask希望这可以帮助需要它的人。

@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(); 
    } 
}