Created
February 19, 2021 05:01
-
-
Save doyle-flutter/efce9eb205012cd322914100e370ab60 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 생략 | |
| // ... 안드로이드 내용 그대로 사용 가능하지만 | |
| // 알림의 이미지 내용 변경 및 어노테이션이 필요합니다 | |
| public class MyService extends Service { | |
| public MyService() { | |
| } | |
| @Override | |
| public IBinder onBind(Intent intent) { | |
| // TODO: Return the communication channel to the service. | |
| throw new UnsupportedOperationException("Not yet implemented"); | |
| } | |
| @RequiresApi(api = Build.VERSION_CODES.O) | |
| @Override | |
| public int onStartCommand(Intent intent, int flags, int startId) { | |
| if("startForeground".equals(intent.getAction())){ | |
| Log.d("DOY", "시작"); | |
| startForegroundService(); | |
| } | |
| return super.onStartCommand(intent, flags, startId); | |
| } | |
| @Override | |
| public void onDestroy() { | |
| Log.d("DOY", "DESTROY"); | |
| super.onDestroy(); | |
| } | |
| @RequiresApi(api = Build.VERSION_CODES.O) | |
| private void startForegroundService() { | |
| Intent notificationIntent = new Intent(this, MainActivity.class); | |
| PendingIntent pendingIntent = | |
| PendingIntent.getActivity(this, 0, notificationIntent, 0); | |
| Notification notification = | |
| new Notification.Builder(this, "default") | |
| .setContentTitle("포그라운드") | |
| .setContentText("with Flutter") | |
| .setSmallIcon(R.mipmap.ic_launcher) // Flutter Logo | |
| .setContentIntent(pendingIntent) | |
| .setTicker("tickerTxt") | |
| .build(); | |
| if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O){ | |
| NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); | |
| manager.createNotificationChannel( | |
| new NotificationChannel("default","기본", NotificationManager.IMPORTANCE_DEFAULT) | |
| ); | |
| } | |
| startForeground(1, notification); | |
| Timer m = new Timer(); | |
| TimerTask mt = new TimerTask() { | |
| @Override | |
| public void run() { | |
| stopSer(); | |
| } | |
| }; | |
| m.schedule(mt,10000); | |
| } | |
| void stopSer(){ | |
| this.stopSelf(); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment