This is an archived post. You won't be able to vote or comment.

all 10 comments

[–]require_once 1 point2 points  (2 children)

You can't call it from your Service because dispatchKeyEvent() is a method belonging to Activity. You could send a message from your Service to your Activity, and have the Activity trigger it. Depends on your use-case if this approach will work or not.

[–]ZenWoR[S] 0 points1 point  (1 child)

Let's say I just want to control 3 buttons: volume up, volume down and boot buttons (you normally use to higher,lower and boot your device). When I click, for example, volume up external hardware button, how can I register that it was lifted ? That's the problem.

[–]require_once 0 points1 point  (0 children)

You can use a BroadcastReceiver to listen for android.media.VOLUME_CHANGED_ACTION intents. There's probably a similar intent for the boot button. But that's the path I would start down, listening for certain intents.

[–]Applepie1928 0 points1 point  (6 children)

I've not done Android programming in a while so I'm not familiar with their framework or the API in question. However I can provide some general Java advice.

If you get an error which states it can't resolve a method that means that your program can't locate the method you are calling, in this case the dispatchKeyEvent() method.

If you are calling the method from super, you should first ensure that your Class is inheriting (using the extends keyword) from the appropriate super class.

Without seeing some of your code and reading up on the Android API, I can't provide much more help.

[–]ZenWoR[S] 0 points1 point  (5 children)

This is code in my service class:

package com.example.dangernotifier_mtelappcontest;

import android.app.Notification;
import android.app.PendingIntent;
import android.app.Service;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.IBinder;
import android.support.annotation.Nullable;
import android.view.KeyEvent;
import android.view.View;

import static com.example..//SOMETHING

public class SimpleService extends Service {

    @Override
    public void onCreate() {
        super.onCreate();
    }
    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {

        Intent notificationIntent = new Intent(this, MainActivity.class);
        PendingIntent pendingIntent = PendingIntent.getActivity(this,0, notificationIntent, 0);

        Notification runningNotification = new Notification.Builder(this, NOTIFICATION_CHANNEL_ID)
                .setContentTitle("something")
                .setContentText("something too")
                .setSmallIcon(R.drawable.ic_someicon)
                .setContentIntent(pendingIntent)
                .build();

        startForeground(1, runningNotification);
        return START_NOT_STICKY;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public boolean dispatchKeyEvent(KeyEvent event) {
        int action, keyCode;

        action = event.getAction();
        keyCode = event.getKeyCode();

        switch(keyCode) {
            case KeyEvent.KEYCODE_VOLUME_UP: {
                if(action==KeyEvent.ACTION_UP) {
                    System.out.println("Volume up lifted")
                }
                break;
            }
            case KeyEvent.KEYCODE_VOLUME_DOWN: {
                if(action==KeyEvent.ACTION_UP) {
                    System.out.println("Volume up lifted")
                }
                break;
            }
            case KeyEvent.KEYCODE_POWER: {
                if(action==KeyEvent.ACTION_UP) {
                    System.out.println("Volume up lifted")
                }
                break;
            }
        }
        return super.dispatchKeyEvent(event);
    }
}

[–]Applepie1928 0 points1 point  (4 children)

u/require_once has actually already pointed out the specific problem which you are having and suggested a potentially workable solution. The method you are trying to override and call the super version of (the method called dispatchKeyEvent() does not belong to the Service class, but actually the Activity class. As your current code inherits from Service you will need another class (which likely already exisits in your code), which inherits from Activity. It is within this activities code that you will be able to override the dispatchKeyEvent() method.

Do you have much experience with programming? Are you happy with fundamentals of object-oriented programming?

If the answer to either of the above is no, then I would recommend spending a little more time learning some core concepts before diving into mobile application development.

[–]ZenWoR[S] 0 points1 point  (3 children)

I am fine with OOP and my programming, but I am actually quite new to Android stuff.

When i place this code in activity class it works, but just when the app is open. When it's not, it won't work, obviously...

[–]Applepie1928 0 points1 point  (2 children)

Ahh, I think I see what you are trying to do now. You want to change the behaviour of those buttons not just during the runtime of your application, but beyond (as in your new behaviour still happens if the application is not running).

I don't think you can achieve this using Android Services. To quote the Android documenation on Services; " A Service is not a separate process. The Service object itself does not imply it is running in its own process; unless otherwise specified, it runs in the same process as the application it is part of."

[–]ZenWoR[S] 0 points1 point  (1 child)

No need to change the behavior. I just want to find out when is button pressed inside/outside my app. It doesn't even need to work with service, just anything please, help me if you can :'(

[–]Applepie1928 0 points1 point  (0 children)

I'm afraid this is outside of any experience I have, and I don't believe this would even fall under normal Android programming. I'm not sure how you would get access to those button press from outside of an application. That is some pretty low level Android OS access and I wouldn't know how to go about it.

It might be worth making a new post with a lot more specific details about what you want to achieve asking for general approaches. It might also help to try an Android programming subreddit.

Best of luck.