无法解析符号

时间:2014-07-25 22:43:48

标签: java android sinch

我正在按照本教程构建消息传递应用:http://sinch.github.io/android-messaging-tutorial/ 一切都很顺利,直到我在MessageActivity上看到错误     package com.sinch.messagingtutorialskeleton;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.example.messagingtutorialskeleton.R;
import com.sinch.android.rtc.PushPair;
import com.sinch.android.rtc.messaging.Message;
import com.sinch.android.rtc.messaging.MessageClient;
import com.sinch.android.rtc.messaging.MessageClientListener;
import com.sinch.android.rtc.messaging.MessageDeliveryInfo;
import com.sinch.android.rtc.messaging.MessageFailureInfo;

import java.util.List;


public class MessagingActivity extends Activity implements
    ServiceConnection, MessageClientListener {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(com.example.messagingtutorialskeleton.R.layout.messaging);
    doBind();
    Intent intent = getIntent();
    recipientId = intent.getStringExtra("RECIPIENT_ID");
    sendButton = (Button) findViewById(R.id.sendButton);
    messageBodyField = (EditText) findViewById(R.id.messageBodyField);
    sendButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            sendMessage();
        }
    });
}

private void doBind() {
    Intent serviceIntent = new Intent(this, MessageService.class);
    bindService(serviceIntent, this, BIND_AUTO_CREATE);
}

private void sendMessage() {
    messageBody = messageBodyField.getText().toString();
    if (messageBody.isEmpty()) {
        Toast.makeText(this, "Please enter a message", Toast.LENGTH_LONG).show();
        return;
    }

    //Here is where you will actually send the message throught Sinch
    messageService.sendMessage(recipientId, messageBody);
}

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





private String recipientId;
private Button sendButton;
private EditText messageBodyField;
private String messageBody;
private Class messageServ


@Override
public void onIncomingMessage(MessageClient client, Message message) {
    //Update the UI on an incoming message
}


@Override
public void onMessageSent(MessageClient client, Message message,
                          String recipientId) {
    //Update the UI on an outgoing message
}

@Override
public void onMessageFailed(MessageClient client, Message message,
                            MessageFailureInfo failureInfo) {
    //Notify the user if message fails to send
    Toast.makeText(this, "Message failed to send.", Toast.LENGTH_LONG).show();
}

@Override
public void onMessageDelivered(MessageClient client,
                               MessageDeliveryInfo deliveryInfo) {
    //Intentionally  left blank
}


@Override
public void onShouldSendPushData(MessageClient client, Message message,
                                 List<PushPair> pushPairs) {
    //Intentionally left blank
}
@Override
public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
    //Define the messaging service and add a listener
    messageService = (MessageService.MessageServiceInterface) iBinder;
    messageService.addMessageClientListener(this);

    //Notify the user if they are not connected to the Sinch client.
    //Otherwise, for example, if your app key & secret are typed
    //in wrong, the user might keep hitting the send button
    //with no feedback
    if (!messageService.isSinchClientStarted()) {
        Toast.makeText(this, "The message client did not start."
                ,Toast.LENGTH_LONG).show();
    }
}

@Override
public void onServiceDisconnected(ComponentName componentName) {
    messageService = null;
}
}

它说无法解析符号messageService,我在MessageService.java中定义了消息服务:

package com.sinch.messagingtutorialskeleton;

import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;

import com.parse.ParseUser;
import com.sinch.android.rtc.ClientRegistration;
import com.sinch.android.rtc.Sinch;
import com.sinch.android.rtc.SinchClient;
import com.sinch.android.rtc.SinchClientListener;
import com.sinch.android.rtc.SinchError;
import com.sinch.android.rtc.messaging.MessageClient;
import com.sinch.android.rtc.messaging.MessageClientListener;
import com.sinch.android.rtc.messaging.WritableMessage;


