Android开发:实现系统自带截屏功能 需要获取System权限

在一个service界面上,点击一个button按钮,可以截屏

贴上代码:

mButton.setOnClickListener(new OnClickListener(){
         public void onClick(View arg0) {
          capturescreen();
         }
         });

private void captureScreen ()

{

mHandler.postDelayed(mScreenshotChordLongPress,1000);

}

private final Runnable mScreenshotChordLongPress = new Runnable() {      
  public void run() {
   takeScreenshot();       
  }
  };
  
  final Runnable mScreenshotTimeout = new Runnable() {
   public void run() {
    synchronized (mScreenshotLock) {
     if (mScreenshotConnection != null) {
      mContext.unbindService(mScreenshotConnection);                   
            mScreenshotConnection = null;
            }
     }
    }
   };
   
 private void takeScreenshot() {
  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() {
    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;
//                           if (mStatusBar != null && mStatusBar.isVisibleLw())
                            //   msg.arg1 = 1;
//                           if (mNavigationBar != null && mNavigationBar.isVisibleLw())
                             //  msg.arg2 = 1;
                           try {
                               messenger.send(msg);
                           } catch (RemoteException e) {
                           }
                       }
                   }
                   public void onServiceDisconnected(ComponentName name) {}
               };
            
               if (mContext.bindService(intent, conn, Context.BIND_AUTO_CREATE)) {               
                   mScreenshotConnection = conn;               
                   mHandler.postDelayed(mScreenshotTimeout, 10000);           
                   }      

           }
       }

 

点击button之后会停运,因为调用系统截屏的serivce:com.android.systemui.screenshot.TakeScreenshotService

要调用此service需要sysytem权限才能调用:

唯有将此应用内置到手机里面,才能获取到sysytem权限:

做法如下:

1.在你自己的程序当中的的mainfest.xml的manifest加上此属性android:sharedUserId="android.uid.system"

2.内置的时候Android.mk中加上LOCAL_CERTIFICATE := PLATFORM

3.在linux环境下将此应用编译进去。这样就能够实现系统自带的截屏了

你可能感兴趣的:(Android开发:实现系统自带截屏功能 需要获取System权限)