Действие начинается правильно из службы, но не отображается - PullRequest
0 голосов
/ 02 августа 2020

Доброе утро, я сейчас кодирую приложение android, и мне нужно запустить функцию преобразования речи в текст Google из службы, которая отображает простой значок над другими приложениями. Когда я запускаю приложение и просматриваю журналы, мне кажется, что все в порядке, и намерение запущено. Но ничего не появляется. Вот класс обслуживания и код xml:

package com.e.blue;

import android.app.Service;
import android.content.ActivityNotFoundException;
import android.content.Intent;
import android.graphics.PixelFormat;
import android.net.Uri;
import android.os.IBinder;
import android.speech.RecognizerIntent;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ImageView;

import android.app.Service;
import android.content.Intent;
import android.graphics.PixelFormat;
import android.os.IBinder;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.Toast;

import java.util.Locale;

public class FloatWidgetService extends Service {
        private WindowManager mWindowManager;
        private View mFloatingWidget;
        Button startts;
        public FloatWidgetService() {
        }
        @Override
        public IBinder onBind(Intent intent) {
            return null;
        }
        @Override
        public void onCreate() {
            super.onCreate();
            mFloatingWidget = LayoutInflater.from(this).inflate(R.layout.layout_floating_widget, null);
            final WindowManager.LayoutParams params = new WindowManager.LayoutParams(
                    WindowManager.LayoutParams.WRAP_CONTENT,
                    WindowManager.LayoutParams.WRAP_CONTENT,
                    WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY,
                    WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
                    PixelFormat.TRANSLUCENT);
            params.gravity = Gravity.TOP | Gravity.LEFT;
            params.x = 0;
            params.y = 100;
            mWindowManager = (WindowManager) getSystemService(WINDOW_SERVICE);
            mWindowManager.addView(mFloatingWidget, params);


            final View collapsedView = mFloatingWidget.findViewById(R.id.collapse_view);
            final View expandedView = mFloatingWidget.findViewById(R.id.expanded_container);
            ImageView closeButtonCollapsed = (ImageView) mFloatingWidget.findViewById(R.id.close_btn);
            closeButtonCollapsed.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    stopSelf();
                }
            });
            ImageView closeButton = (ImageView) mFloatingWidget.findViewById(R.id.close_button);
            closeButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    collapsedView.setVisibility(View.VISIBLE);
                    expandedView.setVisibility(View.GONE);
                }
            });
            mFloatingWidget.findViewById(R.id.root_container).setOnTouchListener(new View.OnTouchListener() {
                private int initialX;
                private int initialY;
                private float initialTouchX;
                private float initialTouchY;
                @Override
                public boolean onTouch(View v, MotionEvent event) {
                    switch (event.getAction()) {
                        case MotionEvent.ACTION_DOWN:
                            initialX = params.x;
                            initialY = params.y;
                            initialTouchX = event.getRawX();
                            initialTouchY = event.getRawY();
                            return true;
                        case MotionEvent.ACTION_UP:
                            int Xdiff = (int) (event.getRawX() - initialTouchX);
                            int Ydiff = (int) (event.getRawY() - initialTouchY);
                            if (Xdiff < 10 && Ydiff < 10) {
                                if (isViewCollapsed()) {
                                    collapsedView.setVisibility(View.GONE);
                                    StartSpeechToText();
                                    expandedView.setVisibility(View.VISIBLE);
                                }
                            }
                            return true;
                        case MotionEvent.ACTION_MOVE:
                            params.x = initialX + (int) (event.getRawX() - initialTouchX);
                            params.y = initialY + (int) (event.getRawY() - initialTouchY);
                            mWindowManager.updateViewLayout(mFloatingWidget, params);
                            return true;
                    }


                    return false;
                }

            });


        }
        private boolean isViewCollapsed() {
            return mFloatingWidget == null || mFloatingWidget.findViewById(R.id.collapse_view).getVisibility() == View.VISIBLE;



        }
        @Override
        public void onDestroy() {
            super.onDestroy();
            if (mFloatingWidget != null) mWindowManager.removeView(mFloatingWidget);
        }




    private void StartSpeechToText() {
        Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
        intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, Locale.getDefault());
        intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
        intent.putExtra(RecognizerIntent.EXTRA_PROMPT, "Parlez a Blue !");
        intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); // You need this if starting from a service
        startActivity(intent);

    }
}

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<RelativeLayout
    android:id="@+id/root_container"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    tools:ignore="UselessParent">
    <RelativeLayout
        android:id="@+id/collapse_view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:visibility="visible">
        <ImageView
            android:id="@+id/collapsed_iv"
            android:layout_width="70dp"
            android:layout_height="70dp"
            android:layout_marginTop="8dp"
            android:src="@drawable/diamond"
            tools:ignore="ContentDescription" />
        <ImageView
            android:id="@+id/close_btn"
            android:layout_width="25dp"
            android:layout_height="25dp"
            android:layout_marginLeft="50dp"
            android:src="@drawable/error"
            tools:ignore="ContentDescription" />
    </RelativeLayout>
    <LinearLayout
        android:visibility="gone"
        android:id="@+id/expanded_container"
        android:layout_width="wrap_content"
        android:layout_height="80dp"
        android:background="#00C1E7"
        android:orientation="horizontal"
        android:padding="5dp">
        <ImageView
            android:layout_width="70dp"
            android:layout_height="70dp"
            android:src="@drawable/diamond"
            tools:ignore="ContentDescription" />
        <TextView
            android:lineSpacingExtra="1dp"
            android:layout_width="100dp"
            android:layout_height="match_parent"
            android:gravity="center_vertical"
            android:padding="5dp"
            android:text="Floating Widget contols here"
            android:textColor="@android:color/white"
            android:textSize="14sp"
            android:textStyle="bold" />
        <ImageView
            android:id="@+id/close_button"
            android:layout_width="25dp"
            android:layout_height="25dp"
            android:src="@drawable/error"
            tools:ignore="ContentDescription" />

    </LinearLayout>
</RelativeLayout>
</FrameLayout>

Заранее спасибо!

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...