gpt4 book ai didi

android - 将数据从 Service 发送到 Fragment,Android

转载 作者:搜寻专家 更新时间:2023-11-01 08:42:01 25 4
gpt4 key购买 nike

晚上好!
我的服务有小问题。起初,我有几个 TextViews 的 fragment 。我想在这些 TextView 中放入来 self 的服务的文本。我的简单服务代码

public class ChallengeService extends Service {
final String LOG_TAG = "myLogs";
private String url = "";
public static ArrayList<Exercises> exercises = new ArrayList<>();

public void onCreate() {
super.onCreate();
Log.d(LOG_TAG, "onCreate");
}

public int onStartCommand(Intent intent, int flags, int startId) {
Log.d(LOG_TAG, "onStartCommand");
url = intent.getStringExtra("Challenge_URL");
new getChallenge(url).execute();
return super.onStartCommand(intent, flags, startId);
}

public void onDestroy() {
super.onDestroy();
Log.d(LOG_TAG, "onDestroy");
}

public IBinder onBind(Intent intent) {
Log.d(LOG_TAG, "onBind");
return null;
}

public class getChallenge extends AsyncTask<Void,Void,String> {

String mUrl;
OkHttpClient client = new OkHttpClient();
String result = "";

public getChallenge(String url) {
this.mUrl = url;
}

HttpURLConnection urlConnection = null;
BufferedReader reader = null;

@Override
protected String doInBackground(Void... params) {
Log.d("Background", "inBackground");
try {
URL mUrl = new URL(url);
Log.d("URL!!!!", "url = " + url);
urlConnection = (HttpURLConnection) mUrl.openConnection();
urlConnection.setRequestMethod("GET");
urlConnection.connect();
InputStream inputStream = urlConnection.getInputStream();
StringBuffer buffer = new StringBuffer();
reader = new BufferedReader(new InputStreamReader(inputStream));
String line;
while ((line = reader.readLine()) != null) {
buffer.append(line);
}
result = buffer.toString();

} catch (Exception e) {
e.printStackTrace();
}
return result;
}

@Override
protected void onPostExecute(String strJson) {
super.onPostExecute(strJson);
Log.d(LOG_TAG, strJson + " " + url);
exercises = ParseJSON.ChallengeParseJSON(strJson);
Log.d("Challenges", "challenges: " + exercises.get(0).getName() + " " + exercises.get(1).getName());
}
}
}

我需要将练习 列表发送到我的 Fragment,我该怎么做?我是服务新手,我不知道,请帮助我

最佳答案

简单地广播您的服务的结果并通过广播接收器在您的 fragment 中接收它

例如,您可以像这样在 onPostExecute 中广播您的练习

    @Override
protected void onPostExecute(String strJson) {
super.onPostExecute(strJson);
Log.d(LOG_TAG, strJson + " " + url);
exercises = ParseJSON.ChallengeParseJSON(strJson);
Log.d("Challenges", "challenges: " + exercises.get(0).getName() + " " + exercises.get(1).getName());

Intent intent = new Intent(FILTER); //FILTER is a string to identify this intent
intent.putExtra(MY_KEY, exercises);
sendBroadcast(intent);
}

但是,您的 Exercise 对象需要是 parcelable为了顺利进行。或者你可以广播你的原始 json 字符串并在你的 fragment 中调用你的 ParseJSON.ChallengeParseJSON(strJson);

然后在您的 Fragment 中创建一个接收器

private BroadcastReceiver receiver = new BroadcastReceiver() {

@Override
public void onReceive(Context context, Intent intent) {
ArrayList<Exercises> exercises = intent.getExtras().get(MY_KEY);

//or
//exercises = ParseJSON.ChallengeParseJSON(intent.getStringExtra(MY_KEY));

}
};

register它在你的 onResume

  @Override
protected void onResume() {
super.onResume();
getActivity().registerReceiver(receiver, new IntentFilter(FILTER));
}

您还需要使用 getActivity().unregisterReceiver(receiver);

在您的 onPause 中注销它

您还可以阅读更多相关信息 here如果有兴趣

关于android - 将数据从 Service 发送到 Fragment,Android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31707280/

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