从正在运行的服务更新活动UI

时间:2013-07-25 07:34:17

标签: java android android-service

我目前有一个正在运行的服务,每当另一个用户向某人发送消息时,它就会从套接字接收消息。现在,在活动中,我可以轻松地调用对话框来显示已收到消息的通知,但是我想从正在运行的服务中执行此操作。我怎么能得到这个呢?

这是我正在运行的服务。

public class MyService extends Service implements ChatCallbackAdapter {

    public StartSocket connect;
    public static Context mContent;
    private ConnectSocket connectsocket;
    final Context context = this;

    @Override
    public IBinder onBind(Intent arg0) {
        // TODO Auto-generated method stub
        return null;
    }

     @Override
     public void onStart(Intent intent, int startId) {
        System.out.println("Service is running");
        connectsocket= new ConnectSocket(this);
        connectsocket.start();
        connect=new StartSocket();
    }

    public void startNotification(){
        Intent intent = new Intent(this, ReceiveNotification.class);
        PendingIntent pIntent = PendingIntent.getActivity(this, 0, intent, 0);

        // Build notification
        // Actions are just fake
        Notification noti = new Notification.Builder(this)
            .setContentTitle("Received a message")
            .setContentText("Subject").setSmallIcon(R.drawable.ic_launcher)
            .setContentIntent(pIntent)
            .addAction(R.drawable.ic_launcher, "Rply", pIntent)
            .build();
        NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        // Hide the notification after its selected
        noti.flags |= Notification.FLAG_AUTO_CANCEL;

        notificationManager.notify(0, noti);
    }

    @Override
    public void callback(JSONArray data) throws JSONException {
        // TODO Auto-generated method stub

    }

    @Override
    public void on(String event, JSONObject data) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onMessage(String message) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onMessage(JSONObject json) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onConnect() {
        // TODO Auto-generated method stub

    }

    @Override
    public void onDisconnect() {
        // TODO Auto-generated method stub

    }

    @Override
    public void onConnectFailure() {
        // TODO Auto-generated method stub

    }

    @Override
    public void onMessageReceived(Message m) {
        // TODO Auto-generated method stub
        System.out.println("Received a message in service");
        if(m.status.equals("ready")){
            connectsocket.login(SaveSharedPreference.getUserName(getApplicationContext()), SaveSharedPreference.getUserId(getApplicationContext()));
            connectsocket.subscribe();
        }
        if(m.status.equals("message")){
            //getMsg(m.msg, m.name);
            startNotification();
            System.out.println("Received a message "+m.msg+" and a name "+m.name);
            final String name=m.name;
            final String pid=m.pid;
            final String msg=m.msg;

            //Intent intenter=new Intent(TabExercise.this, CreateNotification.class);
            //startActivity(intenter);

            //runOnUiThread(new Runnable(){

            //public void run(){
            //Handler handler = new Handler(Looper.getMainLooper());

             AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
                    context);
             alertDialogBuilder.setTitle(name+" just sent you a message");
             alertDialogBuilder.setMessage("Click yes to go to the message");
             alertDialogBuilder

                .setCancelable(false)
                .setPositiveButton("Yes",new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog,int id) {
                        Intent inte=new Intent(MyService.this, Chat.class);
                        Bundle extras=new Bundle();
                        extras.putString("name", name);
                        extras.putString("pid", pid);
                        extras.putString("msg", msg);
                        inte.putExtras(extras);
                        startActivity(inte);

                        dialog.cancel();
                    }
                  })
                .setNegativeButton("No",new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog,int id) {
                        // if this button is clicked, just close
                        // the dialog box and do nothing
                        dialog.cancel();
                    }
                });

                // create alert dialog
                AlertDialog alertDialog = alertDialogBuilder.create();

                // show it
                alertDialog.show();
            //}
            //});

        }
    }

}

1 个答案:

答案 0 :(得分:2)

您有两个选择:

  1. 使用Messenger文件,如文档here中所述。

  2. 使用LocalBroadcastManager从服务发送广播,并使目标活动实施BroadcastReceiver以收听该广播。

  3. Messenger的优势在于服务可以选择通知其所有客户的特定信使。