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

标签 android android-fragments service android-service

晚上好!
我的服务有一些小问题。首先,我有带有几个 TextView 的 fragment 。我想将我的服务中的文本放入这些 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());
        }
    }
}

我需要将练习列表发送到我的 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);
    }

但是,您的锻炼对象需要是 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/

相关文章:

symfony - get ('doctrine' 之间的区别);和 getDoctrine();

c# - Windows 服务总线 - 多个接收器

windows - 如何使用命令行安装将 Tomcat 7 安装为 Windows 服务

android - 如何使用kotlin显示在recyclerview中选择的单个项目

android - 无法使用导航组件清除所有 fragment 的回栈

android - ImageView 裁剪为多边形

android - 在自定义 DialogFragment 中找不到 fragment ID 的 View

android - 如何在没有 finish() 的情况下从另一个类刷新 RecyclerView

android - Bootstrap Modal 无法在 Android Webview 上正确显示

android - Android Listview项目中的翻转动画