设置自定义通话活动

时间:2017-12-11 18:22:53

标签: android android-activity broadcastreceiver

我想在呼叫屏幕前设置我自己的活动。我已经看到有很多这样的例子,但有旧版本的android,而我希望它与Android 6.0及以上版本一起使用。这意味着我必须处理权限。我设法授予必要的权限。之后,我创建了一个继承BroadcastReceiver的类,以便我可以检测到手机何时响铃,唯一的问题是我无法在呼叫显示器前面发送我的活动。这些是我使用的一些类:

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();
                }
            }
        });

    }
}

如果还有其他需要,请询问!

1 个答案:

答案 0 :(得分:1)

我设法解决了它,问题是启动内置调用活动需要时间,因此我的活动首先启动,另一个启动它。因此,我让我活动的当前线程睡了不到一秒钟。内置活动已经启动,然后我的活动就开始了。

 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();
        }
    }
}