如何解决不是从服务开始的活动?

问题描述 投票:0回答:1

我有一个后台服务,它是一个可访问性服务(不要以为有区别)。我想通过此服务启动一项新活动。我尝试使用此建议的解决方案:

Intent dialogIntent = new Intent(this, ScreenshotActivity.class);
dialogIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
getApplicationContext().startActivity(dialogIntent);

但是,这似乎不起作用。我的logcat提供了此输出,但活动未启动:

020-03-20 20:54:02.824 22977-22977/com.xx.xx I/Timeline: Timeline: Activity_launch_request time:19482980

我的服务和活动独立正常运行。但不是在这种情况下。从我拨打此电话的地点也可以到达。我用于测试的设备正在运行Android 9。

编辑:主要服务代码:

public class MyAccessibility extends AccessibilityService {
public static MyAccessibility instance;
@Override
public void onCreate() {
    super.onCreate();
    Log.i("myLog", "create accessibility");
}

@Override
public void onAccessibilityEvent(AccessibilityEvent accessibilityEvent) {
    Log.i("myLog", "Event");
    if (instance == null) {
        Log.i("myLog", "Instance set to not null");
        instance = this;
    }

}

@Override
public void onInterrupt() {

}

@Override
protected void onServiceConnected() {
    super.onServiceConnected();

    AccessibilityServiceInfo info = new AccessibilityServiceInfo();
    info.eventTypes = AccessibilityEvent.TYPES_ALL_MASK;
    info.notificationTimeout = 100;
    info.feedbackType = AccessibilityServiceInfo.FEEDBACK_ALL_MASK;
    this.setServiceInfo(info);
    Toast t = Toast.makeText(getApplicationContext(), "Accessibility Service is connected now", Toast.LENGTH_SHORT);
    t.show();

    System.out.println("Accessibility was connected!");

    instance = this;

}

public void takeSS(){
    Intent dialogIntent = new Intent(this, ScreenshotActivity.class);
    dialogIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    this.startActivity(dialogIntent);
}

还有其他一些使用dispatchGesture的方法。这就是为什么我对此有静态引用,并且可以从其他地方访问这些方法的原因。屏幕截图活动使用媒体投影来捕获屏幕截图并使用图像:

public class ScreenshotActivity extends Activity {
private static final int REQUEST_MEDIA_PROJECTION = 1;
private static final String TAG = "ScreenshotActivity";

private MediaProjectionManager mProjectionManager;

private MediaProjection mMediaProjection = null;

private VirtualDisplay mVirtualDisplay;

private ImageReader mImageReader;

private static final int MAX_IMAGE_BUFFER = 10;
private  int counter;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_screenshot_dummy);

    counter = 0;

    OrientationChangedListener mOrientationChangedListener = new OrientationChangedListener(this);
    mOrientationChangedListener.enable();

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        mProjectionManager = (MediaProjectionManager)getSystemService(MEDIA_PROJECTION_SERVICE);
        startActivityForResult(mProjectionManager.createScreenCaptureIntent(), REQUEST_MEDIA_PROJECTION);
    }
}

@TargetApi(Build.VERSION_CODES.LOLLIPOP)
public void onActivityResult(int requestCode, int resultCode, Intent resultData) {
    super.onActivityResult(requestCode, resultCode, resultData);
    if (requestCode == REQUEST_MEDIA_PROJECTION) {
        String message;

        if (resultCode != Activity.RESULT_OK) {
            message = "Media Projection Declined";
            mMediaProjection = null;
        } else {

            message = "Media Projection Accepted";
            mMediaProjection = mProjectionManager.getMediaProjection(resultCode, resultData);
            attachImageCaptureOverlay();

        }

        Toast toast = Toast.makeText(this, message, Toast.LENGTH_SHORT);
        toast.show();

    }
}

private class OrientationChangedListener extends OrientationEventListener {

    int mLastOrientation = -1;

    OrientationChangedListener(Context context) {
        super(context);
    }

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    @Override
    public void onOrientationChanged(int orientation) {

        final int screenOrientation = getWindowManager().getDefaultDisplay().getRotation();

        if (mVirtualDisplay == null) return;

        if (mLastOrientation == screenOrientation) return;

        mLastOrientation = screenOrientation;

        detachImageCaptureOverlay();

        attachImageCaptureOverlay();
    }
}

private final ImageReader.OnImageAvailableListener mOnImageAvailableListener = new ImageReader.OnImageAvailableListener() {

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    @Override
    public void onImageAvailable(ImageReader reader) {

        Image image = reader.acquireLatestImage();

        if (image == null || image.getPlanes().length <= 0) return;

        final Image.Plane plane = image.getPlanes()[0];

        final int rowPadding = plane.getRowStride() - plane.getPixelStride() * image.getWidth();
        final int bitmapWidth = image.getWidth() + rowPadding / plane.getPixelStride();

        final Bitmap tempBitmap = Bitmap.createBitmap(bitmapWidth, image.getHeight(), Bitmap.Config.ARGB_8888);
        tempBitmap.copyPixelsFromBuffer(plane.getBuffer());

        Rect cropRect = image.getCropRect();
        final Bitmap bitmap = Bitmap.createBitmap(tempBitmap, cropRect.left, cropRect.top, cropRect.width(), cropRect.height());

        //Do something with the bitmap


        image.close();
    }
};

@RequiresApi(Build.VERSION_CODES.LOLLIPOP)
private void attachImageCaptureOverlay() {

    if (mMediaProjection == null) return;

    final DisplayMetrics metrics = new DisplayMetrics();
    getWindowManager().getDefaultDisplay().getRealMetrics(metrics);

    mImageReader = ImageReader.newInstance(metrics.widthPixels, metrics.heightPixels, PixelFormat.RGBA_8888, MAX_IMAGE_BUFFER);

    mVirtualDisplay = mMediaProjection.createVirtualDisplay("ScreenCaptureTest",
            metrics.widthPixels, metrics.heightPixels, metrics.densityDpi,
            DisplayManager.VIRTUAL_DISPLAY_FLAG_AUTO_MIRROR,
            mImageReader.getSurface(), null, null);

    mImageReader.setOnImageAvailableListener(mOnImageAvailableListener, null);
}

@RequiresApi(api = Build.VERSION_CODES.KITKAT)
private void detachImageCaptureOverlay() {
    mVirtualDisplay.release();
    mImageReader.close();
}


}
android android-service accessibilityservice
1个回答
0
投票

您无法从后台since Android Oreo开始活动。

一种解决方法是,在您的服务启动时调用startForeground(true),然后通过适当的操作(Notification)将粘性starting your desired activity via PendingIntent添加到您的服务中。

© www.soinside.com 2019 - 2024. All rights reserved.