2017-12-12 5 views
0

거기에 비슷한 스레드가 거의 없다는 것을 알고 있습니다. 모두 시도했지만 아무 것도 작동하지 않습니다. 안드로이드에서 스크린 샷 캡처를 시도하고 있지만 불행히도 계속해서 오류가 발생합니다. AndroidMaifest.xml에서는 인식되지 않는 것처럼 이름이 빨간색으로 표시됩니다.java.lang.SecurityException : 서비스에 바인드 할 수 없습니다.

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" /> 

답변

0

I'm trying capture Screenshots in Android

사용을 안드로이드의 미디어 프로젝션 API를 5.0.

It keeps crashing and giving me this error

당신은 해당 서비스에 결합 할 수있는 권한이 없습니다. 해당 서비스가 특정 장치에 존재하는지 여부조차 모릅니다.

This name is in red which tells me that it doesn't recognise it.

해당 서비스를 작성하지 않았기 때문입니다.

+0

위의 내용은 4.2.1에서 작동하지만 4.4.4에서는 작동하지 않을 때 4.4.4에서 작동해야합니다. + 어떤 제안? – Tony

+0

@ 토니 : 미디어 프로젝션 API를 사용하여 스크린 샷을 제공하지 않거나 API 레벨 21 이상 (Android 5.0 이상)의 스크린 샷 만 제공합니다. Android 5.0 이전에 프로그래밍 방식으로 시스템 스크린 샷을 요청하는 지원되는 방법은 없습니다. – CommonsWare

+0

감사합니다. 문제는 내가 게시 한 것과 동일한 코드를 사용하여 동일한 하드웨어 및 Android 버전에서 앱을 개발 한 다른 사람이 있다는 것입니다. 위의 코드를 사용하여 Android 4 기기에서 작동하는 앱 버전이 있지만 다른 기기에서는 오류가 발생하여 충돌합니다. 한 앱에는 설정되었지만이 앱/코드와 관련하여 다른 앱에는 설정되지 않은 라이브러리 또는 권한이있는 것으로 보입니다. 이 코드가 logcat에 패키지 권한을 기록하도록 디버그를 추가하여 해당 코드가 실행되는지 확인합니다. – Tony