如何在android中使用showcase-view使用Service?

时间:2016-09-08 08:57:16

标签: java android

我开发了一个应用程序。我使用从StartActivity类开始的服务加载所有内容。我想在我加载的内容中设置showcaseview,我正在使用this github demo lib进行showcaseview。

请任何人都可以帮助我,提前谢谢,帮助是值得的。

问题: - 当传递活动上下文然后它是null或我不确定如何从服务类传递活动上下文。

当我的服务开始时,ui就是这样 screen short

StartActivity.java

public class StartActivity extends Activity {

    private static final String SHOWCASE_ID = "com.zennaxx.screenrecorder";

    private ImageView btnCamera;
    private ImageView btnSetting;
    private ImageView btnAlbum;
    private ImageView btnClose;

    Activity activity;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        initView();
    }

    public void initView()
    {
        setContentView(R.layout.floating_view);

        activity = StartActivity.this;

        btnCamera = (ImageView)findViewById(R.id.btn_camera);
        btnSetting = (ImageView)findViewById(R.id.btn_settings);
        btnAlbum = (ImageView)findViewById(R.id.btn_album);
        btnClose = (ImageView)findViewById(R.id.btn_close);

        startShowcaseView();
    }

    private void startShowcaseView()
    {
        ShowcaseConfig config = new ShowcaseConfig();
        config.setDelay(500); // half second between each showcase view

        MaterialShowcaseSequence sequence = new MaterialShowcaseSequence(activity, SHOWCASE_ID);
        sequence.setOnItemShownListener(new MaterialShowcaseSequence.OnSequenceItemShownListener() {
            @Override
            public void onShow(MaterialShowcaseView itemView, int position) {

                Toast.makeText(itemView.getContext(), "Item #" + position, Toast.LENGTH_SHORT).show();

                if (position == 3) {
                    startService();
                }
            }
        });
        sequence.setConfig(config);
        sequence.addSequenceItem(btnCamera, "This is button one", "GOT IT");
        sequence.addSequenceItem(btnSetting, "This is Setting button ", "GOT IT");
        sequence.addSequenceItem(btnAlbum, "This is Album button ", "GOT IT");
        sequence.addSequenceItem(btnClose, "This is Close button ", "GOT IT");

        Log.i("Ready to Showcaseview", "(^'_'^)");
        sequence.start();
    }

    private void startService()
    {
        startService(new Intent(getApplicationContext(), FloatingViewService.class));
        finish();
    }
}

FloatViewService.java

public class FloatingViewService extends Service {

    private static final String SHOWCASE_ID = "example.showcaseview";
    private WindowManager windowManager;
    private View floatingView;
    private TextView mTextCoolDown;
    private WindowManager.LayoutParams params;

    private ImageView btnCamera;
    private ImageView btnSettings;
    private ImageView btnAlbum;
    private ImageView btnExit;

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

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

        return START_STICKY;
    }

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

        Log.i("onCreate()...", "...called");
        initView();
    }



    private void initView() {

        Log.i("initView()...","...called");
        windowManager = (WindowManager) getSystemService(WINDOW_SERVICE);

        int screen_height = getResources().getDisplayMetrics().heightPixels;

        LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        floatingView = inflater.inflate(R.layout.floating_view, null);

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

        params.gravity = Gravity.TOP | Gravity.LEFT;
        params.x = 0;
        params.y = 100;

        btnCamera = (ImageView) floatingView.findViewById(R.id.btn_camera);
        btnSettings = (ImageView) floatingView.findViewById(R.id.btn_settings);
        btnAlbum = (ImageView) floatingView.findViewById(R.id.btn_album);
        btnExit = (ImageView) floatingView.findViewById(R.id.btn_close);

        btnExit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                stopSelf();
            }
        });


        windowManager.addView(floatingView, params);


    }

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

1 个答案:

答案 0 :(得分:1)

您的代码中的主要问题是您尝试手动创建活动。虽然您可以自己创建一个对象(new StartActivity()),但它不会由系统管理,因此它对Showcase库没用,因为它实际上并不代表应用程序中的任何活动屏幕。

我认为您要为叠加视图创建教程,因此我建议您创建一个transparent activity,您将从该视图中的服务开始,这就是您可以用来显示教程的内容。当然,您需要使用Context.startActivity()正确启动它。