Need an alternative to com.android.internal.policy.PolicyManager.makeNewWindow(Context context) - android

This code that sets up a floating window for a video view was working great. Until Android 6.0 - it now throws NoClassDefFoundError. Also when migrating the project to Android Studio, gradle refuses to compile the project because of that line. Reading up a bit, I realize it would be unwise to try and copy/paste the android internals source code. How can a get a new window for my Playback Controller?
private void initFloatingWindow() {
mWindowManager = (WindowManager) mActivity.getSystemService(Context.WINDOW_SERVICE);
**// original**
mWindow = com.android.internal.policy.PolicyManager.makeNewWindow(mActivity.getApplicationContext());
// attempt instead of PolicyManager, but it really needs a new window
// mWindow = mActivity.getWindow();
mWindow.setWindowManager(mWindowManager, null, null);
mWindow.requestFeature(Window.FEATURE_NO_TITLE);
mDecor = mWindow.getDecorView();
mDecor.setOnTouchListener(mTouchListener);
mWindow.setContentView(mViewGroup);
mWindow.setBackgroundDrawableResource(android.R.color.transparent);
// While the media controller is up, the volume control keys should
// affect the media stream type
mWindow.setVolumeControlStream(AudioManager.STREAM_MUSIC);
mViewGroup.setFocusable(true);
mViewGroup.setFocusableInTouchMode(true);
mViewGroup.setDescendantFocusability(ViewGroup.FOCUS_AFTER_DESCENDANTS);
mViewGroup.requestFocus();
mViewGroup.setOnKeyListener(new View.OnKeyListener() {
#Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
LLog.d(LOG_TAG, "onKey keyCode=" + keyCode);
if (keyCode == KeyEvent.KEYCODE_BACK
|| keyCode == KeyEvent.KEYCODE_MENU) {
hideControlLayout();
InputMethodManager mInputManager = (InputMethodManager) mActivity
.getSystemService(Context.INPUT_METHOD_SERVICE);
mInputManager.hideSoftInputFromWindow(
mDecor.getWindowToken(), 0);
return true;
}
return false;
}
});
}

Related

How to disbale 1D scanner code on mobile device in android

I am using 1D scanner i.e "https://www.issyzonepos.com/ipda018-android-7-0-pda-bluetooth-4-1-support-gprs-wifi_p326.html" for my app. The app is basically to scan code and order data. I have also implemented a scanner through a camera. My APP is working perfectly on this device but when I try to run it on a mobile device then it crashes on "sm = new ScanDevice();" as it is a non-scanner device. I tried a lot to resolve this can you tell me how can I solve this. I have used this device's SDK for the scanner.
Code:
sm = new ScanDevice();
public void onKeyDown(int keyCode, KeyEvent event) {
int charCode = event.getKeyCode();
System.out.println("charCode = " + keyCode + " " + event);
//sm.openScan();
if (charCode == 302 || charCode == 301 || charCode == 303) {
if (spBar.getSelectedItem().toString().trim().equals("SELECT BAR")) {
Toast.makeText(getActivity(), getResources().getString(R.string.select_a_bar), Toast.LENGTH_SHORT).show();
} else {
InputMethodManager imm = (InputMethodManager) getActivity().getSystemService(
Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(actSearchCode.getApplicationWindowToken(), 0);
System.out.println("openScannerL = " + sm.getOutScanMode());
sm.openScan();
}
} else if (charCode == 82) {
} else {
sm.closeScan();
}
}
For the sake of completeness, here's the stack trace of the error from the OP - unfortunately, as a screenshot.
Line 76 is sm = new ScanDevice();
As I mentioned in my comment, you can just create your scanner instance inside a try-catch block. For this, you'll have to declare the variable in a class, but instantiate it in a method. Your code still doesn't show where you're declaring your sm, but you may end up with something like this:
public class MyScanActivity extends Activity {
ScanDevice sm;
public void onCreate(Bundle bundle) {
try {
...
sm = new ScannerDevice();
...
} catch (Exception ex) {
Log.w("SCANNER_ACTIVITY", "Could not create scanner device - not a scanner?");
}
...
}
public void onKeyDown(int keyCode, KeyEvent event) {
...
if (sm == null) {
Log.i("SCANNER_ACTIVITY", "Scanner not instantiated - not a scanner?");
} else {
// all your scanning code goes here
}
}
}

Android Google Keyboard crashes when capturing KeyEvent.KEYCODE_ENTER

I'm trying to manage the Next event on the Google Keyboard on a Google Nexus 5. I want my application to check user information when the Next button gets pressed.
The code looks like this:
private TextView.OnEditorActionListener GenerateEditorListeners()
{
return new TextView.OnEditorActionListener() {
#Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if(actionId == EditorInfo.IME_NULL && event.getKeyCode() == KeyEvent.KEYCODE_ENTER){
if (!finished)
{
if (CheckUserInfo()) finished = true;
}
return true;
}
else
{
return false;
}
}
};
}
On a Samsung Galaxy S4 works perfect, but on a Google Nexus 5 the actionId = 0 and the event = null. So I figure out that the Samsung keyboard works fine with this code, but doesn't happen the same with the Google Keyboard.
Any idea on why it's not wokring for Google's keyboards?
EDIT: I've read in this post that Google keyboard has a bug in some LatinIME keyboards.... I'm using a latin one. If that's the problem, how to solve it?
I could solve it by using the OnKeyListener event:
textUserEmail.setOnKeyListener(new OnKeyListener() {
#Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
if(event.getKeyCode() == KeyEvent.KEYCODE_ENTER){
if (!finished)
{
if (CheckUserInfo()) finished = true;
}
return true;
}
else
{
return false;
}
}
});
Now it works in all keyboards.

