当前位置: 动力学知识库 > 问答 > 编程问答 >

android - QuickBlox chat, by leaked IntentReceiver of vc908.stickerfactory, unregisterReceiver error

问题描述:

I am using Quickblox chat sample. When I run chat app, there is no error.

But after kill process project, app happens crashes. Because AnalyticsTaskReceiver and UpdatePacksTaskReceiver.

When I use vc908.stickerfactory library, chat activity have warning, that ChatActivity has leaked IntentReceiver [email protected] which was originally registered here.

Are you missing a call to unregisterReceiver()? So I try to find registered receiver but didn't found.

How can I fix this bug?

I use this;

`compile('vc908.stickers:stickerfactory:[email protected]') {

transitive = true;

}'

`public class CustomBuyingChatActivity extends BaseActivity implements KeyboardHandleRelativeLayout.KeyboardSizeChangeListener {

private static final String TAG = CustomBuyingChatActivity.class.getSimpleName();

public static final String EXTRA_DIALOG = "dialog";

private final String PROPERTY_SAVE_TO_HISTORY = "save_to_history";

private EditText messageEditText;

private ListView messagesContainer;

private Button sendButton;

private ProgressBar progressBar;

private ChatAdapter adapter;

private Chat chat;

private QBDialog dialog;

private KeyboardHandleRelativeLayout keyboardHandleLayout;

private View stickersFrame;

private boolean isStickersFrameVisible;

private ImageView stickerButton;

private RelativeLayout container;

private QBSentData receiveSellerData;

private String sellerName;

private QBUser selectedUser;

private ImageView sellerImage;

private TextView goods,sellername, orderprice;

private CircularImageView manphoto;

private Button btnBack;

private String manPhotoUrl, myPhotoUrl;

private SharedPreferences objSharedPref;

public static void start(Context context, Bundle bundle) {

Intent intent = new Intent(context, ChatActivity.class);

intent.putExtras(bundle);

context.startActivity(intent);

}

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.buying_chat);

objSharedPref = Utils.getSharedPreferences(this);

Bundle extras = getIntent().getExtras();

receiveSellerData = (QBSentData)extras.getSerializable("senddatatoCustomChat");

sellerName = receiveSellerData.getSellername();

sellerImage = (ImageView)findViewById(R.id.seller_photo11);

goods = (TextView)findViewById(R.id.goodsname11);

sellername = (TextView)findViewById(R.id.sellername11);

orderprice = (TextView)findViewById(R.id.price11);

manphoto = (CircularImageView)findViewById(R.id.manphoto11);

btnBack = (Button)findViewById(R.id.btnqb_back);

btnBack.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View v) {

finish();

}

});

goods.setText(receiveSellerData.getTitle());

sellername.setText(receiveSellerData.getSellername());

orderprice.setText("$ " + receiveSellerData.getId());

manPhotoUrl = receiveSellerData.getSellerPhoto();

myPhotoUrl = objSharedPref.getString(Utils.USERPHOTO, "");

UrlImageViewHelper.setUrlDrawable(sellerImage, receiveSellerData.getImage_url(), R.drawable.no_image, new UrlImageViewCallback() {

@Override

public void onLoaded(ImageView imageView, Bitmap loadedBitmap, String url, boolean loadedFromCache) {

if (!loadedFromCache) {

ScaleAnimation scale = new ScaleAnimation(0, 1, 0, 1, ScaleAnimation.RELATIVE_TO_SELF, .5f, ScaleAnimation.RELATIVE_TO_SELF, .5f);

scale.setDuration(300);

scale.setInterpolator(new OvershootInterpolator());

imageView.startAnimation(scale);

}

}

});

UrlImageViewHelper.setUrlDrawable(manphoto, receiveSellerData.getSellerPhoto(), R.drawable.no_image, new UrlImageViewCallback() {

@Override

public void onLoaded(ImageView imageView, Bitmap loadedBitmap, String url, boolean loadedFromCache) {

if (!loadedFromCache) {

ScaleAnimation scale = new ScaleAnimation(0, 1, 0, 1, ScaleAnimation.RELATIVE_TO_SELF, .5f, ScaleAnimation.RELATIVE_TO_SELF, .5f);

scale.setDuration(300);

scale.setInterpolator(new OvershootInterpolator());

imageView.startAnimation(scale);

}

}

});

loadNext();

ChatService.getInstance().addConnectionListener(chatConnectionListener);

}

@Override

