设置自定义调用 activity

Set Custom call activity

我想在通话屏幕前设置我自己的 activity。我已经看到有很多关于此的示例,但是使用旧版本的 android,而我希望它与 android 6.0 及更高版本一起使用。这意味着我必须处理权限。我设法授予了必要的权限。之后我制作了一个继承 BroadcastReceiver 的 Class 以便我可以检测到 phone 何时响铃,唯一的问题是我无法发送我的 activity来电显示。这些是我使用的一些类:

public class PhoneStateReceiver extends BroadcastReceiver {

    public void onReceive(Context context, Intent intent) {
        try {
            System.out.println("Receiver start");
            String state = intent.getStringExtra(TelephonyManager.EXTRA_STATE);
            Toast.makeText(context, " Receiver start ", Toast.LENGTH_SHORT).show();

            if (state.equals(TelephonyManager.EXTRA_STATE_RINGING)) {
                Toast.makeText(context, "Ringing State Number is -", Toast.LENGTH_SHORT).show();
                Intent dialogIntent = new Intent(context, LockActivity.class);
                dialogIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
                context.startActivity(dialogIntent);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

public class LockActivity extends AppCompatActivity {

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_lock_screen);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON
                + WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD |
                +WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED |
                +WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);
        Button btnLock = (Button) findViewById(R.id.btnUnlock);
        final EditText txtPass = (EditText) findViewById(R.id.txtPass);
        btnLock.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                String pass = txtPass.getText().toString();
                if(pass.equals("pass")||pass.equals("пасс")) {
                    finish();
                }else{
                    Toast.makeText(LockActivity.this, "Wrong password!", Toast.LENGTH_SHORT).show();
                }
            }
        });

    }
}

如有其他需要请询问!

我设法解决了,问题是启动内置调用 activity 需要时间,所以我的 activity 先启动,另一个在它之上。因此,我让 activity 的当前线程休眠了不到一秒钟。内置的 activity 已启动,然后我的 activity 在其之上。

 public class PhoneStateReceiver extends BroadcastReceiver {

        public void onReceive(Context context, Intent intent) {
            try {
                System.out.println("Receiver start");
                String state = intent.getStringExtra(TelephonyManager.EXTRA_STATE);
                Toast.makeText(context, " Receiver start ", Toast.LENGTH_SHORT).show();

                if (state.equals(TelephonyManager.EXTRA_STATE_RINGING)) {
                    Toast.makeText(context, "Ringing State Number is -", Toast.LENGTH_SHORT).show();
                    Intent dialogIntent = new Intent(context, LockActivity.class);
                    dialogIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
                    Thread.sleep(700);
                    context.startActivity(dialogIntent);
                }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}