无法使用 onStartCommand 的额外意图

Unable to use intent extra from onStartCommand

我正在尝试制作一个带有服务(浮动小部件)的 Android 应用程序。由于我不是专业程序员,出于很多原因,我没有使用触摸侦听器来更改该小部件的位置。但我希望用户可以选择更改该小部件的位置(仅限某些首选位置),并且该选项将在调用该服务的 activity 中可用。我将数据作为额外的意图从我的 activity 传递到服务,这是从服务 onStartCommand() 获得的。我现在面临的问题 - 我无法在 onCreate() 中使用从 onStartCommand() 获得的数据。似乎服务是在调用 onStartCommand() 之前创建的,并且我收到空指针异常,因为我的一些变量没有从 onStartCommand() 获取值。我已经检查过,当我使用任何可点击对象(例如:按服务中的任何按钮视图)时,这些变量正在获取值,我为此使用了一个虚拟吐司。

请就如何克服这个问题分享您的宝贵意见。

public class MainFloatingWidget extends Service {

    private WindowManager windowManager;
    private View myFloatingView, myCollapsedView, myExpandedView;
    private WindowManager.LayoutParams params;
    private String position;

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }
    @Override
    public void onCreate() {
        super.onCreate();

        myFloatingView = LayoutInflater.from(this).inflate(R.layout.layout_main_floating_widget, null);

        params = new WindowManager.LayoutParams(WindowManager.LayoutParams.WRAP_CONTENT, WindowManager.LayoutParams.WRAP_CONTENT, WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY, WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE, PixelFormat.TRANSLUCENT);

        windowManager = (WindowManager) getSystemService(WINDOW_SERVICE);
        windowManager.addView(myFloatingView, params);

        myCollapsedView = myFloatingView.findViewById(R.id.collapsedViewMainService);

        myExpandedView = myFloatingView.findViewById(R.id.expandedViewMainService);

        myCollapsedView.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                myCollapsedView.setVisibility(View.GONE);
                myExpandedView.setVisibility(View.VISIBLE);
                return true;
            }
        });

        myExpandedView.findViewById(R.id.buttonBack).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                myCollapsedView.setVisibility(View.VISIBLE);
                myExpandedView.setVisibility(View.GONE);
            }
        });

        myExpandedView.findViewById(R.id.buttonClose).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                windowManager.removeView(myFloatingView);
                stopSelf();
            }
        });

        if(position.equals("Right-Middle")){
                params.gravity = Gravity.END | Gravity.CENTER;
            }else if(position.equals("Right-Top")){
                params.gravity = Gravity.END | Gravity.TOP;
            }else if(position.equals("Right-Bottom")){
                params.gravity = Gravity.END | Gravity.BOTTOM;
            }else if(position.equals("Left-Middle")){
                params.gravity = Gravity.START | Gravity.CENTER;
            }else if(position.equals("Left-Top")){
                params.gravity = Gravity.START | Gravity.TOP;
            }else if(position.equals("Left-Bottom")){
                params.gravity = Gravity.START | Gravity.BOTTOM;
            }else{
                params.gravity = Gravity.CENTER;
            }

            windowManager.updateViewLayout(myFloatingView, params);

    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {

       
        position = (String) intent.getExtras().get("POSITION");
        

        return super.onStartCommand(intent, flags, startId);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }

}

onCreate 会在 onStartCommand 之前调用,并且只会在 Service init 时调用一次,所以你应该将你的代码句柄 position 移到 onStartCommand

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {

       params = new WindowManager.LayoutParams(WindowManager.LayoutParams.WRAP_CONTENT, WindowManager.LayoutParams.WRAP_CONTENT, WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY, WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE, PixelFormat.TRANSLUCENT);
        position = (String) intent.getExtras().get("POSITION");
        if(position.equals("Right-Middle")){
                params.gravity = Gravity.END | Gravity.CENTER;
            }else if(position.equals("Right-Top")){
                params.gravity = Gravity.END | Gravity.TOP;
            }else if(position.equals("Right-Bottom")){
                params.gravity = Gravity.END | Gravity.BOTTOM;
            }else if(position.equals("Left-Middle")){
                params.gravity = Gravity.START | Gravity.CENTER;
            }else if(position.equals("Left-Top")){
                params.gravity = Gravity.START | Gravity.TOP;
            }else if(position.equals("Left-Bottom")){
                params.gravity = Gravity.START | Gravity.BOTTOM;
            }else{
                params.gravity = Gravity.CENTER;
            }

        windowManager.updateViewLayout(myFloatingView, params);
        return super.onStartCommand(intent, flags, startId);
    }