protected void onDestroy() {

ChatService.getInstance().removeConnectionListener(chatConnectionListener);

if (!getIntent().getBooleanExtra("fromDialog", false)) {

QBChatService.getInstance().destroy();

}

Utils.garbageCollect();

super.onDestroy();

}

@Override

public void onBackPressed() {

if (isStickersFrameVisible) {

setStickersFrameVisible(false);

stickerButton.setImageResource(R.drawable.ic_action_insert_emoticon);

} else {

try {

chat.release();

} catch (XMPPException e) {

Log.e(TAG, "failed to release chat", e);

}

super.onBackPressed();

finish();

}

}

private void initViews() {

messagesContainer = (ListView) findViewById(R.id.messagescontainer11);

messageEditText = (EditText) findViewById(R.id.messageEdit11);

progressBar = (ProgressBar) findViewById(R.id.progressBar);

TextView companionLabel = (TextView) findViewById(R.id.companionLabel11);

container = (RelativeLayout) findViewById(R.id.container11);

if (dialog.getType() == QBDialogType.GROUP) {

TextView meLabel = (TextView) findViewById(R.id.meLabel11);

container.removeView(meLabel);

container.removeView(companionLabel);

} else if (dialog.getType() == QBDialogType.PRIVATE) {

Integer opponentID = ChatService.getInstance().getOpponentIDForPrivateDialog(dialog);

companionLabel.setText(ChatService.getInstance().getDialogsUsers().get(opponentID).getLogin());

}

// Send button

//

sendButton = (Button) findViewById(R.id.chatSendButton11);

sendButton.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View v) {

String messageText = messageEditText.getText().toString();

if (TextUtils.isEmpty(messageText)) {

return;

}

sendChatMessage(messageText);

}

});

// Stickers

keyboardHandleLayout = (KeyboardHandleRelativeLayout) findViewById(R.id.sizeNotifierLayout11);

keyboardHandleLayout.listener = this;

stickersFrame = findViewById(R.id.frame11);

stickerButton = (ImageView) findViewById(R.id.stickers_button11);

stickerButton.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View v) {

if (isStickersFrameVisible) {

showKeyboard();

stickerButton.setImageResource(R.drawable.ic_action_insert_emoticon);

} else {

if (keyboardHandleLayout.isKeyboardVisible()) {

keyboardHandleLayout.hideKeyboard(CustomBuyingChatActivity.this, new KeyboardHandleRelativeLayout.OnKeyboardHideCallback() {

@Override

public void onKeyboardHide() {

stickerButton.setImageResource(R.drawable.ic_action_keyboard);

setStickersFrameVisible(true);

}

});

} else {

stickerButton.setImageResource(R.drawable.ic_action_keyboard);

setStickersFrameVisible(true);

}

}

}

});

updateStickersFrameParams();

StickersFragment stickersFragment = (StickersFragment) getSupportFragmentManager().findFragmentById(R.id.frame11);

if (stickersFragment == null) {

stickersFragment = new StickersFragment.Builder()

.setStickerPlaceholderColorFilterRes(android.R.color.darker_gray)

.build();

getSupportFragmentManager().beginTransaction().replace(R.id.frame11, stickersFragment).commit();

}

stickersFragment.setOnStickerSelectedListener(stickerSelectedListener);

stickersFragment.setOnEmojiBackspaceClickListener(new OnEmojiBackspaceClickListener() {

@Override

public void onEmojiBackspaceClicked() {

KeyEvent event = new KeyEvent(

0, 0, 0, KeyEvent.KEYCODE_DEL, 0, 0, 0, 0, KeyEvent.KEYCODE_ENDCALL);

messageEditText.dispatchKeyEvent(event);

}

});

setStickersFrameVisible(isStickersFrameVisible);

}

private void showKeyboard() {

((InputMethodManager) messageEditText.getContext().getSystemService(Context.INPUT_METHOD_SERVICE)).showSoftInput(messageEditText, InputMethodManager.SHOW_IMPLICIT);

}

private void sendChatMessage(String messageText) {

QBChatMessage chatMessage = new QBChatMessage();

chatMessage.setBody(messageText);

chatMessage.setProperty(PROPERTY_SAVE_TO_HISTORY, "1");

chatMessage.setDateSent(new Date().getTime() / 1000);

try {

chat.sendMessage(chatMessage);

} catch (XMPPException e) {

Log.e(TAG, "failed to send a message", e);

} catch (SmackException sme) {

Log.e(TAG, "failed to send a message", sme);

}

messageEditText.setText("");

if (dialog.getType() == QBDialogType.PRIVATE) {

showMessage(chatMessage);

}

}

