可以将服务放在CanvasWatchFaceService中吗?

时间:2016-11-07 12:40:11

标签: android wear-os

我如何以及在哪里可以将我的服务放在canvasWatchfaceservice中?我的服务是关于httpurlconnection连接服务器发送邮件请求和响应是图片所以我想在android服装(壁纸更换器应用程序)这样的服务有没有办法,如果有办法,我应该把我的服务代码(我不希望代码放在哪里放我的代码)和另一个问题,如果我的应用程序在设备上工作并随机更改壁纸我可以将它与我的手表配对(当设备壁纸改变手表壁纸也改变)或者我应该创建一个类和一个按钮并且意图它(因为我试过并且不工作)

myasyncTask

private class WallpaperData extends AsyncTask<Void, Void, String> {

    @Override
    protected String doInBackground(Void... params) {

        HttpURLConnection urlConnection = null;
        BufferedReader reader = null;


        try {

            URL url = new URL("http://555.555.555.555/yay/yay.php");

            urlConnection = (HttpURLConnection) url.openConnection();
            urlConnection.setRequestMethod("POST");
            urlConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
            urlConnection.setDoInput(true);
            urlConnection.setDoOutput(true);
            urlConnection.connect();
            DataOutputStream wr = new DataOutputStream(
                    urlConnection.getOutputStream());
            wr.write("method=get_random_wallpaper".getBytes());
            wr.flush();
            wr.close();

            InputStream inputStream = urlConnection.getInputStream();
            StringBuffer buffer = new StringBuffer();
            if (inputStream == null) {
                return null;
            }
            reader = new BufferedReader(new InputStreamReader(inputStream));

            String line;
            while ((line = reader.readLine()) != null) {

                buffer.append(line + "\n");
                Log.d("hey", buffer.toString());

            }

            if (buffer.length() == 0) {
                return null;
            }
            forecastJsonStr = buffer.toString();

            return forecastJsonStr;
        } catch (IOException e) {
            Log.e("PlaceholderFragment", "Error ", e);

            return null;
        } finally {
            if (urlConnection != null) {
                urlConnection.disconnect();
            }
            if (reader != null) {
                try {
                    reader.close();
                } catch (final IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    @Override
    protected void onPostExecute(final String forecastJsonStr) {

        txt.setText(forecastJsonStr);

        Thread thread = new Thread(new Runnable() {

            @Override
            public void run() {

                WallpaperManager wallpaperManager = WallpaperManager.getInstance(getApplicationContext());
                try {
                    Bitmap result = Picasso.with(getBaseContext())
                            .load(hostName + hostWallpaperName + forecastJsonStr)
                            .get();
                    wallpaperManager.setBitmap(result);
                } catch (IOException ex) {
                    ex.printStackTrace();
                }

            }
        });
        thread.start();

        super.onPostExecute(forecastJsonStr);


    }

0 个答案:

没有答案