Last active
February 19, 2021 05:01
-
-
Save doyle-flutter/49540268443c9cf3eafb943fbf7000cd to your computer and use it in GitHub Desktop.
#07 포그라운드 - 플러터 : 안드로이드 액티비티
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // ... import 생략 | |
| // ... 기존 내용과 같이 구성하였습니다 | |
| // * Service 파일 생성 및 Permission 등록해주세요(안드로이드 내용을 미리 보시면 수월합니다) | |
| public class MainActivity extends FlutterActivity { | |
| private static final String CHANNEL = "app.james.cam/cam"; | |
| private static final String SEND_CHANNEL = "app.james.cam/cam2"; | |
| MethodChannel sendChannel; | |
| private static final String BACK_CHANNEL2 = "app.james.cam/intentservice"; | |
| private static final String BACK_METHOD_NAME = "intentservicestart"; | |
| ResultReceiver mRecevier; | |
| private static final String BACK_CHANNEL3 = "app.james.cam/foregroundflutter"; | |
| private static final String BACK_METHOD_NAME2 = "foregroundflutter"; | |
| @RequiresApi(api = Build.VERSION_CODES.O) | |
| @Override | |
| public void configureFlutterEngine(@NonNull FlutterEngine flutterEngine) { | |
| super.configureFlutterEngine(flutterEngine); | |
| new MethodChannel(flutterEngine.getDartExecutor().getBinaryMessenger(), CHANNEL) | |
| .setMethodCallHandler( | |
| ((call, result) -> { | |
| if(call.method.equals("cam")){ | |
| dispatchTakePictureIntent(); | |
| } | |
| })); | |
| sendChannel = new MethodChannel(flutterEngine.getDartExecutor().getBinaryMessenger(), SEND_CHANNEL); | |
| new MethodChannel(flutterEngine.getDartExecutor().getBinaryMessenger(), BACK_CHANNEL2) | |
| .setMethodCallHandler( | |
| ((call, result) -> { | |
| if(call.method.equals(BACK_METHOD_NAME)){ | |
| this.intentServiceStart(); | |
| } | |
| })); | |
| new MethodChannel(flutterEngine.getDartExecutor().getBinaryMessenger(), BACK_CHANNEL3) | |
| .setMethodCallHandler( | |
| ((call, result) -> { | |
| if(call.method.equals(BACK_METHOD_NAME2)){ | |
| final Intent forIntent = new Intent(this, MyService.class); | |
| forIntent.setAction("startForeground"); | |
| startForegroundService(forIntent); | |
| } | |
| })); | |
| final String SEND_CHANNEL2 = "app.james.cam/backdata"; | |
| final String SEND_METHOD2 = "data"; | |
| MethodChannel m2 = new MethodChannel(flutterEngine.getDartExecutor().getBinaryMessenger(), SEND_CHANNEL2); | |
| mRecevier = new ResultReceiver(new Handler()){ | |
| @Override | |
| protected void onReceiveResult(int resultCode, Bundle resultData) { | |
| super.onReceiveResult(resultCode, resultData); | |
| Log.d("DOY", resultData.getString("key")); | |
| m2.invokeMethod(SEND_METHOD2, resultData.getString("key").toString()); | |
| } | |
| }; | |
| } | |
| // 04 영상 | |
| // static final int REQUEST_IMAGE_CAPTURE = 1; | |
| // @SuppressLint("QueryPermissionsNeeded") | |
| // private void dispatchTakePictureIntent() { | |
| // Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); | |
| // if (takePictureIntent.resolveActivity(getPackageManager()) != null) { | |
| // startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE); | |
| // } | |
| // } | |
| // 05 영상 | |
| static final int REQUEST_IMAGE_CAPTURE = 1; | |
| private void dispatchTakePictureIntent() { | |
| // [ UI 및 제어는 플러터에서 만들기 때문에 주석 | |
| // if(this.imageBitmap != null){ | |
| // this.clear(); | |
| // button.setText("촬영"); ] | |
| // return; | |
| // } | |
| Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); | |
| if (takePictureIntent.resolveActivity(getPackageManager()) != null) { | |
| startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE); | |
| } | |
| } | |
| @Override | |
| protected void onActivityResult(int requestCode, int resultCode, Intent data) { | |
| super.onActivityResult(requestCode, resultCode, data); | |
| if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) { | |
| Bundle extras = data.getExtras(); | |
| Bitmap imageBitmap = (Bitmap) extras.get("data"); | |
| ByteArrayOutputStream stream = new ByteArrayOutputStream() ; | |
| imageBitmap.compress( Bitmap.CompressFormat.PNG, 100, stream) ; | |
| byte[] byteArray = stream.toByteArray() ; | |
| // [ UI 및 제어는 플러터에서 만들기 때문에 주석 | |
| // iv.setImageBitmap(imageBitmap); | |
| // button.setText("지우기"); ] | |
| sendChannel.invokeMethod("sendImg", byteArray); | |
| } | |
| } | |
| // void clear(){ | |
| // [ UI 및 제어는 플러터에서 만들기 때문에 주석 | |
| // iv.setImageBitmap(null); ] | |
| // this.imageBitmap = null; | |
| // } | |
| void intentServiceStart(){ | |
| final Intent intent = new Intent(this, MyIntentService.class); | |
| intent.putExtra("INTENT_KEY_RECEIVER",mRecevier); | |
| Log.d("DOY", "CLICK?"); | |
| startService(intent); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment