public
class
CService
extends
Service {
BroadcastReceiver CReceiver =
null
;
Bitmap bm;
View view;
@Override
public
void
onCreate(){
super
.onCreate();
Intent intent =
new
Intent(
this
, MainActivity.
class
);
PendingIntent pIntent = PendingIntent.getActivity(
this
,
0
, intent, PendingIntent.FLAG_UPDATE_CURRENT);
Notification noti =
new
NotificationCompat.Builder(
this
)
.setContentTitle(
"Capture & Crop"
)
.setContentText(
"Running Cature & Crop Service"
)
.setContentIntent(pIntent)
.build();
startForeground(
6974
, noti);
}
private
void
screenshot(Bitmap bm){
try
{
File path =
new
File(
"/sdcard/CropCature"
);
if
(!path.isDirectory()) {
path.mkdir();
}
String temp =
"/sdcard/CropCapture"
;
temp = temp + System.currentTimeMillis();
temp = temp +
".jpg"
;
FileOutputStream out =
new
FileOutputStream(temp);
bm.compress(Bitmap.CompressFormat.JPEG,
100
, out);
Toast.makeText(
this
,
"스크린샷"
+temp+
"이 저장되었습니다."
,Toast.LENGTH_SHORT).show();
sendBroadcast(
new
Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse(
"file://"
+ Environment.getExternalStorageState())));
}
catch
(FileNotFoundException e){
Log.d(
"FileNotFoundException:"
,e.getMessage());
}
}
@Override
public
int
onStartCommand(Intent intent,
int
flags,
int
startId) {
super
.onStartCommand(intent, flags, startId);
Toast.makeText(
this
,
"서비스실행."
,Toast.LENGTH_SHORT).show();
IntentFilter intentFilter =
new
IntentFilter();
intentFilter.addAction(
"android.intent.extra.KEY_EVENT"
);
intentFilter.addAction(
"android.intent.action.MEDIA_BUTTON"
);
CReceiver =
new
BroadcastReceiver()
{
@Override
public
void
onReceive(Context context, Intent intent){
Toast.makeText(getBaseContext(),
"리시버작동"
, Toast.LENGTH_SHORT).show();
KeyEvent event = (KeyEvent)intent.getParcelableExtra(Intent.EXTRA_KEY_EVENT);
if
(event ==
null
) {
return
;
}
int
action = event.getAction();
if
(action == KeyEvent.ACTION_DOWN) {
try
{
View v1 = view.getRootView();
v1.setDrawingCacheEnabled(
true
);
bm = v1.getDrawingCache();
screenshot(bm);
}
catch
(Exception e){
e.printStackTrace();
}
}
else
if
(action == KeyEvent.KEYCODE_VOLUME_DOWN)
{
try
{
View v1 = view.getRootView();
v1.setDrawingCacheEnabled(
true
);
bm = v1.getDrawingCache();
screenshot(bm);
}
catch
(Exception e){
e.printStackTrace();
}
}
}
};
registerReceiver(CReceiver, intentFilter);
return
START_STICKY;
}
@Override
public
void
onDestroy(){
unregisterReceiver(CReceiver);
super
.onDestroy();
}
@Override
public
IBinder onBind(Intent arg0){
return
null
;
}
}