java.lang.SecurityException:不允许绑定到服务

时间:2017-12-12 17:54:15

标签: android android-activity

我知道那里几乎没有类似的线程我尝试过所有这些线程,它们似乎都不适合我。我尝试在Android中捕获屏幕截图,但不幸的是它一直在崩溃并给我这个错误。在AndroidMaifest.xml中,名称为红色,就好像它没有被识别一样。

public class ScreenshotAction extends SinglePressAction {

private static Context mContext;
private String mScreenshotLabel;
private Drawable mScreenshotIcon;
private static final Object mScreenshotLock = new Object();
private static ServiceConnection mScreenshotConnection = null;

public ScreenshotAction(){
    super();
}

public ScreenshotAction(Context context, String screenshotLabel, Drawable screenshotIcon) {
    super(context);
    mContext = context;
    mScreenshotLabel = screenshotLabel;
    mScreenshotIcon = screenshotIcon;
}

@Override
protected void setupLabel(TextView labelView) {
    labelView.setText(mScreenshotLabel);
}

@Override
protected void setupIcon(ImageView icon) {
    icon.setImageDrawable(mScreenshotIcon);
}

@Override
protected void onPress() {
    final Handler handler = new Handler();
    handler.postDelayed(new Runnable() {
        @Override
        public void run() {
            takeScreenshot(handler);
            Toast.makeText(mContext, "screenshot ok", Toast.LENGTH_LONG).show();
        }
    }, 1000);
}

public static void takeScreenshot(final Handler mHandler) {
    synchronized (mScreenshotLock) {
        if (mScreenshotConnection != null) {
            return;
        }
        ComponentName cn = new ComponentName("com.android.systemui",
                "com.android.systemui.screenshot.TakeScreenshotService");
        Intent intent = new Intent();
        intent.setComponent(cn);
        ServiceConnection conn = new ServiceConnection() {
            @Override
            public void onServiceConnected(ComponentName name, IBinder service) {
                synchronized (mScreenshotLock) {
                    if (mScreenshotConnection != this) {
                        return;
                    }
                    Messenger messenger = new Messenger(service);
                    Message msg = Message.obtain(null, 1);
                    final ServiceConnection myConn = this;

                    Handler h = new Handler(mHandler.getLooper()) {
                        @Override
                        public void handleMessage(Message msg) {
                            synchronized (mScreenshotLock) {
                                if (mScreenshotConnection == myConn) {
                                    mContext.unbindService(mScreenshotConnection);
                                    mScreenshotConnection = null;
                                    mHandler.removeCallbacks(mScreenshotTimeout);
                                }
                            }
                        }
                    };
                    msg.replyTo = new Messenger(h);
                    msg.arg1 = msg.arg2 = 0;
                    try {
                        messenger.send(msg);
                    } catch (RemoteException e) {
                    }
                }
            }
            @Override
            public void onServiceDisconnected(ComponentName name) {}
        };

        if (mContext.bindService(intent, conn, Context.BIND_AUTO_CREATE)) {
            mScreenshotConnection = conn;
            mHandler.postDelayed(mScreenshotTimeout, 10000);
        }
    }
}

private static final Runnable mScreenshotTimeout = new Runnable() {
    @Override
    public void run() {
        synchronized (mScreenshotLock) {
            if (mScreenshotConnection != null) {
                mContext.unbindService(mScreenshotConnection);
                mScreenshotConnection = null;
            }
        }
    }
  };
}

错误: -

E/AndroidRuntime: FATAL EXCEPTION: main
              Process: ......, PID: 6022
              java.lang.SecurityException: Not allowed to bind to service Intent { cmp=com.android.systemui/.screenshot.TakeScreenshotService }
                  at android.app.ContextImpl.bindServiceCommon(ContextImpl.java:1595)
                  at android.app.ContextImpl.bindService(ContextImpl.java:1559)
                  at android.content.ContextWrapper.bindService(ContextWrapper.java:517)

AndroidMaifest.xml

此名称为红色,告诉我它没有识别出来。

<service android:name=".screenshot.TakeScreenshotService"
        android:process=":screenshot"
        android:exported="true" />

1 个答案:

答案 0 :(得分:0)

  

我正在尝试捕获Android中的屏幕截图

在Android 5.0 +上使用媒体投影API。

  

它一直崩溃并给我这个错误

您无权绑定该服务。您甚至不知道任何给定设备上是否存在该服务。

  

此名称为红色,告诉我它没有识别出来。

那是因为你没有写那项服务。