Service API22中的Android KeyEvent

时间:2017-03-04 12:35:14

标签: java android service background-process

以朋友的名义询问,因为我已经拥有代表帐户。

“当我按下音量调高按钮并按下音量调低按钮时禁用它时,我有一个小项目应该激活手机的手电筒。这样我就可以轻松访问手电筒了戴手套。我的问题是应用程序应该在后台运行作为服务,并在屏幕关闭时工作,这是不可能的......或者看起来如此。“

我们尝试了什么:

  • 背景活动
  • 媒体会议
  • 编译SDL C ++并从那里轮询密钥
  • Broadcast Reciever

但这些都没有奏效。

以下是服务文件的当前版本:

//imports
public class FlashService extends Service {

public static final String BROADCAST = "android.intent.action.MEDIA_BUTTON";
private static final String TAG = "OutdoorFlashlightService";

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    return Service.START_REDELIVER_INTENT;
}

@Override
public IBinder onBind(Intent intent) {
    // TODO Auto-generated method stub
    return null;
}

@SuppressWarnings("deprecation")
@Override
public void onCreate() {
    // TODO Auto-generated method stub
    super.onCreate();

    Log.w(TAG, "Service started!");

    MediaSession _mediaSession = new MediaSession(getApplicationContext(), TAG);

    if (_mediaSession == null) {
        Log.e(TAG, "initMediaSession: _mediaSession = null");
        return;
    }

    Token _mediaSessionToken = _mediaSession.getSessionToken();

    _mediaSession.setCallback(new Callback() {
        public boolean onMediaButtonEvent(Intent mediaButtonIntent) {
            Log.d(TAG, "onMediaButtonEvent called: " + mediaButtonIntent);
            return super.onMediaButtonEvent(mediaButtonIntent);
        }

        public void onPause() {
            Log.d(TAG, "onPause called (media button pressed)");
            super.onPause();
        }

        public void onPlay() {
            Log.d(TAG, "onPlay called (media button pressed)");
            super.onPlay();
        }

        public void onStop() {
            Log.d(TAG, "onStop called (media button pressed)");
            super.onStop();
        }
    });

    _mediaSession.setFlags(MediaSession.FLAG_HANDLES_MEDIA_BUTTONS | MediaSession.FLAG_HANDLES_TRANSPORT_CONTROLS);

    PlaybackState state = new PlaybackState.Builder().setActions(PlaybackState.ACTION_PLAY)
            .setState(PlaybackState.STATE_STOPPED, PlaybackState.PLAYBACK_POSITION_UNKNOWN, 0).build();

    _mediaSession.setPlaybackState(state);

    _mediaSession.setActive(true);
}
}

这是对BroadcastReciever失败的尝试,从未实际收到任何东西:

package de.platzhirsch.outdoorflashlight;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.hardware.Camera;
import android.util.Log;
import android.view.KeyEvent;

@SuppressWarnings("deprecation")

public class MyBroadcastReceiver extends BroadcastReceiver {

    private Camera cam;
    private boolean lighting = false;

    public MyBroadcastReceiver() {
        super();
        Log.w("", "Receiver registered!");
    }

    @Override
    public void onReceive(Context context, Intent intent) {
        // TODO Auto-generated method stub
        Log.w("", "Got msg!");

        String intentAction = intent.getAction();
        KeyEvent event = null;
        if (Intent.ACTION_MEDIA_BUTTON.equals(intentAction)) {
            event = (KeyEvent) intent.getParcelableExtra(Intent.EXTRA_KEY_EVENT);
        }
        if (event == null) {
            return;
        }

        Log.w("", "triggered...");

        int keycode = event.getKeyCode();
        int action = event.getAction();
        long eventtime = event.getEventTime();

        if (event.getAction() == KeyEvent.ACTION_DOWN) {
            Camera.Parameters parameters;
            if (cam == null) {
                cam = Camera.open();
            }
            switch (keycode) {
            case KeyEvent.KEYCODE_VOLUME_UP:
                if (!lighting) {
                    parameters = cam.getParameters();
                    parameters.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH);
                    cam.setParameters(parameters);
                    cam.startPreview();
                    lighting = true;
                }
                break;
            case KeyEvent.KEYCODE_VOLUME_DOWN:
                if (lighting) {
                    parameters = cam.getParameters();
                    parameters.setFlashMode(Camera.Parameters.FLASH_MODE_OFF);
                    cam.setParameters(parameters);
                    cam.stopPreview();
                    cam.release();
                    cam = null;
                    lighting = false;
                }
                break;
            }
        }
    }
}

我们正在寻找的只是将Volume KeyEvents提供给服务的方法

注意:该应用程序仅用于在我朋友的设备上运行,因此如果所有其他方法都失败,则具有硬件ID的脏设备特定解决方案就足够了

0 个答案:

没有答案