将数据从服务发送到Fragment,Android [英] Send data from Service to Fragment, Android
问题描述
晚上好!
我在服务方面遇到了小问题.首先,我有几个TextViews的片段.我要将这些来自我的服务的文本放入这些TextView中.这是我简单的Service
Good evening!
I have small problem with Service. At first, I have fragment with several TextViews. Into these TextView I want put text from my Service. And this code of my simple Service
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());
}
}
}
我需要将 练习 的列表发送到我的片段中,我该如何做? 我是Services的新手,我不知道,请帮助我
And I need send list of exercises to my Fragment, how can I make it? I'm new with Services, and I haven't any idea, please help me
推荐答案
简单地广播服务中的结果,并通过广播接收器以片段形式接收它
Simply broadcast the result from your service and receive it in your fragment through a Broadcast Receiver
例如,您可以像这样在onPostExecute
中广播练习
You could, for example, broadcast your exercises in the onPostExecute
like this
@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对象必须可打包工作顺利.或者,您可以只广播原始的json字符串,然后在片段中调用ParseJSON.ChallengeParseJSON(strJson);
However, Your Exercise object needs to be parcelable for this to work smoothly. Or you could just broadcast your raw json string and call your ParseJSON.ChallengeParseJSON(strJson);
in your fragment
然后在您的Fragment中创建一个接收器
Then in your Fragment you create a receiver
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));
}
};
@Override
protected void onResume() {
super.onResume();
getActivity().registerReceiver(receiver, new IntentFilter(FILTER));
}
您还需要使用getActivity().unregisterReceiver(receiver);
如果有兴趣,还可以在此处了解更多信息
You can also read more about it here if interested
这篇关于将数据从服务发送到Fragment,Android的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!