我希望对话框和来电屏幕都可以点击。我尝试过这个网站的不同解决方案,但有些在一些条件下有效,有些则没有。我想创建一个类似真正调用者的应用程序,我从BroadcastReceiver
调用了一个活动。当屏幕未锁定时,我的代码工作正常,因为传入的屏幕不是全屏。但是当屏幕全屏显示时,对话框活动会在调用屏幕上显示几毫秒,然后进入调用屏幕。
这是我从BroadcastReceiver调用的活动代码
public class IncomingCallActivity extends AppCompatActivity {
private static final int MSG_ID_CHECK_TOP_ACTIVITY = 1;
private String userName;
private String TAG = IncomingCallActivity.class.getSimpleName();
private Window wind;
private PowerManager powerManager;
private PowerManager.WakeLock wakeLock;
/*private ActivityManager mActivityManager;
private boolean mDismissed = false;*/
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
wind = this.getWindow();
wind.requestFeature(Window.FEATURE_NO_TITLE);
wind.addFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD);
wind.addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED);
wind.addFlags(WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);
wind.addFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL);
powerManager = (PowerManager)getSystemService(Context.POWER_SERVICE);
wakeLock = powerManager.newWakeLock(PowerManager.FULL_WAKE_LOCK, "My Lock");
wakeLock.acquire();
setContentView(R.layout.activity_incoming_call);
userName = getIntent().getStringExtra(IncomingCallReceiver.NAME_KEY);
final TextView textView = (TextView) findViewById(R.id.tvUsername);
textView.setText(userName);
final ImageView ivCancel = (ImageView) findViewById(R.id.ivCancel);
ivCancel.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
wakeLock.release();
IncomingCallActivity.this.finish();
}
});
}
}
答案 0 :(得分:1)
//Add Permissions in Manifest file and don't forget to check overlay permission
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>
<uses-permission android:name="android.permission.SYSTEM_OVERLAY_WINDOW"/>
//Create Class Level Variable or as per requirement
WindowManager.LayoutParams mWindowsParams;
WindowManager mWindowManager;
View mDialogView;
//initialize variable
mWindowsParams =new WindowManager.LayoutParams(WindowManager.LayoutParams.MATCH_PARENT,
WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.TYPE_SYSTEM_ERROR,
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
PixelFormat.TRANSLUCENT);
mWindowManager=(WindowManager)context.getSystemService(Context.WINDOW_SERVICE);
//Add Window on your event
mWindowManager.addView(mDialogView, mWindowsParams);
//For Remove window
if (mDialogView.getParent() != null) {
try {
mWindowManager.removeViewImmediate(mDialogView);
} catch (Exception e) {
}
}