下面写一下具体实现
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);
}
}
}
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。