0

現在、別のユーザーが誰かにメッセージを送信するたびにソケットからメッセージを受信するサービスを実行しています。アクティビティでは、メッセージを受信したという通知を表示するダイアログを簡単に呼び出すことができますが、実行中のサービスから実行したいと考えています。どうすればこれを解決できますか?

これが私の実行中のサービスです。

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();
            //}
            //});

        }
    }

}
4

1 に答える 1

2

次の 2 つのオプションがあります。

  1. ここのドキュメントで説明されているように、Messenger パターンを使用します。

  2. サービスからブロードキャストを送信し、そのブロードキャストをリッスンするLocalBroadcastManagerようにターゲット アクティビティを実装するために使用します。BroadcastReceiver

Messenger の利点は、サービスが特定の 1 つのメッセンジャーにすべてのクライアントを通知するように選択できることです。

于 2013-07-25T07:45:08.690 に答える