开发者社区> 问答> 正文

android怎么调用截屏接口

android怎么调用截屏接口

展开
收起
云计算小粉 2018-05-10 20:02:01 2213 0
1 条回答
写回答
取消 提交回答
  • 阿里云ping https://ping.gaomeluo.com/aliyun/

    下面写一下具体实现
    1.定义一个意图字符串

    public static final String
    SYSTEM_SHOOT=”com.screenshot.quietly”

    2.添加到意图过滤器
    filter.addAction(SYSTEMUI_SHOOT);//添加到意图过滤器

    3.接收广播
    private BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {
    …..
    else if(SYSTEMUI_SHOOT.equals(action)) {

    Log.d("Victor","SYSTEMUI_LEKE_SHOOT: "+action);
    
    String pp=intent.getExtra("pp").toString();//获取用户定义的图片路径
    
    String pn= (intent.getExtra("pn")+".jpg").toString();//图片名
        Settings.System.putString(mContext.getContentResolver(),"ssp", pp);
        Settings.System.putString(mContext.getContentResolver(),"ssn", pn);
        takeScreenShot();//调用此方法做开启服务操作
        } 
    

    }

    4.执行takeScreenShot()

    private void takeScreenShot() {

    String imageDir=Settings.System.getString(mContext.getContentResolver(), "ssp");
    String imageNam=Settings.System.getString(mContext.getContentResolver(), "ssn");
    File file=new File(imageDir);
    String text=null;
    Log.d("Victor","imageDir="+imageDir+ ",imageName="+imageNam);

    file.mkdir();

      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
    
           Log.d("Victor","onServiceConnected---------");
              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=0;
                      msg.arg2=1;
                      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);
          }
      }

    }

    2019-07-17 22:12:12
    赞同 展开评论 打赏
问答分类:
问答地址:
问答排行榜
最热
最新

相关电子书

更多
58同城Android客户端Walle框架演进与实践之路 立即下载
Android组件化实现 立即下载
蚂蚁聚宝Android秒级编译——Freeline 立即下载