public class MessageService extends Service implements SinchClientListener {

//App key and app secret are in your Sinch dashboard
//Explanatory screenshot below
private static final String APP_KEY = "41ea73ff-fb32-41e7-8c1f-f3f609bba107";
private static final String APP_SECRET = "xsy05v7bLUyNva7yXMdtJg==";
private static final String ENVIRONMENT = "sandbox.sinch.com";
private final MessageServiceInterface serviceInterface =
        new MessageServiceInterface();
private SinchClient sinchClient = null;
private MessageClient messageClient = null;
private String currentUserId;

//Gets called when you start the service from LoginActivity
@Override
public int onStartCommand(Intent intent, int flags, int startId) {

    //Get the current user id
    currentUserId = ParseUser.getCurrentUser().getObjectId().toString();

    if (currentUserId != null && !isSinchClientStarted()) {
        startSinchClient(currentUserId);
    }

    return super.onStartCommand(intent, flags, startId);
}

//Start the Sinch client
public void startSinchClient(String username) {
    //Build the Sinch client with the current user id,
    //app key, app secret, and environment host
    sinchClient = Sinch.getSinchClientBuilder()
            .context(this)
            .userId(username)
            .applicationKey(APP_KEY)
            .applicationSecret(APP_SECRET)
            .environmentHost(ENVIRONMENT)
            .build();

    sinchClient.addSinchClientListener(this);

    //This line is necessary to have messaging in your app!
    sinchClient.setSupportMessaging(true);
    sinchClient.setSupportActiveConnectionInBackground(true);

    sinchClient.checkManifest();
    sinchClient.start();
}

//To check if the Sinch client is already started
private boolean isSinchClientStarted() {
    return sinchClient != null && sinchClient.isStarted();
}

//Do you want your app to do something if starting the client fails?
@Override
public void onClientFailed(SinchClient client, SinchError error) {
    sinchClient = null;
}

//Do you want your app to do something when the sinch client starts?
@Override
public void onClientStarted(SinchClient client) {
    client.startListeningOnActiveConnection();
    messageClient = client.getMessageClient();
}

//Do you want your app to do something when the sinch client stops?
@Override
public void onClientStopped(SinchClient client) {
    sinchClient = null;
}

//Pretty self explanitory - stop the Sinch client
public void stop() {
    if (isSinchClientStarted()) {
        sinchClient.stop();
        sinchClient.removeSinchClientListener(this);
    }
    sinchClient = null;
}

//Called when you bind this to an activity (I'll get there soon)
@Override
public IBinder onBind(Intent intent) {
    return serviceInterface;
}

@Override
public boolean onUnbind(Intent intent) {
    stop();
    stopSelf();
    return super.onUnbind(intent);
}

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

@Override
public void onLogMessage(int level, String area, String message) {
    //Intentionally left blank. You will get errors if you
    //try to remove this method.
}

@Override
public void onRegistrationCredentialsRequired(SinchClient client,
                                              ClientRegistration clientRegistration) {
    //Intentionally left blank. You will get errors if you
    //try to remove this method.
}

public void sendMessage(String recipientUserId, String textBody) {
    if (messageClient != null) {
        WritableMessage message =
                new WritableMessage(recipientUserId, textBody);
        messageClient.send(message);
    }
}

public void addMessageClientListener(MessageClientListener listener) {
    if (messageClient != null) {
        messageClient.addMessageClientListener(listener);
    }
}

public void removeMessageClientListener(MessageClientListener listener) {
    if (messageClient != null) {
        messageClient.removeMessageClientListener(listener);
    }
}

//Methods that you can call from MessagingActivity
public class MessageServiceInterface extends Binder {
    public void sendMessage(String recipientUserId, String textBody) {
        MessageService.this.sendMessage(recipientUserId, textBody);
    }

    public void addMessageClientListener(MessageClientListener listener) {
        MessageService.this.addMessageClientListener(listener);
    }

    public void removeMessageClientListener(MessageClientListener listener) {
        MessageService.this.removeMessageClientListener(listener);
    }

    public boolean isSinchClientStarted() {
        return MessageService.this.isSinchClientStarted();
    }
}
}

我真的很陌生,我希望得到一些帮助:D

1 个答案:

答案 0 :(得分:1)

哦,我的坏。我错过了剩下的代码。

我认为你需要在你的类中添加一个messageService声明,所以添加

private MessageServiceInterface messageService;

靠近您的代码:

private String recipientId;
private Button sendButton;
private EditText messageBodyField; 
private String messageBody;

onServiceConnected回调将设置messageService字段的值。