Tapping / Swiping not detected in Android Studio Google Glass emulator - java

I'm running an emulator for google glass as seen here, works pretty flawlessly by showing settings, main display and even my activity (which I pretend to be an interactive static card).
http://mobilevangelist.com/2014/01/02/gdk-and-the-android-emulator/
I've seen that the motion gestures are captured using onKeyUp or onKeyDown events but neither are working and I don't understand why.
Here is my code.
public class LiveCardMenuActivity extends Activity {
private TextView textView;
#Override //isn't catching a thing, even with onKeyDown (mouse taps or slides in the emulator)
public boolean onKeyUp(int keycode, KeyEvent event){
Log.d("tag","keyUp");
if(keycode == KeyEvent.KEYCODE_DPAD_CENTER){
Log.d("tag","keypadcenter");
textView.setText("tap");
}else if(keycode == KeyEvent.KEYCODE_BACK){
Log.d("tag","swipedown");
textView.setText("down");
}
return true;
}
#Override
public void onAttachedToWindow() {
super.onAttachedToWindow();
setContentView(R.layout.live_card);
//does successfully, I can see the layout in the emulator
//and I can swipe it to the left (returning to the main display successfully)
textView = (TextView) findViewById(R.id.textView);
Log.d("tag","attached to window");
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.live_card, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_stop:
// Stop the service which will unpublish the live card.
stopService(new Intent(this, LiveCardService.class));
return true;
default:
return super.onOptionsItemSelected(item);
}
}
#Override
public void onOptionsMenuClosed(Menu menu) {
super.onOptionsMenuClosed(menu);
// Nothing else to do, finish the Activity.
finish();
}
}
Can someone help me on this one? Tyvm!

Try calling setFocusable(true) on your content view.
Though I don't know much about how this emulator works, this is how you might get this working for standard Android.

You're using onKeyUp() when you need to be using onKeyDown(). Here's an example I'm using in my Glass project (but I have a physical Google Glass, not using an emulator):
public boolean onKeyDown(int keyCode, KeyEvent event) {
switch( keyCode ) {
case KeyEvent.KEYCODE_DPAD_CENTER:
Log.e("GESTURE_EVENT", "PostVideoActivity.onKeyDown() TAP/KEYCODE_DPAD_CENTER");
// DO SOMETHING
return true;
case KeyEvent.KEYCODE_BACK:
Log.e("GESTURE_EVENT", "PostVideoActivity.onKeyDown() SWIPE/KEYCODE_BACK");
// DO SOMETHING
return true;
default:
Log.wtf("GESTURE_EVENT", "PostVideoActivity.onKeyDown() DEFAULT -- SHOULDNT BE HERE!");
return false;
}
}
edit Maybe this is an issue with the emulator? I doubt it though but you can test this theory by creating some dummy Android project for a phone/phone emulator that is known to work. If this snippet doesn't even work for the other Android emulation then maybe its something else?

Related

Android WebView canGoBack() returns false when URL is similar

I have an Android webview that I want it to go back with the Android back button. I have it working if the previous site is, for example https://www.aaa.com/index.php and https://www.aaa.com/index2.php, but canGoBack() returns false when the URLs are for example, https://www.aaa.com/index.php?page=page1 and https://www.aaa.com/index.php?page=page2. I think this is probably because the URLs are the same, it's just the variables at the end that change. This is my code:
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (event.getAction() == KeyEvent.ACTION_DOWN) {
switch (keyCode) {
case KeyEvent.KEYCODE_BACK:
if (view.canGoBack()) {
view.goBack();
} else {
finish();
}
return true;
}
}
return super.onKeyDown(keyCode, event);
}
How can I make it to go back even if the variable on the URL changes?
You are probably being hit by the bug in Chrome version 63. See: https://bugs.chromium.org/p/chromium/issues/detail?id=794020

I want to link my application back button to android mobile builtin back button

I tried many ways but find out that linking my application to the android back button will be easy for my application but didn't find anyway. If anyone can help me out how can I link so when I press the android mobile back icon my application will also move to back category. Working on Android Studio, API 25.
Try override this method:
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
switch (keyCode) {
case KeyEvent.KEYCODE_BACK:
//Your intent
}
return super.onKeyDown(keyCode, event);
}
Or if is your back button from toolbar use this
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
//your intent
return true;
}
return super.onOptionsItemSelected(item);
}

Android: Stop Back Button From Exiting LockTask / Kiosk Mode

