0

Main Activity当我Bind Service改变状态时,我需要刷新。我的代码MainActivity

    public class MainActivity extends AppCompatActivity
        implements UDPService.OnHeadlineSelectedListener{
      protected void onStart() {
         super.onStart();
         Intent intent = new Intent(this, UDPService.class);
         bindService(intent, myConnection, Context.BIND_AUTO_CREATE);
         //startService(intent);
     }
      private ServiceConnection myConnection = new ServiceConnection() {
            public void onServiceConnected(ComponentName className, IBinder service) {
                // This is called when the connection with the service has been
                // established, giving us the object we can use to
                // interact with the service.  We are communicating with the
                // service using a Messenger, so here we get a client-side
                // representation of that from the raw IBinder object.
                UDPService.MyLocalBinder binder = (UDPService.MyLocalBinder) service;
                myService = binder.getService();
            }
            public void onServiceDisconnected(ComponentName className) {
                // This is called when the connection with the service has been
                // unexpectedly disconnected -- that is, its process crashed.
                myService = null;
            }
        };

       protected void onCreate(Bundle savedInstanceState) {
         ... //In this part i have initialized BaseAdaptater
       }

       public static Context getContextOfApplication() {
        return contextOfApplication;
    }



       @Override
       public void onArticleSelected(int position) {
        baseAdapter.notifyDataSetChanged();
       }
}

虽然服务是:

public class UDPService extends Service  {
    private final IBinder myBinder = new MyLocalBinder();
    LampManager lm = LampManager.getInstance();
    int port = 4096;
    public String URL;
    private DatagramSocket udpSocket;
    private DatagramPacket packet;
    String text;
    static boolean bol =false;
    OnHeadlineSelectedListener mCallback;
    int i=2;
    private void listenAndWaitAndThrowIntent() throws Exception {
        byte[] message = new byte[5120];
        if(udpSocket == null || udpSocket.isClosed()){
            udpSocket = new DatagramSocket(port);
        }
        packet = new DatagramPacket(message, message.length);
        //Thread.sleep(5000);
        Log.i("UDP client: ", "about to wait to receive");
        udpSocket.receive(packet);
        String text = new String(message, 0, packet.getLength());
        this.text = text;
        Log.d("Received data", text);
        packet.getPort();
        URL = packet.getAddress().toString().replace("/", "");
        Log.d("Address", String.valueOf(packet.getAddress()));
        Log.d("Port", String.valueOf(packet.getPort()));
        udpSocket.close();
    }

    Thread UDPBroadcastThread;

    void startListenForUDPBroadcast(){
        UDPBroadcastThread = new Thread(new Runnable() {
            @Override
            public void run() {
                Looper.prepare();
                while (true) {
                    try {
                        listenAndWaitAndThrowIntent();
                         bol = lm.addLamp(URL,text);
                        mCallback = (OnHeadlineSelectedListener) MainActivity.getContextOfApplication();
                        mCallback.onArticleSelected(i);
                        Thread.sleep(1000);
                    } catch (Exception e) {
                        Log.i("UDP", e.getMessage());
                    }
                }
            }
        });
        UDPBroadcastThread.start();
    }

    private void stopListen() {
        udpSocket.close();
    }




    @Override
    public void onDestroy() {
        stopListen();
    }

    /*public int onStartCommand(Intent intent, int flags, int startId){
        startListenForUDPBroadcast();
        Log.i("UDP", "Service Started");
        return START_STICKY;
    }*/
    @Override
    public void onCreate(){
        super.onCreate();
        startListenForUDPBroadcast();
    }
    @Override
    public IBinder onBind(Intent intent) {
        // We don't provide binding, so return null
        Log.i("UDP", "Service Started");
        return myBinder;
    }

    public interface OnHeadlineSelectedListener {
        void onArticleSelected(int position);
    }

    public class MyLocalBinder extends Binder {
        UDPService getService() {
            return UDPService.this;
        }
        }
}

主要Activity有一个Listview, 一个方法addLamp(URL,text)添加Lamp到列表中。我需要刷新 Main Activity,当 bol 返回时true,我调用notifyDataSetChanged()刷新listView. 该代码有效,因为当调用onCreate()包含 的主活动时,使用我创建notifyDataSetChanged()的 刷新列表。我的问题是不出来。代码是:lampaddLampServicelooperaddLamp

public class LampManager extends AppCompatActivity {


    private static final LampManager ourInstance = new LampManager();
    private List<Lamp> lista = new ArrayList();
    Context applicationContext = MainActivity.getContextOfApplication();
 public boolean addLamp(String URL, String name) throws InterruptedException{
        String ip = URL.replace("/", "");
        Log.i("UDP", "Messagio ricevuto!");
        boolean b = true;
        System.out.println(getLamps().size());
        Lamp l =null;
        try {
        for(int i=0; i<getLamps().size();i++) {
            System.out.println(getLamp(i).getURL());
            System.out.println(getLamp(i).getURL());
            char c = name.charAt(name.length()-1);
            Thread.sleep(5000);
            if (lista.get(i).getName().equals(name)) {
                b = false;
            }else if(c>='0' && c<='9' && name.contains("LAMP_"))
                b=true;
            else
                b=false;
        }
        if(b){
            System.out.println("Thread.sleep(5000)");
            l = new Lamp(URL, name);
            new TcpClient(l, applicationContext).execute();
            lista.add(l);
            return b;
        }
        }catch (Exception e){
            Log.d("ERR",e.getMessage());
        }
        return b;
    }
}

虽然代码lamp是:

public  class Lamp extends AppCompatActivity {
    private String name;
    private int rgb;
    private Bitmap image;;
    private int intensity;
    private Boolean state;
    public String   URL;

    //save the context recievied via constructor in a local variable
    Context applicationContext = MainActivity.getContextOfApplication();


    public Lamp( String URL, String name){
        this.name=name;
        this.URL=URL;
        SharedPreferences sh = PreferenceManager.getDefaultSharedPreferences(applicationContext);
        SharedPreferences.Editor editor = sh.edit();
        setName(name);
        editor.putString(getName()+":URL", URL);
        editor.apply();
    }
4

1 回答 1

0

使用接口

首先,在您的服务类上定义一个接口:

    // Container Activity must implement this interface
public interface OnHeadlineSelectedListener {
    public void onArticleSelected(int position);
}

现在,片段可以通过使用 OnHeadlineSelectedListener 接口的 mCallback 实例调用 onArticleSelected() 方法(或接口中的其他方法)将消息传递给活动。

然后,在活动上:

public static class MainActivity extends Activity
    implements HeadlinesFragment.OnHeadlineSelectedListener{
...

public void onArticleSelected(int position) {
    // The user selected the headline of an article from the HeadlinesFragment
    // Do something here to display that article
}

}

成功:D

于 2018-01-15T10:35:44.380 回答