电话锁定后WifiLock无法正常工作且服务已停止

时间:2013-11-23 17:57:19

标签: android sockets service android-wifi wakelock

我有一个服务正在运行,它在局域网中接收消息,服务工作正常,当手机打开但电话被手动或自动锁定服务无法工作,即使我已添加WifiLock和WakeLock ..我的服务是: package soft.b.peopleassist;

public class Server extends Service {
    static String UDP_BROADCAST = "soft.b.peopleassist";
    public static String ip;
    //Boolean shouldListenForUDPBroadcast = false;
    DatagramSocket socket;
     private WifiLock wifiLock;
        private WakeLock wakeLock;

    //Intent intent;
    @SuppressLint("DefaultLocale")

    public String getIpAddr() {
           WifiManager wifiManager = (WifiManager) getSystemService(WIFI_SERVICE);
           WifiInfo wifiInfo = wifiManager.getConnectionInfo();
           int ip = wifiInfo.getIpAddress();

           String ipString = String.format(
           "%d.%d.%d.%d",
           (ip & 0xff),
           (ip >> 8 & 0xff),
           (ip >> 16 & 0xff),
           (ip >> 24 & 0xff));

           return ipString;
        }





void setIP()


    {
      ConnectivityManager connManager = (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo mWifi = connManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);

        if (mWifi.isConnected()) {

        ip="192.168.1.255";
        Log.i("server", "AP connected so 192");
        }
        if (!mWifi.isConnected()) {

        ip="192.168.49.255";
        Log.i("server", "AP not connected so"+ip);
        }
        }





    private void listenAndWaitAndThrowIntent(InetAddress broadcastIP, Integer port) throws Exception {
        byte[] recvBuf = new byte[15000];
        if (socket == null || socket.isClosed()) {
            socket = new DatagramSocket(port);
        //  socket.setBroadcast(true);
        }
        //socket.setSoTimeout(1000);
        DatagramPacket packet = new DatagramPacket(recvBuf, recvBuf.length);
        Log.e("UDP", "Waiting for UDP broadcast");
        socket.receive(packet);

        String senderIP = packet.getAddress().getHostAddress().trim();
        String message = new String(packet.getData());

        Log.e("UDP", "Got UDB broadcast from " + senderIP + ", message: " + message);

 String str=getIpAddr().trim();
//if(!str.equals(senderIP))
//{
    broadcastIntent(senderIP, message);
    String[] arr = message.split(" ", 5);
    char messagestatus = arr[1].charAt(0);
    Log.i("UDP", String.valueOf(messagestatus));
    if(messagestatus=='s')
    {
     Uri notification = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
        Ringtone r = RingtoneManager.getRingtone(getApplicationContext(), notification);
        r.play();
    }
    //      str=null;

//}
str=null;

        socket.close();
    }

    private void broadcastIntent(String senderIP, String message) {
    Intent  intent = new Intent(Server.UDP_BROADCAST);
        intent.putExtra("sender", senderIP);
        intent.putExtra("messages", message);
        sendBroadcast(intent);
    }

    Thread UDPBroadcastThread;

    void startListenForUDPBroadcast() {
        UDPBroadcastThread = new Thread(new Runnable() {
            public void run() {
                try {
                    InetAddress broadcastIP = InetAddress.getByName(ip); //172.16.238.42 //192.168.1.255
                    Integer port = 11111;
                    while (shouldRestartSocketListen) {
                        listenAndWaitAndThrowIntent(broadcastIP, port);
                    }
                    //if (!shouldListenForUDPBroadcast) throw new ThreadDeath();
                } catch (Exception e) {
                    Log.i("UDP", "no longer listening for UDP broadcasts cause of error " + e.getMessage());
                }
            }
        });
        UDPBroadcastThread.start();
    }

    private Boolean shouldRestartSocketListen=true;

    void stopListen() {
        shouldRestartSocketListen = false;
        if(socket!=null)
        socket.close();
    }

    @Override
    public void onCreate() {

    };

    @Override
    public void onDestroy() {
        stopListen();
         wifiLock.release();
            wakeLock.release();

        Log.i("UDP", "Service stoped");
    }


    @SuppressLint("Wakelock")
    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        WifiManager wm = (WifiManager) getSystemService(Context.WIFI_SERVICE);
        wifiLock = wm.createWifiLock(WifiManager.WIFI_MODE_FULL , "MyWifiLock");
        wifiLock.acquire();

        PowerManager powerManager = (PowerManager)getSystemService(Context.POWER_SERVICE);
        wakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "My Lock");
        wakeLock.acquire();


            shouldRestartSocketListen = true;

        startListenForUDPBroadcast();
        Log.i("UDP", "Service started");
        return START_STICKY;
    }

    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

}

0 个答案:

没有答案