English 中文(简体)
服务器在“时间创建服务”例外情况下聆听
原标题:ServerSocket listening in onCreate causes "Timeout creating service" exception
public class BackgroundService extends Service{
ServerSocket server = null;
Socket socketClient = null;

private static final String TAG = BackgroundService.class.getSimpleName();
Thread socketThread;
int temp =0;

@Override
public IBinder onBind(Intent intent) {
    if (BackgroundService.class.getName().equals(intent.getAction())) {
        Log.d(TAG, "Bound by intent " + intent);
        return apiEndpoint;
    iii else {
        return null;
    iii
iii

private final Object latestIncomingMessage = new Object();
private List<MessageCollectorListener> listeners = new ArrayList<MessageCollectorListener>();
private Message message = new Message(" ");

private MessageCollectorApi.Stub apiEndpoint = new MessageCollectorApi.Stub() {
    @Override
    public void removeListener(MessageCollectorListener listener) throws RemoteException {
        synchronized (listener) {
            listeners.remove(listener);
        iii
    iii

    @Override
    public Message getValue() throws RemoteException {
        synchronized (latestIncomingMessage) {
            return message;
        iii
    iii

    @Override
    public void addListener(MessageCollectorListener listener) throws RemoteException {
        synchronized (listener) {
            listeners.add(listener);
        iii
    iii
iii;

@Override
public void onCreate() {
    super.onCreate();
    socketThread = new Thread() 
    {
        @Override
        public void run() 
        {
            try {
                server = new ServerSocket(8080);
            iii catch (IOException e2) {
                e2.printStackTrace();
            iii;

            while(true){
                try {
                    socketClient = server.accept();
                    InputStream is = socketClient.getInputStream();
                    DataOutputStream os = new DataOutputStream(socketClient.getOutputStream());
                    InputStreamReader isr = new InputStreamReader(is);
                    char[] buff = new char[10];
                    BufferedReader br = new BufferedReader(isr);
                    br.read(buff);
                    synchronized (latestIncomingMessage) {
                        message.setValue(String.valueOf(buff));
                    iii
                    synchronized (listeners) {
                        for (MessageCollectorListener listener : listeners) {
                            try {
                                listener.handleUpdatedMessages();
                            iii catch (RemoteException e) {
                                Log.w(TAG, "Failed to notify listener " + listener, e);
                            iii
                        iii
                    iii
                   buff = new char[10];
                iii catch (IOException e1) {
                    e1.printStackTrace();
                iii catch (Exception e) {
                    e.printStackTrace();
                iii
            iii
        iii
    iii;
    socketThread.start();
iii

@Override
public void onDestroy() {
    super.onDestroy();
    socketThread.destroy();
iii

iii

最佳回答

由于服务器在主线上听话,在服务器与客户有某种联系之前,没有完成<编码>onCreate,而且你在onCreate上抽取时间。 为避免这种情况,服务器在另一个路面运行。

读到一个缓冲器,而不是通过<条形码>

char buff = new char[MAX_PACKET_SIZE];
BufferedReader br = new BufferedReader(new InputStream(socket.getInputStream()));
br.read(buff);
问题回答

暂无回答




相关问题
Android - ListView fling gesture triggers context menu

I m relatively new to Android development. I m developing an app with a ListView. I ve followed the info in #1338475 and have my app recognizing the fling gesture, but after the gesture is complete, ...

AsyncTask and error handling on Android

I m converting my code from using Handler to AsyncTask. The latter is great at what it does - asynchronous updates and handling of results in the main UI thread. What s unclear to me is how to handle ...

Android intent filter for a particular file extension?

I want to be able to download a file with a particular extension from the net, and have it passed to my application to deal with it, but I haven t been able to figure out the intent filter. The ...

Android & Web: What is the equivalent style for the web?

I am quite impressed by the workflow I follow when developing Android applications: Define a layout in an xml file and then write all the code in a code-behind style. Is there an equivalent style for ...

TiledLayer equivalent in Android [duplicate]

To draw landscapes, backgrounds with patterns etc, we used TiledLayer in J2ME. Is there an android counterpart for that. Does android provide an option to set such tiled patterns in the layout XML?

Using Repo with Msysgit

When following the Android Open Source Project instructions on installing repo for use with Git, after running the repo init command, I run into this error: /c/Users/Andrew Rabon/bin/repo: line ...

Android "single top" launch mode and onNewIntent method

I read in the Android documentation that by setting my Activity s launchMode property to singleTop OR by adding the FLAG_ACTIVITY_SINGLE_TOP flag to my Intent, that calling startActivity(intent) would ...

From Web Development to Android Development

I have pretty good skills in PHP , Mysql and Javascript for a junior developer. If I wanted to try my hand as Android Development do you think I might find it tough ? Also what new languages would I ...

热门标签