private OnStickerSelectedListener stickerSelectedListener = new OnStickerSelectedListener() {

@Override

public void onStickerSelected(String code) {

if (StickersManager.isSticker(code)) {

sendChatMessage(code);

} else {

// append emoji to edit

messageEditText.append(code);

}

}

};

@Override

public void onKeyboardVisibilityChanged(boolean isVisible) {

if (isVisible) {

setStickersFrameVisible(false);

stickerButton.setImageResource(R.drawable.ic_action_insert_emoticon);

} else {

if (isStickersFrameVisible) {

stickerButton.setImageResource(R.drawable.ic_action_keyboard);

} else {

stickerButton.setImageResource(R.drawable.ic_action_insert_emoticon);

}

}

}

private void setStickersFrameVisible(final boolean isVisible) {

stickersFrame.setVisibility(isVisible ? View.VISIBLE : View.GONE);

isStickersFrameVisible = isVisible;

if (stickersFrame.getHeight() != vc908.stickerfactory.utils.KeyboardUtils.getKeyboardHeight()) {

updateStickersFrameParams();

}

final int padding = isVisible ? vc908.stickerfactory.utils.KeyboardUtils.getKeyboardHeight() : 0;

if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) {

keyboardHandleLayout.post(new Runnable() {

@Override

public void run() {

setContentBottomPadding(padding);

scrollDown();

}

});

} else {

setContentBottomPadding(padding);

}

scrollDown();

}

private void updateStickersFrameParams() {

StickersManager.initialize("847b82c49db21ecec88c510e377b452c", this);

int d = vc908.stickerfactory.utils.KeyboardUtils.getKeyboardHeight();

stickersFrame.getLayoutParams().height = vc908.stickerfactory.utils.KeyboardUtils.getKeyboardHeight();

}

public void setContentBottomPadding(int padding) {

container.setPadding(0, 0, 0, padding);

}

private void initChat() {

if (dialog.getType() == QBDialogType.GROUP) {

// now I don't need GroupChat;

 // Join group chat

//

progressBar.setVisibility(View.VISIBLE);

//

joinGroupChat();

} else if (dialog.getType() == QBDialogType.PRIVATE) {

Integer opponentID = ChatService.getInstance().getOpponentIDForPrivateDialog(dialog);

chat = new PrivateChatImpl(CustomBuyingChatActivity.this, opponentID);

// Load CHat history

//

loadChatHistory();

}

}

private void joinGroupChat() {

((GroupChatImpl) chat).joinGroupChat(dialog, new QBEntityCallbackImpl() {

@Override

public void onSuccess() {

// Load Chat history

//

loadChatHistory();

}

@Override

public void onError(List list) {

AlertDialog.Builder dialog = new AlertDialog.Builder(CustomBuyingChatActivity.this);

dialog.setMessage("error when join group chat: " + list.toString()).create().show();

}

});

}

private void loadChatHistory() {

QBRequestGetBuilder customObjectRequestBuilder = new QBRequestGetBuilder();

customObjectRequestBuilder.setPagesLimit(100);

customObjectRequestBuilder.sortDesc("date_sent");

QBChatService.getDialogMessages(dialog, customObjectRequestBuilder, new QBEntityCallbackImpl<ArrayList<QBChatMessage>>() {

@Override

public void onSuccess(ArrayList<QBChatMessage> messages, Bundle args) {

adapter = new ChatAdapter(CustomBuyingChatActivity.this, new ArrayList<QBChatMessage>(), manPhotoUrl, myPhotoUrl);

messagesContainer.setAdapter(adapter);

for (int i = messages.size() - 1; i >= 0; --i) {

QBChatMessage msg = messages.get(i);

showMessage(msg);

}

progressBar.setVisibility(View.GONE);

if (getIntent().getBooleanExtra("real", false)){

sendChatMessage("I want to buy.");

}

}

@Override

public void onError(List<String> errors) {

if (!CustomBuyingChatActivity.this.isFinishing()) {

AlertDialog.Builder dialog = new AlertDialog.Builder(CustomBuyingChatActivity.this);

dialog.setMessage("load chat history errors: " + errors).create().show();

}

}

});

}

