대화 상대에게 메시지 보내기
시작하기 전에: BlackBerry Messenger 플랫폼에 프로그램 등록 작업을 완료합니다. 다음의 코드 샘플에서 화면을 표시하는 클래스가 프로그램과 연결된
BBMPlatformContext 객체에 대한 참조를 전달하는지 확인합니다.
- 필요한 클래스와 인터페이스를 가져옵니다.
import net.rim.blackberry.api.bbm.platform.*;
import net.rim.blackberry.api.bbm.platform.io.*;
import net.rim.blackberry.api.bbm.platform.profile.*;
import net.rim.blackberry.api.bbm.platform.service.*;
import net.rim.device.api.ui.component.*;
- MainScreen을 확장하는 클래스를 만듭니다. 이 화면 클래스의 생성자에서 프로그램과 연결된 BBMPlatformContext를 전달합니다.
public class MyBBMScreen extends MainScreen
{
public MyBBMScreen(BBMPlatformContext platformContext)
{
}
}
- BBMPlatformChannel 인스턴스를 MyBBMScreen 클래스의 멤버 변수로 선언합니다.
public class MyBBMScreen extends MainScreen
{
BBMPlatformChannel _channel;
public MyBBMScreen(BBMPlatformContext platformContext)
{
}
}
- MyBBMScreen 생성자에서 메시징 서비스의 인스턴스를 검색합니다.
public MyBBMScreen(BBMPlatformContext platformContext)
{
MessagingService myMessagingService = platformContext.getMessagingService();
}
- 채널을 생성하여 9단계에서 정의할 BBMPlatformChannelListener 인스턴스를 전달합니다.
public MyBBMScreen(BBMPlatformContext platformContext)
{
MessagingService myMessagingService = platformContext.getMessagingService();
_channel = messagingService.createChannel(_channelListener);
}
- 프로그램에 대화 상대를 초대합니다. sendInvitation()의 매개 변수에서 대화 상대가 초대를 수신할 때 제공될 메시지, 프로그램의 기본 메서드에 args[1]으로 전달될 매개 변수, 만료 시간을 지정할 수 있습니다. 다음 코드 샘플은 만료 시간에 대한 값으로 0을 전달합니다. 즉, 초대가 만료되지 않습니다. 사용자가 초대할 대화 상대를 선택할 수 있도록 대화 상대 선택기 UI 구성 요소가 열립니다.
public MyBBMScreen(BBMPlatformContext platformContext)
{
MessagingService messagingService = platformContext.getMessagingService();
_channel = messagingService.createChannel(_channelListener);
_channel.sendInvitation("Let's play a game", "Chess App", 0);
}
- MyBBMScreen 클래스에서 onContactJoined() 도우미 메서드를 정의합니다. 이 메서드는 BBMPlatformChannelListener.contactJoined() 콜백 메서드가 호출합니다. 방금 참여한 대화 상대에 대한 참조를 전달합니다.
public class MyBBMScreen extends MainScreen
{
BBMPlatformChannel _channel;
public MyBBMScreen(BBMPlatformContext platformContext)
{
//code from steps 5 to 7
}
private onContactJoined(BBMPlatformContact contact)
{
}
}
- onContactJoined() 메서드에서 메시지를 생성하고 BBMPlatformData 개체에 래핑하여 try-catch 블록에서 메시지를 보냅니다. 다음 코드 샘플에서는 참여자가 게임을 나가면 채널에 가입한 모든 대화 상대에게 이를 알리는 메시지가 전송됩니다.
private void onContactLeft(BBMPlatformContact contact)
{
String goodbyeMsg = contact.getDisplayName() + " has left the game.";
BBMPlatformData data = new BBMPlatformData(goodbyeMsg);
try {
_channel.sendData(data, _channel.getContactList());
}
catch (BBMPlatformException e)
{
// Error handler code
}
}
- MyBBMScreen 클래스에서 채널 수신기 _channelListener를 개인 비공개 내부 클래스로 정의합니다. contactJoined() 메서드에서 onContactJoined() 메서드를 호출하여 방금 참여한 BBMPlatformContact를 전달합니다.
public class MyBBMScreen extends MainScreen
{
public MyBBMScreen(BBMPlatformContext platformContext)
{
//code from steps 4 to 6
}
private void onContactJoined(BBMPlatformContact contact)
{
//code from step 8
}
private BBMPlatformChannelListener _channelListener = new BBMPlatformChannelListener()
{
public void contactsInvited(BBMPlatformConnection conn, BBMPlatformContactList
contactList)
{
}
public void contactsJoined(BBMPlatformConnection conn, BBMPlatformContactList
contacts, String cookie, int type)
{
}
public void contactDeclined(BBMPlatformConnection conn,
BBMPlatformContact contact)
{
}
public void contactLeft(BBMPlatformConnection conn, BBMPlatformContact contact)
{
onContactLeft(contact);
}
public void dataReceived(BBMPlatformConnection conn, BBMPlatformContact sender,
BBMPlatformData data)
{
}
}
};
}
코드 샘플: 대화 상대에게 메시지 보내기
다음 코드 샘플에서는 MyBBMScreen을 표시한 클래스가 프로그램과 연결된 BBMPlatformContext 개체에 대한 참조를 MyBBMScreen 생성자에 전달했다고 가정합니다.
import net.rim.blackberry.api.bbm.platform.*;
import net.rim.blackberry.api.bbm.platform.io.*;
import net.rim.blackberry.api.bbm.platform.profile.*;
import net.rim.blackberry.api.bbm.platform.service.*;
import net.rim.device.api.ui.component.*;
public class MyBBMScreen extends MainScreen
{
public MyBBMScreen(BBMPlatformContext platformContext)
{
MessagingService messagingService = platformContext.getMessagingService();
_channel = messagingService.createChannel(_channelListener);
_channel.sendInvitation("Let's play a game", "Chess App", 0);
}
private void onContactLeft(BBMPlatformContact contact)
{
String goodbyeMsg = contact.getDisplayName() + " has left the game.";
BBMPlatformData data = new BBMPlatformData(goodbyeMsg);
try
{
_channel.sendData(data, _channel.getContactList());
}
catch (BBMPlatformException e)
{
// Error handler code
}
}
private BBMPlatformChannelListener _channelListener = new BBMPlatformChannelListener()
{
public void contactsInvited(BBMPlatformConnection conn, BBMPlatformContactList contactList )
{
}
public void contactsJoined(BBMPlatformConnection conn, BBMPlatformContactList contacts, String cookie,
int type)
{
}
public void contactDeclined(BBMPlatformConnection conn, BBMPlatformContact contact)
{
}
public void contactLeft(BBMPlatformConnection conn, BBMPlatformContact contact)
{
onContactLeft(contact);
}
public void dataReceived(BBMPlatformConnection conn, BBMPlatformContact sender, BBMPlatformData data)
{
}
};
}
이 정보가 도움이 되었습니까? 의견을 보내 주십시오.