package com.spacesoftwares.spacecapture.service;
import android.accessibilityservice.AccessibilityService;
import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Build;
import android.support.v4.app.NotificationCompat;
import android.util.Log;
import android.view.KeyEvent;
import android.view.accessibility.AccessibilityEvent;
import com.spacesoftwares.spacecapture.MainActivity;
import com.spacesoftwares.spacecapture.R;
import java.util.Calendar;
public class KeyAccessibilityService extends AccessibilityService {
public static boolean isAccessAlive = false;
private final static int FOREGROUND_ID = 1000;
private Notification mNotification;
private static final String TAG_KEY = "KEY_ACCESS";
@Override
protected boolean onKeyEvent(KeyEvent event) {
System.out.println("onKeyEvent");
Log.i(TAG_KEY, "onKeyEvent");
if(isAccessAlive) {
int key = event.getKeyCode();
switch (key) {
case KeyEvent.KEYCODE_VOLUME_DOWN:
System.out.println("KEYCODE_VOLUME_DOWN");
Log.i(TAG_KEY, "KEYCODE_VOLUME_DOWN");
return true;
case KeyEvent.KEYCODE_VOLUME_UP:
System.out.println("KEYCODE_VOLUME_UP");
Log.i(TAG_KEY, "KEYCODE_VOLUME_UP");
return true;
default:
break;
}
}
return super.onKeyEvent(event);
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
if(null != mNotification){
Log.i(TAG_KEY, "KeyService->onStartCommand->Notification exists");
return super.onStartCommand(intent, flags, startId);
}
Log.i(TAG_KEY, "KeyService->onStartCommand->Create Notification");
//NotificationManager manager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
//NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
String NOTIFICATION_CHANNEL_ID = "my_channel_id_01";
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel notificationChannel = new NotificationChannel(NOTIFICATION_CHANNEL_ID, "My Notifications", NotificationManager.IMPORTANCE_HIGH);
// Configure the notification channel.
notificationChannel.setDescription("Channel description");
notificationChannel.enableLights(true);
notificationChannel.setLightColor(Color.RED);
notificationChannel.setVibrationPattern(new long[]{0, 1000, 500, 1000});
notificationChannel.enableVibration(true);
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.createNotificationChannel(notificationChannel);
}
NotificationCompat.Builder builder = new NotificationCompat.Builder(this, NOTIFICATION_CHANNEL_ID);
builder.setSmallIcon(R.mipmap.ic_launcher);
builder.setContentTitle("Foreground");
builder.setContentText("Text shown on notification bar");
builder.setContentInfo("Content Info");
builder.setWhen(System.currentTimeMillis());
Intent activityIntent = new Intent(this, MainActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 1, activityIntent, PendingIntent.FLAG_UPDATE_CURRENT);
builder.setContentIntent(pendingIntent);
mNotification = builder.build();
startForeground(FOREGROUND_ID, mNotification);
return super.onStartCommand(intent, flags, startId);
}
@Override
protected void onServiceConnected() {
System.out.println("KEYCODE_onServiceConnected");
Log.i(TAG_KEY, "KEYCODE_onServiceConnected");
super.onServiceConnected();
}
@Override
public void onAccessibilityEvent(AccessibilityEvent accessibilityEvent) {
System.out.println("KEYCODE_onAccessibilityEvent");
Log.i(TAG_KEY, "KEYCODE_onAccessibilityEvent");
}
@Override
public void onInterrupt() {
}
}
评论1
最新资源