2017-08-29 69 views
3

我正在追蹤訂單屏幕上。在用戶下訂單後,Admin將訂單狀態更改爲接受待定。我想在跟蹤訂單屏幕上實時顯示訂單狀態。每當管理員在後端進行更改時,都應該更改狀態。每30秒更新一次訂單狀態

這是點擊追蹤按鈕的初始屏幕。 http://imgur.com/a/5NCFH

當用戶選擇下拉菜單時,將顯示基於用戶的所有訂單。 http://imgur.com/a/PiKf1

以下是選擇特定訂單後的訂單屏幕 http://imgur.com/a/AdxLO。根據該圖片,一旦在後端將其更改爲發貨,我實時想要點酒吧訂購發貨。

這是我的跟蹤訂單活動。

public void onCreate(Bundle savedInstanceState) 
{ 
    super.onCreate(savedInstanceState); 

    try 
    { 
     getActivity().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN); 
     setHasOptionsMenu(true); 
    } 
    catch(Exception ex) { 
     ex.getMessage(); 
    } 
} 

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) 
{ 
    final View view = inflater.inflate(R.layout.fragment_track_order, container, false); 
    try 
    { 
     mView = view; 

     getOrders(); // It gives me all the orders on clicking spinner 

    } 
    catch(Exception ex) 
    { 
     GSLogger.e(ex); 
     showToast(getString(R.string.error_loading_fragments)); 
    } 

    return view; 

} 


private void init_AppContents(final ArrayList<TrackOrderModel> ordersList) 
{ 
    try 
    { 

     final ArrayList<String> order_arr = new ArrayList<>(); 
     for(TrackOrderModel trackOrderModel : ordersList) 
     { 
      order_arr.add(trackOrderModel.getRestaurant_name()+" "+trackOrderModel.getData_time()); 
     } 

     final SpinnerAdapter area_arrayAdapter = new SpinnerAdapter(getActivity(), 
       android.R.layout.simple_spinner_dropdown_item, order_arr); 
     order_spinner.setAdapter(area_arrayAdapter); 
     order_spinner.setSelection(0); 

     order_spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { 
      @Override 
      public void onItemSelected(AdapterView<?> arg0, View arg1, int position, long arg3) 
      { 
       order_id = ordersList.get(position).getOrder_id(); 
       tran_id = ordersList.get(position).getOrder_id(); 


       if(!order_id.equals("0")) 

        getOrderStatus(order_id); // Based on the order id, it displays particular order status. Here I want it to change every 30 seconds. 
       else 
       { 
        order_received_circle.setImageResource(R.mipmap.circle_grey); 

        order_ready_progress.setBackgroundColor(ContextCompat.getColor(getActivity(), R.color.light_grey_2)); 
        order_ready_circle.setImageResource(R.mipmap.circle_grey); 

        order_deliver_progress.setBackgroundColor(ContextCompat.getColor(getActivity(), R.color.light_grey_2)); 
        order_deliver_circle.setImageResource(R.mipmap.circle_grey); 
       } 

      } 

      @Override 
      public void onNothingSelected(AdapterView<?> arg0) { 
      } 
     }); 

    } 
    catch(Exception ex) 
    { 
     GSLogger.e(ex); 
     showToast(getString(R.string.error_init_content)); 
    } 
} 

幫助將不勝感激。

+0

爲什麼當管理員將訂單狀態更改爲從待處理接受時,您在收到通知時使用本地廣播刷新您的列表時,不會要求網絡端發出無聲通知。 –

+0

你目前使用什麼解決方案? –

+0

@UmarHussain我是android新手。如果你可以給我任何解決方案,那很好。我正在考慮計時器任務。我不知道在哪裏放 – Jacky

回答

1

您可以使用廣播接收器來檢查訂單狀態,並顯示給用戶一個通知,當訂單狀態的變化,另一種方法是使用一個運行的線程或計時器()發送一個請求,並檢查訂單狀態每30秒

+0

我使用了Runnable線程。它工作正常 – Jacky

+0

使用定時器每30秒檢查一次是一個不好的解決方案。如果您已經有能力在後端工作,那麼爲應用程序構建通知。其他人解釋瞭如何與firebase。 '事件觸發'刷新是更好的設計,更好的表現。無需使用額外的線程並保持設備繁忙。 如果你想使用計時器版本,我還會推薦使用'Quartz',它基本上是一個可編程的Cronjob。 – Nico

1

您可以使用Firebase Cloud Messaging,使用計時器輪詢服務器以查找結果並不是一種好方法,因爲它不是一種節能的方式。 使用FCM,當後端服務器更新數據並觸發有關數據的通知時,您將能夠接收通知(您可以用於實際用戶通知或僅接收有效內容)。 這是一個有效的方式,因爲它由Google管理,具有更好的實施

閱讀本文檔。 https://firebase.google.com/docs/cloud-messaging/

+0

會經歷它。 – Jacky