Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to Call a Class (with parameters) into Another Class in Android

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 64
    Answer it

    Android Studio:

     

    Hi everyone. How's it going?

    I'm having trouble with calling the Matrix class into the Main Activity.

    Basically, when the lock button is pressed, the matrix class should run/be active.

    I'm not sure if I should use the resizable() method or create a new intent.

     

    All other parts of the code appears to be working fine.

    Please help!

    Thanks For Understanding.

     

     

    MainActivity class:

    import android.app.Activity;
    import android.app.admin.DevicePolicyManager;
    import android.content.ComponentName;
    import android.content.Context;
    import android.content.Intent;
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.Button;
    import android.widget.Toast;
    
    
    public class MainActivity extends AppCompatActivity
    {
    
        Button b_enable, b_lock;
    
        static final int RESULT_ENABLE = 1;
    
        DevicePolicyManager devicePolicyManager;
    
        ComponentName componentName;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            b_enable = (Button) findViewById(R.id.b_enable);
    
            b_lock = (Button) findViewById(R.id.b_lock);
    
            devicePolicyManager = (DevicePolicyManager) getSystemService(Context.DEVICE_POLICY_SERVICE);
            componentName = new ComponentName(MainActivity.this, Controller.class);
    
            boolean active = devicePolicyManager.isAdminActive(componentName);
    
            if(active)
            {
                b_enable.setText("DISABLE");
                b_lock.setVisibility(View.VISIBLE);
    
    
            }
            else
            {
                b_enable.setText("ENABLE");
                b_lock.setVisibility(View.GONE);
    
            }
    
            b_enable.setOnClickListener(new View.OnClickListener()
            {
                @Override
                public void onClick(View view)
                {
                    boolean active = devicePolicyManager.isAdminActive(componentName);
    
                    if (active)
                    {
                       devicePolicyManager.removeActiveAdmin(componentName);
                        b_enable.setText("ENABLE");
                       b_lock.setVisibility(View.GONE);
    
    
                    }
                    else
                    {
                        Intent intent = new Intent(DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN);
                        intent.putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN, componentName);
                        intent.putExtra(DevicePolicyManager.EXTRA_ADD_EXPLANATION, "Allowing Device Admin Is Required!");
                        startActivityForResult(intent, RESULT_ENABLE);
    
                    }
                }
            });
    
            b_lock.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view)
                {
                    devicePolicyManager.lockNow();
                }
            });
    
    
    
        }
    
    
        @Override
        protected void onActivityResult(int requestCode, int resultCode, Intent data)
        {
            switch (requestCode)
            {
                case RESULT_ENABLE:
    
                if (resultCode == Activity.RESULT_OK)
                {
                    b_enable.setText("DISABLE");
                    b_lock.setVisibility(View.VISIBLE);
                }
                else
                {
                    Toast.makeText(this, "FAILED!", Toast.LENGTH_SHORT).show();
                }
                return;
            }
            super.onActivityResult(requestCode, resultCode, data);
        }
    }
    

     

    Matrix class:

    import android.content.Context;
    import android.graphics.Bitmap;
    import android.graphics.Canvas;
    import android.graphics.Color;
    import android.graphics.Paint;
    import android.util.AttributeSet;
    import android.view.View;
    
    import java.io.Serializable;
    import java.util.Random;
    
    public class Matrix extends View
    {
        private static final Random random = new Random();
        private int width, height;
        private Canvas canvas;
        private Bitmap canvasBnp;
        private int fontSize = 15;
        private int columnSize;
        private char[] chars = "!@#$%^&*()_-+=|[]{}<>?/.,;:'~`".toCharArray();
        private int [] txtPostByColumn;
        private Paint paintTxt, paintBg, paintBgBmp, paintInitBg;
    
    
    
        public Matrix (Context context, AttributeSet attrs)
        {
            super(context, attrs);
            paintTxt = new Paint();
            paintTxt.setStyle(Paint.Style.FILL);
            paintTxt.setColor(Color.BLUE);
            paintTxt.setTextSize(15);
    
            paintBg = new Paint();
            paintBg.setStyle(Paint.Style.FILL);
            paintBg.setColor(Color.BLACK);
            paintBg.setAlpha(5);
    
            paintBgBmp = new Paint();
            paintBgBmp.setColor(Color.BLACK);
    
            paintInitBg = new Paint();
            paintInitBg.setColor(Color.BLACK);
            paintInitBg.setAlpha(255);
            paintInitBg.setStyle(Paint.Style.FILL);
    
    
        }
    
    
    
        @Override
        protected void onSizeChanged (int w, int h, int oldw, int oldh)
        {
    
            super.onSizeChanged(w, h, oldw, oldh);
            width = w;
            height = h;
    
            canvasBnp = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
            canvas = new Canvas(canvasBnp);
            canvas.drawRect(0, 0, width, height, paintInitBg);
            columnSize = width/fontSize;
    
    
            txtPostByColumn = new int[columnSize + 1];
    
    
            for ( int y = 0; y < columnSize; y++)
            {
                txtPostByColumn[y] = random.nextInt(height/2) + 1;
    
            }
    
        }
    
    
    
        private void drawText()
        {
            for (int i = 0; i < txtPostByColumn.length; i++)
            {
                canvas.drawText("" + chars[random.nextInt(chars.length)], i * fontSize, txtPostByColumn[i] * fontSize, paintTxt);
    
                if (txtPostByColumn[i] * fontSize > height && Math.random() > 0.975)
                {
                    txtPostByColumn[i] = 0;
    
                }
    
                txtPostByColumn[i]++;
    
    
             }
    
        }
    
    
        private void drawCanvas()
        {
            canvas.drawRect(0, 0, width, height, paintBg);
            drawText();
        }
    
    
    
        @Override
    
        protected void onDraw(Canvas canvas)
        {
            super.onDraw(canvas);
            canvas.drawBitmap(canvasBnp, 0, 0, paintBgBmp);
            drawCanvas();
            invalidate();
        }
    
    
    
    
    
    
    }
    

 0 Answer(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: