gpt4 book ai didi

android - 设置自定义调用 Activity

转载 作者:行者123 更新时间:2023-11-30 00:13:23 25 4
gpt4 key购买 nike

我想在通话屏幕前设置我自己的 Activity 。我已经看到有很多这样的例子,但是使用旧版本的 android,而我希望它可以与 android 6.0 及更高版本一起使用。这意味着我必须处理权限。我设法授予了必要的权限。之后,我创建了一个继承 BroadcastReceiver 的类,以便我可以检测电话何时响铃,唯一的问题是我无法在调用显示前发送我的 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();
}
}
}

关于android - 设置自定义调用 Activity ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47759015/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com