Android - 使用广播接收器重新加载 activity

Android - Use Broadcast Receiver to reload activity

我正在开发一个 android 应用程序,我有一个 activity 可以显示餐厅中的所有 table。在这个 activity 中,有一种方法可以根据客户是否在 table.[=14] 更改 table 颜色(table 只是一个按钮) =]

在客户的屏幕上,当他们 select 他们的 table 时,我的 MYQSL 数据库已更新,用户现在已分配给 table。

现在,当我去打开地图屏幕时(注意:这是应用程序上的服务员使用的,客户无法访问),table将是不同的颜色(它会检查table 数据库中的状态,如果分配了 table 则它是不同的颜色)。

但是,获取更新地图的唯一方法是重新加载地图 activity。我希望地图 activity 使用 BroadcastReceiver() 自动更新,但我正在努力寻找有关如何实现它的教程。当客户点击一个 table 将自己分配给它时,我希望发送广播,自动重新加载地图 activity... 这可能吗?如果是这样,我该如何实施?

编辑:服务员和顾客在不同的设备上使用相同的应用程序

ChooseTable activity 每个 table 都有一个单击侦听器,这会导致调用以下方法:

private void assignUserToTable(final int table, final String userid) {

        String url = "hidden";
        StringRequest stringRequest = new StringRequest(Request.Method.POST, url, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                if (response.trim().equals("success")) {
                    Toast.makeText(ChooseTable.this, "Welcome!", Toast.LENGTH_SHORT).show();
                } else {
                    Toast.makeText(ChooseTable.this, "Oops, something went wrong!", Toast.LENGTH_LONG).show();
                }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                Toast.makeText(ChooseTable.this, "Error! " + error.toString(), Toast.LENGTH_LONG).show();
            }
        }) {
            @Override
            protected Map<String, String> getParams() throws AuthFailureError {

                Map<String, String> params = new HashMap<>();
                params.put("table_id", String.valueOf(table));
                params.put("user", userid);
                return params;
            }
        };
        MySingleton.getInstance(this).addToRequestQueue(stringRequest);

    }

服务员地图视图看起来有以下方法检查正在加载的 activity 上的 table 状态:

private void checkTables(){

        String url = "hidden";
        StringRequest stringRequest = new StringRequest(Request.Method.POST, url, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                try{
                    JSONObject jsonObject = new JSONObject(response);
                    String success = jsonObject.getString("success");
                    JSONArray jsonArray = jsonObject.getJSONArray("toggles");

                    if(success.equals("1")){

                        for(int i=0; i<jsonArray.length(); i++){
                            JSONObject object = jsonArray.getJSONObject(i);
                            String tableid = object.getString("tableid");
                            if(Tbl1.getTag().equals(tableid)){
                                Tbl1.setBackgroundColor(ContextCompat.getColor(WaitingStaffMapScreen.this, R.color.tableReq));
                            } else if(Tbl2.getTag().equals(tableid)){
                                Tbl2.setBackgroundColor(ContextCompat.getColor(WaitingStaffMapScreen.this, R.color.tableReq));
                            } else if(Tbl3.getTag().equals(tableid)){
                                Tbl3.setBackgroundColor(ContextCompat.getColor(WaitingStaffMapScreen.this, R.color.tableReq));
                            } else if(Tbl4.getTag().equals(tableid)){
                                Tbl4.setBackgroundColor(ContextCompat.getColor(WaitingStaffMapScreen.this, R.color.tableReq));
                            } else if(Tbl5.getTag().equals(tableid)){
                                Tbl5.setBackgroundColor(ContextCompat.getColor(WaitingStaffMapScreen.this, R.color.tableReq));
                            } else if(Tbl6.getTag().equals(tableid)){
                                Tbl6.setBackgroundColor(ContextCompat.getColor(WaitingStaffMapScreen.this, R.color.tableReq));
                            } else if(Tbl7.getTag().equals(tableid)){
                                Tbl7.setBackgroundColor(ContextCompat.getColor(WaitingStaffMapScreen.this, R.color.tableReq));
                            } else if(Tbl8.getTag().equals(tableid)){
                                Tbl8.setBackgroundColor(ContextCompat.getColor(WaitingStaffMapScreen.this, R.color.tableReq));
                            }
                        }

                    }

                } catch (JSONException e) {
                    e.printStackTrace();
                    Toast.makeText(WaitingStaffMapScreen.this, "Oops, something went wrong!", Toast.LENGTH_LONG).show();
                }


            }
        },new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                Toast.makeText(WaitingStaffMapScreen.this, "Error! " +error.toString(), Toast.LENGTH_LONG).show();
            }
        }){
            @Override
            protected Map<String, String> getParams() throws AuthFailureError {

                Map<String, String> params = new HashMap<>();
                params.put("table_id", "0");
                return params;
            }
        };
        MySingleton.getInstance(this).addToRequestQueue(stringRequest);
    }

基本上我想要一些在点击 table 按钮时重新加载地图屏幕的方法 activity。

由于所有内容都 运行 在不同的设备上,我要做的是实施 firebase 云消息传递: https://firebase.google.com/docs/cloud-messaging/android/client

当应用程序启动时,设备会收到一个令牌。

当设备收到此令牌时,您将在该设备行的新列(即:FirebaseToken)下将其 POST 到您的数据库(假设您有 table 台设备)。

当 table 状态更新时(即:用户选择 table),并且到达服务器,您将有一个查询遍历注册到业务,并发送推送通知。

然后在 android 应用程序上,您将有一个接收推送通知的 firebase 服务 运行(即:)

public class MyFirebaseMessagingService extends FirebaseMessagingService
{
    NotificationManager notificationManager;
    @Override public void onMessageReceived(RemoteMessage remoteMessage)
    {
        Map<String,String> data = remoteMessage.getData();
        if (data.get("title").equals("Update Tables")) {
             //Call logic to update the adapter
        }
    }
}

非常笼统的要点。比较容易实现。有什么问题,我会尽力解答。

正如下面的评论所说,使用群发消息是最好的。我不知道每个企业会拥有多少台设备,所以尽量保持简单。

这是一个服务器端示例。这将简单地向 phone 发送推送通知,并将在您扩展的 FirebaseMessagingService class 中接收。

public bool SendMessage(string firebaseTokenId, FirebaseMessages type)
    {
        httpWebRequest = (HttpWebRequest)WebRequest.Create("http://fcm.googleapis.com/fcm/send");
        httpWebRequest.ContentType = "application/json";
        httpWebRequest.Headers.Add("Authorization", "your key");
        httpWebRequest.Method = "POST";
        using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
        {

            var messageToSend = new Message()
            {
                to = firebaseTokenId,
                data = new DataPayload()
                {
                    title = firebaseMessages[type].title,
                    message = firebaseMessages[type].body,
                }
            };
            string json = JsonConvert.SerializeObject(messageToSend, Formatting.Indented);
            streamWriter.Write(json);
            streamWriter.Flush();
            streamWriter.Close();
        }
    }
public enum FirebaseMessages
{
    UpdateTables
}

public class Message
{
    public DataPayload data {get;set;}
    public string to { get; set; }
}

    public class DataPayload
{
    public string title { get; set; }
    public string message { get; set; }
}

//on the on receive, this is a datapayload(not notification), so no push intent will ever be shown, hence you can leave the body null.
firebaseMessages.Add(FirebaseMessages.MessageReceived, new FirebaseMessage() { title = "Table Update", body = ""});