YukiPushService.java 3.2 KB
Newer Older
T
Takeya Yuki 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
package yuki.msg.extended;

import android.app.Notification;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Intent;
import android.net.Uri;
import android.os.IBinder;
import android.support.annotation.Nullable;
import android.util.Log;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.Socket;

/**
 * Created by Akeno on 2017/08/28.
 */

public abstract class YukiPushService extends Service {

    public abstract String GetRemoteEndPoint();
    private Socket socket;
    public abstract int GetIcon();
    public abstract int GetServiceID();

    public void MessagePushed(String msg){
        Intent i=new Intent(Intent.ACTION_VIEW, Uri.parse("msg://msg"));
        PendingIntent pi=PendingIntent.getActivity(getApplicationContext(),0,i,PendingIntent.FLAG_UPDATE_CURRENT);
        NotificationController.Notify(getApplicationContext(),GetServiceID(),GetIcon(),pi,
                "Yuki Push Service","Yuki Push Service",msg, Notification.FLAG_NO_CLEAR|Notification.FLAG_AUTO_CANCEL);
    }

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

    @Override
    public void onCreate(){
        super.onCreate();
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {

            Thread thread=new Thread(new Runnable() {
                @Override
                public void run() {
                    try {
                        final String EndPoint=GetRemoteEndPoint();
                        Log.e("ep",EndPoint);
                        Log.e("eph",EndPoint.split(":")[0]);
                        Log.e("epp",EndPoint.split(":")[1]);
                        socket = new Socket(EndPoint.split(":")[0], Integer.parseInt(EndPoint.split(":")[1]));
                        BufferedReader sin=new BufferedReader(new InputStreamReader(socket.getInputStream()));
                        String readline;
                        readline=sin.readLine();

T
Takeya Yuki 已提交
62
                        while(true){
T
Takeya Yuki 已提交
63 64 65
                            readline=sin.readLine();
                            if(readline.isEmpty()){continue;}
                            if(readline==null){
T
Takeya Yuki 已提交
66 67 68 69
                                continue;
                            }
                            else {
                                MessagePushed(readline);
T
Takeya Yuki 已提交
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
                            }
                        }
                    }
                    catch(IOException ex) {

                    }
                    catch (Exception ex){

                    }finally {
                        if(socket.isClosed()){

                        }
                        else if(socket.isConnected()){
                            try {
                                socket.close();
                            } catch (IOException e) {
                                e.printStackTrace();
                            }
                        }
                    }
                }
            });
        thread.start();
        return Service.START_STICKY;
        //return super.onStartCommand(intent, flags, startId);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }
}