gpt4 book ai didi

java - FirebaseMessagingService 没有在 AsyncTask 的 doInBackground 中被调用

转载 作者:行者123 更新时间:2023-11-29 02:37:10 26 4
gpt4 key购买 nike

我有推送通知,我想在手机收到通知时更新领域对象,但当我尝试启动时:

RealmModelActiveUser actUser= realm.where(RealmModelActiveUser.class).equalTo("id",1).findFirst();
int myid= actUser.getUser().getUser_id();
new ServerBackgroundDownloadConversations(getApplicationContext()) {
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);

if (!result.equals("Error")) {
Log.i("Conversation", "UPDATED");
}
}
}.execute(myid);

程序跳转到构造函数 ServerBackgroundDownloadConversations(getApplicationContext()) 但是没有调用 doInBackground,我也不知道为什么。

我的异步任务:

public class ServerBackgroundCreateConversation extends AsyncTask<RealmModelConversations,Void,String> {
Context context;

Handler handler;
String out= "";
@SuppressLint("HandlerLeak")
public ServerBackgroundCreateConversation(Context context) {
this.context = context;

handler = new Handler() {
@Override
public void handleMessage(Message msg) {
Bundle bundle= msg.getData();
if (bundle!=null){
out = (String) bundle.get("response");
} else {
out= "Error";
}
}

};
}


@Override
protected String doInBackground(RealmModelConversations... params) {
RealmModelConversations newConv = params[0];
UploadImageApacheHttp uploadTask = new UploadImageApacheHttp();
uploadTask.doFileUpload(newConv.getWork(newConv.getIntWork()), newConv, handler);


while (out.equals("")){
try {
Thread.sleep(50);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
return out;
}

@Override
protected void onPreExecute() {
}

@Override
protected void onPostExecute(String result) {

if (!result.equals("]") || !result.equals("")){
/// prihlási nového user aj do active (login/register)
CreateNewConversation(result);
} else {
result="Error";
}

}

@Override
protected void onProgressUpdate(Void... values) {
super.onProgressUpdate(values);
}


private void CreateNewConversation(String result){
Realm realm= Realm.getDefaultInstance();
try {
Gson gson = new Gson();
Type typeConv = new TypeToken<JsonSablonaConversations>() {
}.getType();

JSONObject pom;
JSONArray parentArray = new JSONArray(result);
JSONObject finalObject = parentArray.getJSONObject(0);

JsonSablonaConversations conversation = gson.fromJson(finalObject.toString(), typeConv);
final RealmModelConversations NewUserConv = new RealmModelConversations();
NewUserConv.setId_dialog(conversation.getId_dialog());
NewUserConv.setDate(conversation.getDate());
NewUserConv.setKey(conversation.getKey());
NewUserConv.setId_user(conversation.getId_user());
NewUserConv.setId_user2(conversation.getId_user2());
NewUserConv.setMeno(conversation.getMeno());
NewUserConv.setMeno2(conversation.getMeno2());
realm.executeTransaction(new Realm.Transaction() {
@Override
public void execute(Realm realm) {
try {
realm.copyToRealmOrUpdate(NewUserConv);
} catch (Exception e) {
int pom=4;
}
RealmResults<RealmModelConversations> ru= realm.where(RealmModelConversations.class).findAll();
}
});
}

catch (Exception e) {
int ppp=4;
ppp++;
}finally {
realm.close();
}
}
}

我尝试从服务调用的外部线程调用这个 ↑,但是我的 AsyncTask 有处理程序,处理程序需要在 runOnUIthread 和 Thread 中。我无法获取 Activity,因为线程是从无权访问 Activity 的服务调用的。

最佳答案

我用这段代码解决了我的问题

    public String postData(int myUserId) {
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://www.gallopshop.eu/OFY/getConversations.php");
try {
// Add your data
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("id", Integer.toString(myUserId)));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
String responseStr = EntityUtils.toString(response.getEntity());
return responseStr;
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
} catch (IOException e) {
// TODO Auto-generated catch block
}
return null;
}

我会尝试将此方法和此方法之后放入简单的线程中,但也许不需要,因为它在服务中。

问题 - 我是否将它放入线程中,或者您认为它会影响应用程序的性能吗?

关于java - FirebaseMessagingService 没有在 AsyncTask 的 doInBackground 中被调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46373712/

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