how to add special character when the next line key is pressed?

I want to add a special character when the nextline button(key) is pressed in android mobile.
Plz help me to handle the Android 2.2 keyboard.
Am trying to use this code but its not supported me...
etDetail.addKeyPressHandler(new KeyDownHandler() {
public void onKeyPress(KeyPressEvent event) {
int charCode = event.getUnicodeCharCode();
if (charCode == 0) {
// it's probably Firefox
int keyCode = event.getNativeEvent().getKeyCode();
// beware! keyCode=40 means "down arrow", while charCode=40 means '('
// always check the keyCode against a list of "known to be buggy" codes!
if (keyCode == KeyCodes.KEY_ENTER) {
doOnEnterKeyPressed();
}
} else if (charCode == 13) {
doOnEnterKeyPressed();
}
}
});
What packages are added to do this task or give some ideas
Thanks in advance
Please try this. I hope this code snippet will help you
editText.setOnKeyListener(new OnKeyListener() {
public boolean onKey(View v, int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_ENTER && event.getAction() == KeyEvent.ACTION_DOWN) {
doOnEnterKeyPressed();
}
return true;
}
});

Backwards compatibility of onBackPressed

If I use onBackPressed() on Android 1.5, my application crashes. Is there any possibility to deactivate this function if running on an Android 1.5 device?
The code there is not absolute necessary but a real "nice to have", so I would like to keep it on newer devices and just drop it on older ones.
Is this possible?
edit: I think I found it, just the old way:
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
// do something on back.
return true;
}
return super.onKeyDown(keyCode, event);
}
You can try to detect runtime which version of SDK using your application and depending on that prepare different branches. Like:
#Override
public boolean onKeyDown(int keyCode, KeyEvent event)
{
if(Build.VERSION.SDK_INT==Build.VERSION_CODES.CUPCAKE) //if it's 1.5
{
if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0)
{ // do something on back.
return true;
}
}
return super.onKeyDown(keyCode, event);
}

Making Android game, key input registered weirdly on X10 Mini Pro

Hey, I've just started to learn Android development and ran into a problem.
Controls in the game I'm making work on virtual device, but not on phone:
I have an Xperia X10 Mini Pro
I'm making a basic Pong game to learn droid software development
The game works just fine on my Android virtual device, you can move the paddles up and down smoothly
On my phone I've figured that the onKeyDown event doesn't run until I release the button or after I've held down the button for a second or two, but then it only registers it as a brief press of the button, not like if I was holding it down
I believe that it's a feature of my phone to quickly access special characters, because some keys register different key codes when pressed quickly and when held down
The problems this results in is that I can not move the paddles, but I can do single press things, like pause the game
I can also add:
Game is heavily built around sample app LunarLander from the SDK
Lunar lander works fine in virtual device but has same issues as my pong when put in my phone
How could I force my phone to register onKeyDown events like it should? (Or at least like the virtual device does?)
Code from my view class that extends surfaceView:
#Override
public boolean onKeyDown(int keyCode, KeyEvent msg) {
return thread.doKeyDown(keyCode, msg);
}
#Override
public boolean onKeyUp(int keyCode, KeyEvent msg) {
return thread.doKeyUp(keyCode, msg);
}
Code from within the thread:
boolean doKeyDown(int keyCode, KeyEvent msg) {
synchronized (mSurfaceHolder) {
mP1Score = keyCode;//For debugging
boolean okStart = false;
if (keyCode == KeyEvent.KEYCODE_SPACE) okStart = true;
if (mMode == STATE_PAUSE && okStart) {
setState(STATE_RUNNING);
return true;
} else if (mMode == STATE_READY && okStart) {
setState(STATE_RUNNING);
return true;
} else if (mMode == STATE_GOAL && okStart) {
return true;
}else if (mMode == STATE_END_GAME && okStart) {
doStart();
return true;
} else if (mMode == STATE_RUNNING) {
if (keyCode == KeyEvent.KEYCODE_Q) {
mP1dY = -PLAYER_SPEED;//Player 1 up
return true;
} else if (keyCode == KeyEvent.KEYCODE_A) {
mP1dY = PLAYER_SPEED;//Player 1 down
return true;
} else if (keyCode == KeyEvent.KEYCODE_O) {
mP2dY = -PLAYER_SPEED;//Player 2 up
return true;
} else if (keyCode == KeyEvent.KEYCODE_L) {
mP2dY = PLAYER_SPEED;//Player 2 down;
return true;
} else if (keyCode == KeyEvent.KEYCODE_SPACE) {
pause();//Space is pressed in game
return true;
}
}
return false;
}
}
boolean doKeyUp(int keyCode, KeyEvent msg) {
boolean handled = false;
mP2Score = keyCode;//For debugging
synchronized (mSurfaceHolder) {
if (mMode == STATE_RUNNING) {
if (keyCode == KeyEvent.KEYCODE_Q
|| keyCode == KeyEvent.KEYCODE_A) {
mP1dY = 0;
handled = true;
} else if (keyCode == KeyEvent.KEYCODE_O
|| keyCode == KeyEvent.KEYCODE_L) {
mP2dY = 0;
handled = true;
}
}
}
return handled;
}
Complete source available here
Here's a sort-of-solution:
The problem is SE's default input method on the X10 Mini Pro. By installing a different input method on the phone (HTC_IME in my case) I can get controls to work like they do on the virtual device.
Now I only need to figure out how to make it work with the default input method and I'll have a complete solution.

Categories

Resources