public void showMessage(QBChatMessage message) {

adapter.add(message);

runOnUiThread(new Runnable() {

@Override

public void run() {

adapter.notifyDataSetChanged();

scrollDown();

}

});

}

private void scrollDown() {

messagesContainer.setSelection(messagesContainer.getCount() - 1);

}

ConnectionListener chatConnectionListener = new ConnectionListener() {

@Override

public void connected(XMPPConnection connection) {

Log.i(TAG, "connected");

}

@Override

public void authenticated(XMPPConnection connection) {

Log.i(TAG, "authenticated");

}

@Override

public void connectionClosed() {

Log.i(TAG, "connectionClosed");

}

@Override

public void connectionClosedOnError(final Exception e) {

Log.i(TAG, "connectionClosedOnError: " + e.getLocalizedMessage());

// leave active room

//

if (dialog.getType() == QBDialogType.GROUP) {

CustomBuyingChatActivity.this.runOnUiThread(new Runnable() {

@Override

public void run() {

((GroupChatImpl) chat).leave();

}

});

}

}

@Override

public void reconnectingIn(final int seconds) {

if (seconds % 5 == 0) {

Log.i(TAG, "reconnectingIn: " + seconds);

}

}

@Override

public void reconnectionSuccessful() {

Log.i(TAG, "reconnectionSuccessful");

// Join active room

//

if (dialog.getType() == QBDialogType.GROUP) {

CustomBuyingChatActivity.this.runOnUiThread(new Runnable() {

@Override

public void run() {

joinGroupChat();

}

});

}

}

@Override

public void reconnectionFailed(final Exception error) {

Log.i(TAG, "reconnectionFailed: " + error.getLocalizedMessage());

}

};

//

// ApplicationSessionStateCallback

//

@Override

public void onStartSessionRecreation() {

}

@Override

public void onFinishSessionRecreation(final boolean success) {

runOnUiThread(new Runnable() {

@Override

public void run() {

if (success) {

initChat();

}

}

});

}

private void loadNext(){

getQBPagedRequestBuilder(1);

}

public void getQBPagedRequestBuilder(int page){

QBPagedRequestBuilder pagedRequestBuilder = new QBPagedRequestBuilder();

pagedRequestBuilder.setPage(page);

pagedRequestBuilder.setPerPage(50);

QBUsers.getUsers(pagedRequestBuilder, new QBEntityCallbackImpl<ArrayList<QBUser>>() {

@Override

public void onSuccess(ArrayList<QBUser> result, Bundle params) {

for (QBUser user : result) {

if (user.getLogin().equals(sellerName)) {

selectedUser = user;

break;

}

}

gotoChatRoom();

}

@Override

public void onError(List<String> errors) {

Log.d("=============", errors.get(0));

String Error = "";

for (int i = 0; i < errors.size(); i++){

Error += errors.get(i) + " ";

}

finish();

}

});

}

public void gotoChatRoom(){

List<QBUser> selected = new ArrayList<QBUser>();

selected.add(selectedUser);

ChatService.getInstance().addDialogsUsers(selected);

//Create new dialog

QBDialog dialogToCreate = new QBDialog();

dialogToCreate.setName(usersListToChatName());

dialogToCreate.setType(QBDialogType.PRIVATE);

dialogToCreate.setOccupantsIds(getUserIds(selectedUser));

QBChatService.getInstance().getGroupChatManager().createDialog(dialogToCreate, new QBEntityCallbackImpl<QBDialog>() {

@Override

public void onSuccess(QBDialog result, Bundle params) {

dialog = result;

initViews();

if (isSessionActive()) {

initChat();

}

}

@Override

public void onError(List<String> errors) {

super.onError(errors);

String Error = "";

for (int i = 0; i < errors.size(); i++){

Error += errors.get(i) + " ";

}

finish();

}

});

}

private String usersListToChatName(){

return selectedUser.getLogin();

}

public static ArrayList<Integer> getUserIds(QBUser user) {

ArrayList<Integer> ids = new ArrayList<Integer>();

ids.add(user.getId());

return ids;

}`

网友答案:

As per question you need private chat so you need to release private chat using below method when it will not require :

 public void release() {
        Log.w(TAG, "release private chat");
        privateChat.removeMessageListener(this);
        privateChatManager.removePrivateChatManagerListener(this);
    }

Here privateChatManager is initialize like

 privateChatManager = QBChatService.getInstance().getPrivateChatManager();
分享给朋友:
您可能感兴趣的文章:
随机阅读: