0

I develop one app. I load everything's using service which is start from my StartActivity class. i want to set showcaseview in my loaded content and I am using this github demo lib for showcaseview.

please any one can help me,thanks in advance, help is appreciable.

Problem is :- when is pass the activity context then it's null or i am not sure that how to pass activity context from service class.

when my service start then ui is like this 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);
            }
        }
    }
}
Ninja
  • 678
  • 10
  • 26

1 Answers1

1

The main problem in your code is that you are trying to create an activity manually. While you can create an object yourself (new StartActivity()), it will not be managed by the system, and therefore it is useless to the Showcase library since it doesn't actually represent any active screen in your application.

I take it you want to create a tutorial for the overlay view, so I would suggest creating a transparent activity which you will start from the service over your view, and that is what you can use to show the tutorial. Of course, you will need to start it properly, with Context.startActivity().

Community
  • 1
  • 1
Malcolm
  • 41,014
  • 11
  • 68
  • 91
  • thanks for your reply, please,you have or any demo that how to achieve this type or transparency layout. – Ninja Sep 08 '16 at 09:57
  • @Ninja Unfortunately, no, this is a direction in which you'll have to experiment yourself. The case you have is pretty unusual. – Malcolm Sep 08 '16 at 10:03
  • i achieve to set showcase view as you said in my app, but one problem then last button not show the showcase view, please do some help if possible. – Ninja Sep 08 '16 at 12:07
  • @Ninja I think you'll need to open a separate question for that. You should post the new code there. – Malcolm Sep 08 '16 at 12:15
  • see i change my `StartActivity.java` class when four button showcase view is complete then after start service but last button showcase view is not completed and service stared – Ninja Sep 08 '16 at 12:55
  • @Ninja Like I said, you need to create a separate question. I already answered the original one. – Malcolm Sep 08 '16 at 16:36