开发者

Starting an Activity from a service and do not want any key input

开发者 https://www.devze.com 2023-04-04 18:21 出处:网络
I\'m starting an activity from a service, which displays some text. Now if a user presses a key - say a back key, I would like that back button to be processed by any other app which may be running (

I'm starting an activity from a service, which displays some text.

Now if a user presses a key - say a back key, I would like that back button to be processed by any other app which may be running (not started by my service).

Is this possible? I have tried FLAG_NOT_FOCUSABLE, howev开发者_StackOverflow中文版er, the back key is still not processed by any other app/ignored. Android, after some time keep giving a option for "Force Close" or 'Wait".

Any pointers will be helpful.

Just FYI, "whitepages" app does something like this - it shows a dialog window, however, doesn't process the back key.

Thanks.


I'm not sure if this is correct or not, but I've seen the same behavior in a few apps myself. I think they are using Toast messages to display the text. I'm not sure if a service can display Toast or not - you'll have to try... if not then you could also try having the Activity display the toast and immediately exit.


Only the currently running activity receives in the button pressed signal. All you can do is intercept the back button press by implementing:

 onBackPressed()

On older devices this won't quite work and you'll need to do something like:

public boolean onKeyDown(int keyCode, KeyEvent event) 
{
  if (keyCode == KeyEvent.KEYCODE_BACK) {
    //Do something here
    return true;
  }
return super.onKeyDown(keyCode, event);
}

As far as passing the button press to another activity, I'm not sure you can do that. What you could do is pass the activity an intent that could specify the back button was pressed. However, this assumes that the other activity is setup to deal with such an intent.

0

精彩评论

暂无评论...
验证码 换一张
取 消