0

원격 데이터베이스의 모든 업데이트에 대해 사용자에게 알림을 보내려면 어떻게해야합니까? 기존 행이 추가되거나 업데이트 된 행이 있으면 사용자에게 알리고 싶습니다. 사용자 지정 ListView를 사용하고 BigPictureStyle 알림을 표시하려고합니다.안드로이드에 MYSQL 업데이트 알림 받기

public class EventServices extends Service { 
int numMessages = 0; 
String rowCount = null; 
int oldRowCount = 0; 
HashMap<String,String> hashMap = new HashMap<>(); 

String rowCountLink = "http://192.168.0.101/getdbrowcount.php"; 

public EventServices() { 

} 

@Override 
public IBinder onBind(Intent intent) { 
    throw new UnsupportedOperationException("Not yet implemented"); 
} 

@Override 
public void onCreate() { 
    Toast.makeText(this, "Service was Created", Toast.LENGTH_LONG).show(); 
    oldRowCount = Integer.parseInt(getRowCount()); 

} 



@Override 
public void onStart(Intent intent, int startId) { 

    Log.d("Parse","Old row count: "+ oldRowCount); 
    Log.d("Parse","New row count: "+Integer.parseInt(getRowCount())); 


    if(Integer.parseInt(getRowCount())> oldRowCount) { 


     String newEventLink = "http://192.168.0.101/getnewevent.php"; 

     new MyTask(newEventLink).execute(); 
     // NewEventDetails newED = new NewEventDetails(); 
     // NewEventHashMap newEventHashMap = new NewEventHashMap(); 


     String name = hashMap.get("name"); 
     String description = hashMap.get("description"); 
     String image = hashMap.get("image"); 

     Log.d("Parse","New event name: "+name); 
     Log.d("Parse","New event description: "+description); 
     Log.d("Parse","New event image: "+image); 



     Toast.makeText(this, "Service Started", Toast.LENGTH_LONG).show(); 
     Intent resultIntent = new Intent(this, ItemDetailActivity.class); 
     resultIntent.putExtra("description",description); 
     PendingIntent resultPendingIntent = PendingIntent.getActivity(this, 0, 
     resultIntent, PendingIntent.FLAG_UPDATE_CURRENT); 
     NotificationCompat.Builder mNotifyBuilder; 
     NotificationManager mNotificationManager; 
     mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); 
     // Sets an ID for the notification, so it can be updated 
     int notifyID = 9001; 

     mNotifyBuilder = new NotificationCompat.Builder(this) 
       .setContentTitle(name) 
       .setContentText(description) 
       .setSmallIcon(R.drawable.icon); 
     // Set pending intent 


    // startActivity(resultIntent); 
     // mNotifyBuilder.setContentIntent() 
     mNotifyBuilder.setContentIntent(resultPendingIntent); 
     // Set Vibrate, Sound and Light 
     int defaults = 0; 
     defaults = defaults | Notification.DEFAULT_LIGHTS; 
     defaults = defaults | Notification.DEFAULT_VIBRATE; 
     defaults = defaults | Notification.DEFAULT_SOUND; 
     mNotifyBuilder.setDefaults(defaults); 
     // Set the content for Notification 
     // mNotifyBuilder.setContentText(intent.getStringExtra("intntdata")); 
     // Set autocancel 
     mNotifyBuilder.setAutoCancel(true); 
     // Post a notification 
     mNotificationManager.notify(notifyID, mNotifyBuilder.build()); 
     oldRowCount++; 

    }else{ 

    } 
} 

@Override 
public void onDestroy() { 
    Toast.makeText(this, "Service Destroyed", Toast.LENGTH_LONG).show(); 

} 


private String getRowCount(){ 

    RowCountDownloader rowCountDownloader = new RowCountDownloader(rowCountLink); 

    try { 
     rowCount = rowCountDownloader.execute().get(); 
    } catch (InterruptedException e) { 
     e.printStackTrace(); 
    } catch (ExecutionException e) { 
     e.printStackTrace(); 
    } 
    // Log.d("Parse","Rowcount in services: "+rowCount); 
    return rowCount; 
} 


class MyTask extends AsyncTask<Void,Integer,String> { 

    String newEventLink; 
    // HashMap<String,String> hashMap = new HashMap<>(); 




    public MyTask(String newEventLink){ 
     this.newEventLink = newEventLink; 

    } 

    @Override 
    protected String doInBackground(Void... params) { 

     String data = downloadData(); 

     return data; 
    } 


    @Override 
    protected void onProgressUpdate(Integer... values) { 


    } 

    private String downloadData() { 
     String line = null; 
     InputStream inputStream =null; 
     try{ 
      URL url = new URL(newEventLink); 
      HttpURLConnection con = (HttpURLConnection) url.openConnection(); 
      inputStream = new BufferedInputStream(con.getInputStream()); 
      BufferedReader br = new BufferedReader(new InputStreamReader(inputStream)); 
      StringBuffer sb = new StringBuffer(); 

      if (br != null) { 
       while ((line = br.readLine()) != null) { 
        sb.append(line + "\n"); 
       } 
      } else { 
       return null; 
      } 
      try{ 
       JSONObject jo = new JSONObject(sb.toString()); 
       JSONArray ja = jo.getJSONArray("result"); 
       JSONObject obj = ja.getJSONObject(0); 

       hashMap.put("name",obj.getString("name")); 
       hashMap.put("description",obj.getString("description")); 
       hashMap.put("image",obj.getString("image")); 


      }catch (JSONException e){ 
       e.printStackTrace(); 
      } 
      return null; 

     }catch(MalformedURLException e){ 
      e.printStackTrace(); 
     }catch (IOException e){ 
      e.printStackTrace(); 
     }finally { 
      if(inputStream!=null){ 
       try{ 
        inputStream.close(); 
       }catch (IOException e){ 
        e.printStackTrace(); 
       } 
      } 
     } 
     return null; 
     } 
    } 





    } 

새로운 서비스가 생성 될 때마다 전체 (테이블 COUNT (*))의 행의 수와 그 변수에 저장하고 PHP 스크립트에서 업데이트 된 행 수에 대한 다음 시간 서비스 확인을 인출 한 후 비교 기존 행 수 새로운 행 수가 기존 테이블보다 크면 데이터베이스에서 데이터를 가져옵니다 (SELECT * 테이블에서 id = (SELECT COUNT (*) FROM 테이블)). 새 행만 반환 한 다음 알림을 표시합니다.

내 문제는 원격 데이터베이스에서 새 데이터를 보내기 전에 알림을 표시하고 있다는 것입니다. 서버에서 이미지를 다운로드하기 때문일 수 있습니다. 그렇다면이 문제를 해결하는 방법은 무엇입니까?

답변

0

많은 연구 끝에이 문제를 해결했습니다. doInBackground() 내에 데이터를 다운로드하기 위해 모든 코드를 넣은 다음 onPostExecute에 결과를 전달하여 Notification에 표시했습니다.