gpt4 book ai didi

java - BroadcastReceiver onReceive 用于聊天功能

转载 作者:行者123 更新时间:2023-12-01 10:44:05 25 4
gpt4 key购买 nike

我正在尝试使用 android studio 设置聊天功能,但收到消息时聊天屏幕没有更新。

loadMsg();发送消息时功能正在工作。但接收时则不然。

这是我的代码:

@Override
public void onStart() {
super.onStart();
IntentFilter ief = new IntentFilter("test");
registerReceiver(mReceiver, ief);
}

private BroadcastReceiver mReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
// Show("Intent Receved");
if (intent.getAction().equals("test")) {
// Do stuff
loadMsg();
}
}
};

public void loadMsg()
{
ParseUser par=room.getParseUser("unreadBy");
try{
par.fetchIfNeeded();
currentUser.fetchIfNeeded();
}catch (Exception e){}
if(par!=null)
{
if(par.getObjectId().equals(currentUser.getObjectId()))
{
room.put("unread",0);
room.saveInBackground(null);
}
}

final ParseQuery<ParseObject> msgQ=new ParseQuery("Messages");
msgQ.whereEqualTo("match",room);
msgQ.orderByAscending("createdAt");
msgQ.setLimit(500);
msgQ.include("user");
msgQ.findInBackground(new FindCallback<ParseObject>() {
@Override
public void done(List<ParseObject> objects, ParseException e) {
if(e==null) {
Log.d(tag, "Messges--" + objects.size());
msgObj = objects;
if(msgObj.size()>0) {
empty.setVisibility(View.INVISIBLE);
try {
adapter = new AwesomeAdapter(getApplicationContext(), msgObj, currentUser, selfbit, incbit);
lv.setAdapter(adapter);
lv.setSelection(msgObj.size() - 1);
}catch (Exception eg){}
}

}else{
Log.d(tag, "eror--" + e);
}
}
});
}

Manifest.xml

<uses-permission android:name="com.android.vending.CHECK_LICENSE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="com.android.vending.BILLING" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" /> <permission android:protectionLevel="signature" android:name="com.package.name.permission.C2D_MESSAGE" />
<uses-permission android:name="com.package.name.permission.C2D_MESSAGE" />

    <receiver android:name="com.parse.ParseBroadcastReceiver" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<action android:name="android.intent.action.RECEIVE_BOOT_COMPLETED" />
<action android:name="android.intent.action.USER_PRESENT" />
</intent-filter>
</receiver>

<receiver android:name=".Supported.MyReceiver" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<action android:name="android.intent.action.USER_PRESENT" />
<action android:name="com.package.name.UPDATE_STATUS" />
</intent-filter>
</receiver>

<receiver android:name="com.parse.GcmBroadcastReceiver"
android:permission="com.google.android.c2dm.permission.SEND">
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
<action android:name="com.google.android.c2dm.intent.REGISTRATION" />

<!--
IMPORTANT: Change "com.example.app" to match your app's package name.
-->
<category android:name="com.package.name" />
</intent-filter>
</receiver>

知道我做错了什么吗?

最佳答案

尝试改变

    if (intent.getAction().equals("test")) {
// Do stuff
loadMsg();
}

    if (intent.getAction().equals("test")) {
// Do stuff
runOnUiThread(new Runnable() {
@Override
public void run() {
loadMsg();
}
});
}

关于java - BroadcastReceiver onReceive 用于聊天功能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34297757/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com