Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/220.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 从正在运行的服务更新活动UI_Java_Android_Android Service - Fatal编程技术网

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

Java 从正在运行的服务更新活动UI,java,android,android-service,Java,Android,Android Service,目前,我有一个正在运行的服务,每当其他用户向某人发送消息时,它都会从套接字接收消息。现在,在活动中,我可以轻松地调用一个对话框来显示已收到消息的通知,但是我希望从正在运行的服务中执行此操作。我该怎么处理这件事呢 这是我的跑步服务 public class MyService extends Service implements ChatCallbackAdapter { public StartSocket connect; public static Context mCon

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

这是我的跑步服务

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

        }
    }

}
您有两种选择:

  • 按照文档中的说明使用Messenger模式

  • 用于从服务发送广播,并使目标活动实现收听该广播


  • Messenger的一个优势是服务可以选择通知一个特定的Messenger其所有客户端。

    您确实不应该从服务更新UI。改为发送广播。在活动中有一个侦听器,让它处理UI更新。使用接口(回调)