I want to implement a KioskMode, I'm targeting only Android L, since this is a very specific App.
I already went through the process of setting my App as DeviceAdmin, and
DevicePolicyManager.isLockTaskPermitted(this.getPackageName()) already returns true.
I then start a LockTask via startLockTask().
Everything is fine, but when I hold down the backbutton, the app still exits the kiosk mode.
I have overridden onKeyPress to show a custom Dialog for unlocking the app, but this does not hinder android to automatically exit my lock task if the user holds down back.
I don't really know what to do at the moment and would be thankful for every input.
I now have overridden
#Override
public boolean onKeyDown(int KeyCode, KeyEvent event)
{
if(KeyCode == KeyEvent.KEYCODE_BACK)
{
BackDownButtonPressed = true;
if(VolDownPressed)
showTaskLockDialog();
return true;
}
else if(KeyCode == KeyEvent.KEYCODE_VOLUME_DOWN)
{
VolDownPressed = true;
if(BackDownButtonPressed)
showTaskLockDialog();
return true;
}
return super.onKeyDown(KeyCode, event);
}
#Override
public boolean onKeyUp(int KeyCode, KeyEvent event) {
if(KeyCode == KeyEvent.KEYCODE_BACK)
{
BackDownButtonPressed = false;
return true;
}
else if(KeyCode == KeyEvent.KEYCODE_VOLUME_DOWN)
{
VolDownPressed = false;
return true;
}
return super.onKeyUp(KeyCode, event);
}
#Override
public void onBackPressed()
{
return;
}
#Override
public boolean onNavigateUp() {
return true;
}
#Override
public boolean dispatchKeyEvent (KeyEvent event)
{
if (event.getKeyCode() == KeyEvent.KEYCODE_BACK) {
return true;
}
return true;
}
#Override
public boolean onKeyLongPress(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
//do something or nothing in your case
return true;
}
return super.onKeyLongPress(keyCode, event);
}
For the record, I am using a Samsung SM-T700 Tablet with Cyanogenmod CM12.1
Just to close this topic..
I couldn't figure out a perfect solution to this day. My current workaround is receiving an event if the user leaves the kiosk mode and just entering the kiosk mode again.
Sadly this leaves the user with 2 toasts saying "screen unpinned" and "screen pinned", which is unfortunate. But this satisfies my current needs.
Perhaps you need to override onKeyLongPress
#Override
public boolean onKeyLongPress(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
//do something or nothing in your case
return true
}
return super.onKeyLongPress(keyCode, event);
}
Not sure if it's helpful at all, but I wrote a blog about setting Kiosk Mode here:
http://www.sureshjoshi.com/mobile/android-kiosk-mode-without-root/
And also wrote sample code for it here:
https://github.com/sureshjoshi/android-kiosk-example
Not sure if you see any major differences between your code and mine, but I just tried to do a long press on a Samsung Galaxy Tab 4 running Android 5.0, and it won't exit the app.
Could it be something with rooting with Cyanogen?
If you don't have this in your code, perhaps add it in and check out if you see any problems:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Remove title bar and notification bar
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
ComponentName deviceAdmin = new ComponentName(this, AdminReceiver.class);
mDpm = (DevicePolicyManager) getSystemService(Context.DEVICE_POLICY_SERVICE);
if (!mDpm.isAdminActive(deviceAdmin)) {
Toast.makeText(this, getString(R.string.not_device_admin), Toast.LENGTH_SHORT).show();
}
if (mDpm.isDeviceOwnerApp(getPackageName())) {
mDpm.setLockTaskPackages(deviceAdmin, new String[]{getPackageName()});
} else {
Toast.makeText(this, getString(R.string.not_device_owner), Toast.LENGTH_SHORT).show();
}
mDecorView = getWindow().getDecorView();
}
and
protected void enableKioskMode(boolean enabled) {
try {
if (enabled) {
if (mDpm.isLockTaskPermitted(this.getPackageName())) {
startLockTask();
mIsKioskEnabled = true;
} else {
Toast.makeText(this, getString(R.string.kiosk_not_permitted), Toast.LENGTH_SHORT).show();
}
} else {
stopLockTask();
mIsKioskEnabled = false;
}
} catch (Exception e) {
// TODO: Log and handle appropriately
}
}
I have the same problem: How to stop "holding the back button" from escaping "Lock Task mode" on Android 6+
The issue only occurs on my Android 7 tablet.
Running the app on my Android 6 tablet fixed the problem.
Could you add some code to illustrate your current work-around?

I need to minimize the android application on back button click

I need to minimize the application when back button is pressed.
I use following code to catch hardware back button click event
help me with the code of minimize on back key pressed
#Override
public boolean onKeyDown(int keyCode, keyEvent event) {
switch(keyCode) {
case KeyEvent.KEYCODE_BACK;
//minimize application
return true;
}
return super.onKeyDown(keyCode, event);
}
I think that you need to treat back event as home event. The code below is how I emulate home pressed when User press back button:
public void minimizeApp() {
Intent startMain = new Intent(Intent.ACTION_MAIN);
startMain.addCategory(Intent.CATEGORY_HOME);
startMain.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(startMain);
}
This is a simple code to minimize the application
#Override
public void onBackPressed() {
this.moveTaskToBack(true);
}
try this code, this will minimize Activity.
public boolean onKeyDown(int keyCode, KeyEvent event)
{
if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0)
{
this.moveTaskToBack(true);
return true;
}
return super.onKeyDown(keyCode, event);
}
or
If you want to close the activity use this.finish() method to close the current running activity. instead of this.moveTaskToBack(true);

Block the home button on android 4

I need to lock the home button in an app, because it will be used by older people and they will not know how to get back if they accidently touch on the home button. I already have that code below but that is not working on android 4.
What I really want is when somebody touches the home button, it does not do anything. Do you have any idea that can help me?
#Override
public void onAttachedToWindow() {
this.getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD);
super.onAttachedToWindow();
}
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.ECLAIR
&& (keyCode == KeyEvent.KEYCODE_BACK || keyCode == KeyEvent.KEYCODE_HOME)
&& event.getRepeatCount() == 0)
{
onBackPressed();
}
return super.onKeyDown(keyCode, event);
}
#Override
public void onBackPressed() {
// Do nothing
return;
}
The only way to do this is to make your app the launcher app on that device, which may not be desirable to the vast majority of Android users, regardless of their age.

Categories

Resources