晚上好!
我服务方面的问题很小。起初,我有几个TextViews的片段。在这些TextView中,我想从我的服务中添加文本。这个简单的服务代码
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());
}
}
}
我需要将 练习 的发送清单发送到我的片段,我该如何制作? 我是服务新手,我没有任何想法,请帮助我
答案 0 :(得分:3)
只需从您的服务广播结果,然后通过广播接收器
将其接收到您的片段中例如,您可以在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);
}
但是,您的练习对象需要parcelable才能顺利运行。或者您可以只播放原始的json字符串并在片段中调用ParseJSON.ChallengeParseJSON(strJson);
然后在你的片段中创建一个接收器
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));
}
};
在您的onResume
中 @Override
protected void onResume() {
super.onResume();
getActivity().registerReceiver(receiver, new IntentFilter(FILTER));
}
您还需要使用getActivity().unregisterReceiver(receiver);
如果感兴趣,您还可以阅读更多相关信息here
答案 1 :(得分:0)
你可以通过2种方法来实现。