如何以编程方式自动展开浮动小部件

问题描述

我正在尝试向我的应用发送通知。当应用程序在后台运行时收到通知后,它应该启动服务并自动展开我的浮动窗口小部件。从另一个服务启动服务中的功能时遇到问题。下面是我的代码。我正在使用FCM。

我的FCM班

if (title.equals("Request")){
        if (Application.wasInBackground){
        LatLng customerLocation = new Gson().fromJson(remoteMessage.getData().get("location"),LatLng.class);
        LatLng destination = new Gson().fromJson(remoteMessage.getData().get("destination"),LatLng.class);
        String customerKey = remoteMessage.getData().get("customerKey");
        String customer = remoteMessage.getData().get("riderToken");




        Intent intent = new Intent(getBaseContext(),CustomerCall.class);
        intent.putExtra("lat",customerLocation.latitude);
        intent.putExtra("lng",customerLocation.longitude);
        intent.putExtra("destLat",destination.latitude);
        intent.putExtra("destLng",destination.longitude);
        intent.putExtra("customerKey",customerKey);
        intent.putExtra("customer",customer);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_MULTIPLE_TASK);
        startActivity(intent);

       } else {





            if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.Q) {



                LatLng customerLocation = new Gson().fromJson(remoteMessage.getData().get("location"),LatLng.class);
                LatLng destination = new Gson().fromJson(remoteMessage.getData().get("destination"),LatLng.class);
                String customerKey = remoteMessage.getData().get("customerKey");
                String customer = remoteMessage.getData().get("riderToken");
                Intent intent = new Intent(getBaseContext(),RideRequestHeadService.class);


                intent.putExtra("lat",customerLocation.latitude);
                intent.putExtra("lng",customerLocation.longitude);
                intent.putExtra("destLat",destination.latitude);
                intent.putExtra("destLng",destination.longitude);
                intent.putExtra("customerKey",customerKey);
                intent.putExtra("customer",customer);
                startService(intent);


            }




            }

下面的我的浮动窗口小部件服务类

         public class RideRequestHeadService extends Service implements View.OnClickListener {


private WindowManager mWindowManager;
private View mFloatingView;
private View collapsedView;
private View expandedView;

public static String paybill,ref,amount = "";

public RideRequestHeadService() {
}

@Override
public IBinder onBind(Intent intent) {
    return null;
}

@Override
public void onCreate() {
    super.onCreate();


    //getting the widget layout from xml using layout inflater
    mFloatingView = LayoutInflater.from(this).inflate(R.layout.layout_floating_widget,null);

    //setting the layout parameters
    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);


    //getting windows services and adding the floating view to it
    mWindowManager = (WindowManager) getSystemService(WINDOW_SERVICE);
    mWindowManager.addView(mFloatingView,params);


    //getting the collapsed and expanded view from the floating view
    collapsedView = mFloatingView.findViewById(R.id.layoutCollapsed);
    expandedView = mFloatingView.findViewById(R.id.layoutExpanded);

    SharedPreferences myPreference = getSharedPreferences("InukaLoansPreference",MODE_PRIVATE);
    String mobileNumber = myPreference.getString("mobileNumber","");
    // accountNoTextView
 

    //adding click listener to close button and expanded view
    mFloatingView.findViewById(R.id.buttonClose).setOnClickListener(this);
    expandedView.setOnClickListener(this);

    //adding an touchlistener to make drag movement of the floating widget
    mFloatingView.findViewById(R.id.relativeLayoutParent).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:
                    //when the drag is ended switching the state of the widget
                    collapsedView.setVisibility(View.GONE);
                    expandedView.setVisibility(View.VISIBLE);
                    return true;

                case MotionEvent.ACTION_MOVE:
                    //this code is helping the widget to move around the screen with fingers
                    params.x = initialX + (int) (event.getRawX() - initialTouchX);
                    params.y = initialY + (int) (event.getRawY() - initialTouchY);
                    mWindowManager.updateViewLayout(mFloatingView,params);
                    return true;
            }
            return false;
        }
    });
}

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

@Override
public void onClick(View v) {
    switch (v.getId()) {
        case R.id.layoutExpanded:
            //switching views
            collapsedView.setVisibility(View.VISIBLE);
            expandedView.setVisibility(View.GONE);
            break;

        case R.id.buttonClose:
            //closing the widget
            stopSelf();
            break;
    }
}
       }

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...