我有应用程序,我使用OverlayService绘制“allways-on-top”图标。当我点击这个图标时,我想显示对话框(自定义视图)。问题是,当应用程序暂停时 - 当我点击图标时没有任何反应,因为应用程序没有响应事件,暂停时。你可以帮帮我吗?谢谢。
public class OverlayService extends Service {
private WindowManager wm;
private ImageView imageView;
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onCreate() {
super.onCreate();
imageView = new ImageView(this);
imageView.setImageResource(R.drawable.ic_launcher);
wm = (WindowManager) getSystemService(WINDOW_SERVICE);
final WindowManager.LayoutParams params = new WindowManager.LayoutParams();
params.width = ViewGroup.LayoutParams.WRAP_CONTENT;
params.height = ViewGroup.LayoutParams.WRAP_CONTENT;
params.type = WindowManager.LayoutParams.TYPE_PRIORITY_PHONE;
params.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE |
WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS |
WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL;
params.format = PixelFormat.TRANSLUCENT;
params.gravity = Gravity.TOP | Gravity.RIGHT;
params.x = 0;
params.y = 100;
sendResultMessage( Constant.FAB_ICON_ONCLICK, "FAB icon onclicked.");
imageView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
Log.d("OverlayService", "click");
sendResultMessage( Constant.FAB_ICON_ONCLICK, "FAB icon onclicked.");
}
});
imageView.setOnTouchListener(new View.OnTouchListener() {
private int initialX;
private int initialY;
private float initialTouchX;
private float initialTouchY;
@Override public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
initialX = params.x;
initialY = params.y;
initialTouchX = event.getRawX();
initialTouchY = event.getRawY();
return true;
case MotionEvent.ACTION_UP:
imageView.performClick();
return true;
case MotionEvent.ACTION_MOVE:
params.x = initialX - (int) (event.getRawX() - initialTouchX);
params.y = initialY + (int) (event.getRawY() - initialTouchY);
wm.updateViewLayout(imageView, params);
return true;
}
return false;
}
});
wm.addView(imageView, params);
}
@Override
public void onDestroy() {
super.onDestroy();
removeView();
}
private void removeView() {
if (imageView != null) {
wm.removeView(imageView);
}
}
private void sendResultMessage(int cmd, String data) {
Log.d("sender", "Broadcasting result message: " + data);
Intent intent = new Intent(Constant.FILTER);
intent.putExtra(Constant.COMMAND, cmd);
intent.putExtra(Constant.DATA, data);
LocalBroadcastManager.getInstance(this).sendBroadcast(intent);
}
}
public class StepsFragment extends DialogFragment {
public static String LOG_TAG = "StepsFragment";
private TextView stepsText;
Button closeButton;
Button prevButton;
Button nextbutton;
Button stopRecordingFromStepsFragment;
ArrayList<String> tasks = new ArrayList<String>();
TextView currentStepTextView;
public StepsFragment() {
// Empty constructor is required for DialogFragment
// Make sure not to add arguments to the constructor
// Use `newInstance` instead as shown below
}
public static StepsFragment newInstance(String title) {
StepsFragment frag = new StepsFragment();
Bundle args = new Bundle();
args.putString(RecordingActivity.TEST_TITLE, title);
frag.setArguments(args);
return frag;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.steps_fragment, container);
closeButton = (Button) rootView.findViewById(R.id.closeButton);
prevButton = (Button) rootView.findViewById(R.id.prevButton);
nextbutton = (Button) rootView.findViewById(R.id.nextButton);
stopRecordingFromStepsFragment = (Button) rootView.findViewById(R.id.stopRecordingFromStepsFragment);
currentStepTextView = (TextView) rootView.findViewById(R.id.currentStepTextView);
Bundle bundle = this.getArguments();
if (bundle != null) {
tasks = bundle.getStringArrayList(WebViewFragment.TASKS_API_RESPONSE);
if(tasks == null){
tasks = new ArrayList<String>();
}
}
closeButton.setOnClickListener(new Button.OnClickListener(){
@Override
public void onClick(View v) {
final View decorView = getDialog()
.getWindow()
.getDecorView();
ObjectAnimator scaleDown = ObjectAnimator.ofPropertyValuesHolder(decorView,
PropertyValuesHolder.ofFloat("scaleX", 1.0f, 0.0f),
PropertyValuesHolder.ofFloat("scaleY", 1.0f, 0.0f),
PropertyValuesHolder.ofFloat("pivotX", 900f),
PropertyValuesHolder.ofFloat("pivotY", 150f),
PropertyValuesHolder.ofFloat("alpha", 1.0f, 0.0f)
);
scaleDown.addListener(new Animator.AnimatorListener() {
@Override
public void onAnimationEnd(Animator animation) {
dismiss();
}
@Override
public void onAnimationStart(Animator animation) {
}
@Override
public void onAnimationCancel(Animator animation) {
}
@Override
public void onAnimationRepeat(Animator animation) {
}
});
scaleDown.setDuration(1000);
scaleDown.start();
}});
prevButton.setOnClickListener(new Button.OnClickListener(){
@Override
public void onClick(View v) {
onPrevButtonClicked(v);
}});
nextbutton.setOnClickListener(new Button.OnClickListener(){
@Override
public void onClick(View v) {
onNextButtonClicked(v);
}});
stopRecordingFromStepsFragment.setOnClickListener(new Button.OnClickListener(){
@Override
public void onClick(View v) {
onStopButtonClicked();
}});
initializeStopBtnInsteadOfNext();
return rootView;
}
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
currentStepTextView.setText( currentStepStringValue(WebViewFragment.stepsIndex , tasks.size()));
// Get field from view
stepsText = (TextView) view.findViewById(R.id.stepsText);
stepsText.setMovementMethod(new ScrollingMovementMethod());
setStepsText();
// Fetch arguments from bundle and set title
String title = getArguments().getString(RecordingActivity.TEST_TITLE, "");
getDialog().setTitle(title);
// Show soft keyboard automatically and request focus to field
stepsText.requestFocus();
getDialog().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);
}
private String currentStepStringValue(int stepsIndex, int tasksSize){
return "Step " + (stepsIndex + 1) + " out of " + tasksSize;
}
@Override
public void show(FragmentManager manager, String tag) {
try {
FragmentTransaction ft = manager.beginTransaction();
ft.add(this, tag);
ft.commit();
} catch (IllegalStateException e) {
Log.d(LOG_TAG, "Exception", e);
}
}
private void setStepsText(){
if (WebViewFragment.stepsIndex < tasks.size() ){
String taskDesc = tasks.get(WebViewFragment.stepsIndex);
stepsText.setText(taskDesc);
}
}
public void onCloseButtonCliked(View v){
dismiss();
}
public void onPrevButtonClicked(View v){
if (WebViewFragment.stepsIndex> 0){
WebViewFragment.stepsIndex -=1;
setStepsText();
currentStepTextView.setText( currentStepStringValue(WebViewFragment.stepsIndex , tasks.size()));
nextbutton.setVisibility(View.VISIBLE);
stopRecordingFromStepsFragment.setVisibility(View.GONE);
}
}
public void onNextButtonClicked(View v){
if (WebViewFragment.stepsIndex< this.tasks.size()){
WebViewFragment.stepsIndex +=1;
setStepsText();
currentStepTextView.setText( currentStepStringValue(WebViewFragment.stepsIndex , tasks.size()));
initializeStopBtnInsteadOfNext();
}
}
private void initializeStopBtnInsteadOfNext(){
if (WebViewFragment.stepsIndex == this.tasks.size() - 1){
nextbutton.setVisibility(View.GONE);
stopRecordingFromStepsFragment.setVisibility(View.VISIBLE);
}
}
private void onStopButtonClicked(){
if (WebViewFragment.stepsIndex == this.tasks.size() - 1 ){
WebViewFragment.myChronometer.stop();
callStopInRecordingActivity();
}
else {
Toast.makeText(getActivity().getBaseContext(), R.string.not_completed_all_steps , Toast.LENGTH_LONG).show();
}
}
private void callStopInRecordingActivity(){
Intent intent = new Intent(getActivity(), RecordingActivity.class);
intent.putExtra(WebViewFragment.CALL_ON_STOP_RECORDING, WebViewFragment.CALL_ON_STOP_RECORDING);
intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
startActivity(intent);
}
public void onResume() {
// Store access variables for window and blank point
Window window = getDialog().getWindow();
Point size = new Point();
// Store dimensions of the screen in `size`
Display display = window.getWindowManager().getDefaultDisplay();
display.getSize(size);
// Set the width of the dialog proportional to 85% of the screen width
window.setLayout((int) (size.x * 0.85), (int) (size.y * 0.85));
window.setGravity(Gravity.CENTER);
// Call super onResume after sizing
super.onResume();
}
@Override
public void onStart() {
super.onStart();
final View decorView = getDialog()
.getWindow()
.getDecorView();
ObjectAnimator scaleDown = ObjectAnimator.ofPropertyValuesHolder(decorView,
PropertyValuesHolder.ofFloat("scaleX", 0.0f, 1.0f),
PropertyValuesHolder.ofFloat("scaleY", 0.0f, 1.0f),
PropertyValuesHolder.ofFloat("alpha", 0.0f, 1.0f));
scaleDown.setDuration(1000);
scaleDown.start();
}
}
public class WebViewActivity extends MainActivity {
boolean showOverlayService = false;
public final static int REQUEST_CODE = 15;
public static String LOG_TAG = "WebViewActivity";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_empty);
super.onCreateDrawer();
showOverlayService = getIntent().getBooleanExtra(RecordingActivity.RUN_OVERLAY_SERVICE, false);
if (showOverlayService){
if (Build.VERSION.SDK_INT >= 23) {
checkDrawOverlayPermission();
}
else {
startOverlayService();
}
}
Fragment fragment = new WebViewFragment();
fragment.setArguments(getIntent().getExtras());
FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager.beginTransaction().replace(R.id.main_fragment_container, fragment).commit();
}
@Override
protected void onStart() {
super.onStart();
Log.e(LOG_TAG, "onStart()");
LocalBroadcastManager.getInstance(this).registerReceiver((mMessageReceiver), new IntentFilter(Constant.FILTER));
// The activity is about to become visible.
}
@Override
protected void onStop() {
super.onStop();
Log.e(LOG_TAG, "onStop()");
LocalBroadcastManager.getInstance(this).unregisterReceiver(mMessageReceiver);
// The activity is no longer visible (it is now "stopped")
}
@Override
public void onBackPressed() {
// intentionally blank
}
@Override
public void onSaveInstanceState( Bundle outState ) {
}
private void checkDrawOverlayPermission() {
/** check if we already have permission to draw over other apps */
if (!Settings.canDrawOverlays(this)) {
/** if not construct intent to request permission */
Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION,
Uri.parse("package:" + getPackageName()));
/** request permission via start activity for result */
startActivityForResult(intent, REQUEST_CODE);
}
startOverlayService();
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
/** check if received result code
is equal our requested code for draw permission */
if (requestCode == REQUEST_CODE) {
if (Settings.canDrawOverlays(this)) {
// continue here - permission was granted
Log.d(LOG_TAG, "startOverlayService(");
startOverlayService();
}
}
}
private void startOverlayService(){
Intent intent = new Intent(this, OverlayService.class);
intent.putExtras(getIntent().getExtras());
startService(intent);
}
private void onFabClick(){
FragmentManager fm = getSupportFragmentManager();
Bundle bundle = new Bundle();
bundle.putStringArrayList(WebViewFragment.TASKS_API_RESPONSE, getIntent().getStringArrayListExtra(RecordingActivity.TEST_TASKS));
StepsFragment stepsFragment = StepsFragment.newInstance(getIntent().getStringExtra(RecordingActivity.TEST_TITLE));
stepsFragment.setArguments(bundle);
stepsFragment.show(fm, "steps_fragment");
}
/*
* SERVICE COMMUNICATORS
*/
private BroadcastReceiver mMessageReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
Log.d(LOG_TAG, "Got message");
handleMessage(intent);
}
};
/*
* Handle message from services
*/
private void handleMessage(Intent msg) {
Bundle data = msg.getExtras();
switch (data.getInt(Constant.COMMAND, 0)) {
case Constant.FAB_ICON_ONCLICK:
onFabClick();
break;
default:
break;
}
}
}
答案 0 :(得分:0)
您也可以使用BroadcastReceiver并确保注册它! 然后,如果您使用对话框将以下代码复制到对话框代码中。
yourDialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
将权限添加到 manifest.xml
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
不要忘记实现代码来检查android 6.0.1的权限。