-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());
}
什麼是錯誤? –
感謝您的答覆普拉溫,在onPostExecuted代碼不顯示...這裏輸出: I/System.out的:雖然 I/System.out的:doInBackground I/System.out的:雖然 I/System.out:doInBackground I/System.out:While I/System.out:doInBackground –
你爲什麼取消'onPostExecute()'中的任務?你可以嘗試評論它嗎? –