翻蚳è ã®ã¡ã¢ïŒç¿»èš³æã«ãGoogleèªèº«ãæäŸãããã·ã¢èªçã®Androidããã¥ã¡ã³ãã§çšèªãæ倧éã«æŽ»çšããããšãããããããµãŒãã¹ãã¯ããµãŒãã¹ãã«ããã³ã³ãã³ããããã€ããŒãã¯ãã³ã³ãã³ããããã€ããŒããªã©ã«ãªããŸããã ããããã掻åãã¯ãæäœãã«ãªãããšã¯ã§ããŸããã§ãã-ç§ã¯èªåèªèº«ãå§åããŸããã§ããã ããããªãã
èªããŸããããã¢ãã€ã«ããã€ã¹ã«ã¯ãç¡éã®ã¡ã¢ãªãç¡éã®ããããªé»æºããŸãã¯ä»ã®ç¡éã®ãã®ããããŸããã ç§ãã¡ã«ãšã£ãŠãããã¯æ¬¡ã®ããšãæå³ããŸãã ããã»ã¹ã®æ»ãã¢ããªã±ãŒã·ã§ã³ã®ã©ã€ããµã€ã¯ã«ã®èªç¶ãªéšåãšèŠãªããªããã°ãªããŸãã ã ããã»ã¹ã®ç Žå£ã«é¢é£ããã¡ã¢ãªã®è§£æŸããŠãŒã¶ãŒã«æªãçµæããããããªãããšã確èªããããšãéèŠã§ãã ãã®ã¿ã¹ã¯ãéæããããã«ãAndroidã®ããã»ã¹ã¢ãŒããã¯ãã£ã®ã»ãšãã©ã¯ãã©ã®ããã»ã¹ãçããŠæ»ã¬ãã«å¿ããŠå³æ Œãªéå±€ãæäŸããããã«èšèšãããŠããŸãã
Androidããã»ã¹éå±€
ããã¥ã¡ã³ãã«ç®ãåãããšãæãéèŠãªããã»ã¹ã¯ãã©ã¢ã°ã©ãŠã³ã ããã»ã¹ãšåŒã°ã ããã®åŸã«ç®ã«èŠããããã»ã¹ ã ãµãŒãã¹ããã»ã¹ ã ããã¯ã°ã©ãŠã³ã ããã»ã¹ ãæåŸã«ç©ºã®ããã»ã¹ãç¶ããŸã ã ãšããã§ãAndroidã¢ããªã±ãŒã·ã§ã³ã®ã³ã³ããŒãã³ãïŒãµãŒãã¹ã
Activity
ãªã©ïŒã«ã€ããŠã¯èª¬æããŸããã Androidã¯åžžã«ã³ã³ããŒãã³ãã§ã¯ãªãããã»ã¹ãç Žæ£ããããšã«æ³šæããŠãã ããã ãã¡ãããããã¯ã¬ããŒãžã³ã¬ã¯ã·ã§ã³ãšççŸããŸããïŒãã®çµæãä»ã®èª°ãåç §ããŠããªããªããžã§ã¯ãã«ãã£ãŠå æãããŠããã¡ã¢ãªãã·ã¹ãã ã«è¿ãããŸãïŒããããã¯å¥ã®æçš¿ã®ãããã¯ã§ãã

ãã©ã¢ã°ã©ãŠã³ãããã»ã¹
ããããããŠãŒã¶ãŒããã€ã§ãããåãããã®ã¯ãã·ã¹ãã ã®ã³ã³ããŒãã³ãã§ãããäœããã®æ¹æ³ã§ïŒå°ãªããšããŠãŒã¶ãŒãäœæ¥ãç¶ããŠããéãïŒç Žå£ã§ããªããã®ã ãšæããŸãã äžã€ã®ããšïŒããŠãŒã¶ãŒããã€ã§ãããåããããã®ãã¯ããããã«ãããŸããªå®çŸ©ã§ãã ãã®ã«ããŽãªã«åé¡ãããã³ã³ããŒãã³ãã®1ã€ã¯ããã©ã¢ã°ã©ãŠã³ã
Activity
ãonResumeïŒïŒãæ¢ã«åŒã³åºããã onPauseïŒïŒããŸã åŒã³åºãããŠããªããã®ã§ãã
äžéšã®
Activity
ã¯èªåã®ä»äºã®ã¿ã«äŸåããŸãããä»ã®
Activity
ã¯ãã®äžéšããã€ã³ãããããµãŒãã¹ã«ã·ããããŸãã
Activity
é¢é£ä»ãããããµãŒãã¹ãå«ãããã»ã¹ã¯ãForeground
Activity
èªäœãå«ãããã»ã¹ãšåãåªå 床ãã·ã¹ãã ããåãåããŸãã ããã¯æ£ããããšã§ã
Activity
ãããã®äœæ¥ã®ããã«ãµãŒãã¹ãšã®åžžææ¥ç¶ãç¶æããå¿ èŠããããšèããŠããå Žåã¯ã
Activity
èªäœãšAndroidã®äž¡æ¹ã®å©çã®ããã«ããã®ãµãŒãã¹ãçãããŠå®å šã«ä¿ã¡ãŸãã åãååãã³ã³ãã³ããããã€ããŒã«ãé©çšãããŸãã
ãããããã®
Activity
ãæ¶ãããšããã«ãŠãŒã¶ãŒãinãããå¯äžã®ã³ã³ããŒãã³ãã¯
Activity
ã ãšèª°ãèšã£ãã§ããããïŒ ç§ã®é³æ¥œãçªç¶åçãåæ¢ããå ŽåããŸãã¯ããã²ãŒã·ã§ã³ã·ã¹ãã ããã®ãã³ããé§ã®äžã«æ¶ããå Žåãç§ã¯ééããªãæããŸãã 幞ããªããšã«ãAndroidã§ã¯ã startForegroundïŒïŒã¡ãœãããåŒã³åºãããšã«ããããµãŒãã¹ãåªå é äœãä»ããããŠããããšãã·ã¹ãã ã«éç¥ã§ããŸãã ãã®ã¡ãœãããåŒã³åºãããšã¯ã ããã¯ã°ã©ãŠã³ããã¥ãŒãžãã¯ã®åçãä¿èšŒããæè¯ã®æ¹æ³ã§ããä»ã®ã¿ã¹ã¯ã«é¢ããŠã¯ã
startForeground()
åŒã³åºãåã«ãããŠãŒã¶ãŒã¯ããã«ç§ã®ãµãŒãã¹ãæ©èœããªããªã£ãããšã«æ°ä»ãã§ããããïŒãèšç»ã¯ãäœæ¥ã®äžæãããã«æããã«ãªãé倧ãªå Žåã«ã®ã¿äœ¿çšããå¿ èŠããããŸãã
泚ïŒããã³ããšã³ããµãŒãã¹ã§ã¯ããµãŒãã¹ãå®è¡ãããŠããããšããŠãŒã¶ãŒã«éç¥ããéç¥ãæçš¿ããå¿ èŠããããŸãã ç¹å®ã®ã±ãŒã¹ã§éç¥ãäžèŠã§ãããšæãããå Žåã¯ãããããããã³ããšã³ããµãŒãã¹ã¯å¿ èŠãããŸããïŒããã¯æ£åžžã§ãããããã¯ã°ã©ãŠã³ãã§æäœãä¿èšŒããä»ã®æ¹æ³ããããŸããããã«ã€ããŠã¯åŸè¿°ããŸãïŒã
ããã»ã¹ã®åªå 床ãäžæçã«ãã©ã¢ã°ã©ãŠã³ãããã»ã¹ã®åªå 床ã«äžããããä»ã®ããã€ãã®ã±ãŒã¹ããããŸãã ãããã«ã¯ã次ã®ã©ã€ããµã€ã¯ã«ã¡ãœãããå®è¡ãããµãŒãã¹ãå«ãŸããŸãïŒ onCreateïŒïŒ ã onStartCommandïŒïŒãããã³onDestroyïŒïŒ ã¡ãœããonReceiveïŒïŒã®ãããŒããã£ã¹ãã¡ãã»ãŒãžã®åä¿¡è ïŒãããŒããã£ã¹ãåä¿¡è ïŒã«ããå®è¡ãé©çšãããŸãã ãããã®ã©ã€ããµã€ã¯ã«ã¡ãœãããã¢ããââãã¯ã«ããåã³ã³ããŒãã³ããã·ã¹ãã ã«ãã£ãŠç Žå£ãããããšãªãããããå®è¡ã§ããããã«ããã«ã¯ããã®åªå 床ã®å¢å ãå¿ èŠã§ãã
ç®ã«èŠããããã»ã¹
ã ããããã
Activity
ãç§ã¯ãã§ã«
Activity
ã«ã€ããŠè©±ããŸãããïŒ åœŒã¯èšã£ãããAndroidã¯ç¥ç§çãªç¥æµã§ãããªãã®
Activity
ãèŠãããããã©ã¢ã°ã©ãŠã³ãã§ã¯ãªããšãã«ãã®ãããªç¶æ³ãçºçããããšãèš±ããŠããã ããã¯ã
Activity
ããDialogããããŒããç¶æ¿ããå¥ã®
Activity
ãéå§ãããšãã«çºçããå¯èœæ§ããããŸãã ãŸãã¯ãéå§
Activity
ãåéæã®å Žåã ãŸãã¯ããŠãŒã¶ãŒã«ç¹å®ã®ã¢ã¯ã»ã¹èš±å¯ ïŒå®éã¯
Activity
ïŒïŒãèŠæ±ããã·ã¹ãã ãã€ã¢ãã°ãåŒã³åºãããšãã
Activity
ã¯ã onStartïŒïŒåŒã³åºãããonStopïŒïŒåŒã³åºãã«è¡šç€ºãããŸãã ããã2ã€ã®åŒã³åºãã®éã«ãç»é¢ã®æŽæ°ãªã©ãç®ã«èŠãã
Activity
æåŸ ãããããšã¯äœã§ãã§ããŸãã
ã¡ãªã¿ã«ãå¯èŠ
Activity
ã®ãªã³ã¯ããããµãŒãã¹ãšã³ã³ãã³ããããã€ããŒ
Activity
ããŠããããã»ã¹ããå¯èŠ
Activity
ã®ããã»ã¹ãšåãåªå 床ãååŸããŸãïŒãã©ã¢ã°ã©ãŠã³ã
Activity
çºçããããšãšåæ§ïŒãããã»ã¹ã¯ã·ã¹ãã ã«ãã£ãŠç Žå£ãããŸããã
次ã®ç¹ã«æ³šæããŠãã ãããããã»ã¹ãèŠãããšããäºå®ã¯ãã·ã¹ãã ã«ãã£ãŠç Žå£ãããªãããšãä¿èšŒãããã®ã§ã¯ãããŸãã ã ãã©ã¢ã°ã©ãŠã³ãããã»ã¹ãããŒãºã®ããã«å€§éã®ã¡ã¢ãªãå¿ èŠãšããå ŽåãAndroidã極端ã«ãªãã衚瀺ãããŠããããã»ã¹ããã¿ã³ãšããå¯èœæ§ããããŸãã ãŠãŒã¶ãŒã«ãšã£ãŠãããã¯åæ¯ã®
Activity
äžã«ãã次ã®
Activity
ãé»ãç»é¢ã«çœ®ãæããããããã«èŠããŸãã ãã¡ããã ãã«ãã
Activity
æ£ããåäœæãããšã ãã©ã¢ã°ã©ãŠã³ã
Activity
ãç Žæ£ããããšããã«ãããã»ã¹ãš
Activity
ãç¶æ ã倱ãããšãªãåäœæãããŸãã
æ³šïŒ startActivityForResultïŒïŒã®çµæãonActivityResultïŒïŒã§åŠçããã requestPermissionsïŒïŒã®çµæãã³ãŒã«ããã¯é¢æ°ã§ã¯ãªãonRequestPermissionsResultïŒïŒã«ããçç±ã®1ã€ã¯ãå¯èŠããã»ã¹ã匷å¶çµäºããå¯èœæ§ã§ããããã»ã¹ãç Žæ£ããããšããã®äžã®æ¢åã®ã³ãŒã«ããã¯é¢æ°ããã¹ãŠç Žæ£ãããŸãã ãããã£ãŠãã³ãŒã«ããã¯é¢æ°ã䜿çšããã¢ãããŒãã䜿çšããã©ã€ãã©ãªãèŠãå Žåãã·ã¹ãã ã«ååãªã¡ã¢ãªããªãå ŽåãæåŸ ã©ããã«åäœããªãå¯èœæ§ãããããšã«æ³šæããŠãã ããã
ãµãŒãã¹ããã»ã¹
ããã»ã¹ãäžèšã®ã«ããŽãªã®ãããã«ã該åœãããå®è¡äžã®ãµãŒãã¹ãããå Žåãããã»ã¹ã¯ãµãŒãã¹ããã»ã¹ã®åªå 床ãååŸããŸãã ãã®ãããªã±ãŒã¹ã¯ãããã¯ã°ã©ãŠã³ãäœæ¥ïŒããŒã¿ã®ããŠã³ããŒããªã©ïŒãå®è¡ããå€ãã®ã¢ããªã±ãŒã·ã§ã³ã®å žåã§ããããã®äœæ¥ã®çµæã¯ããã³ããšã³ããµãŒãã¹ã®å Žåã»ã©éèŠã§ã¯ãããŸããã
ãããŠããããã®ããã»ã¹ã¯èŠãç®ã»ã©åœ¹ã«ç«ããªãããã§ã¯ãããŸããã å¹ åºãã¿ã¹ã¯ã®å Žåããã®ãããªããã»ã¹ã¯ããã¯ã°ã©ãŠã³ãã¿ã¹ã¯ãå®è¡ããããã®æè¯ã®æ¹æ³ã§ããããã¯ãå¯èŠããã»ã¹ãšãã©ã¢ã°ã©ãŠã³ãã§å€ãã®ããšãçºçããã©ããããã¡ã¢ãªãç·æ¥ã«ååŸããå¿ èŠãããå Žåã«ã®ã¿ç Žæ£ãããããã§ãã
ããã»ã¹ãã¡ã¢ãªäžè¶³ã§ç Žå£ãããå Žåã®ã·ã¹ãã ã®åäœã決å®ããããã onStartCommandïŒïŒããè¿ãå®æ°ã«ç¹ã«æ³šæããŠãã ããã
- START_STICKYã¯ãå¯èœãªå Žåã«ã·ã¹ãã ããµãŒãã¹ã埩掻ãããããšãæå³ããŸããããµãŒãã¹ãåãåã£ãæåŸã®
Intent
ãåéä¿¡ããŸããïŒããšãã°ãèªåã§ç¶æ ã埩å ããããèµ·åæã«äœããã¹ããã決å®ããèªå·±èšè¿°ã®ã©ã€ããµã€ã¯ã«ãæã£ãŠããŸãïŒãµãŒãã¹ã®ã·ã£ããããŠã³ïŒã - START_REDELIVER_INTENTã¯ãonStartCommandïŒïŒã§åãåã£ãã®ãšåã
Intent
ããåèµ·åãããµãŒãã¹çšã§ã - START_NOT_STICKY㯠ãé§ã®äžã«éãã«æ¶ããå Žåã«åèµ·åããå¿ èŠã®ãªããµãŒãã¹ã«åœ¹ç«ã¡ãŸãã ãã®åäœã¯ããã°ããã¹ãããã§ããå®æçãªã¿ã¹ã¯ãå®è¡ãããµãŒãã¹ã«åœ¹ç«ã€å ŽåããããŸãã
ããã¯ã°ã©ãŠã³ãããã»ã¹
Foreground
Activity
ãããããŠãŒã¶ãŒã[ããŒã ]ãã¿ã³ãã¯ãªãã¯ãããšã onStopïŒïŒã³ãŒã«ãçºçãããšããŸãã ãã®
Activity
ãšã¯å¥ã«ãããã»ã¹ã®é«ãåªå 床ãç¶æãç¶ããããšãã§ãããã®ããªããªã£ãå Žåãããã»ã¹ã¯ããã¯ã°ã©ãŠã³ãããã»ã¹ã®ã«ããŽãªã«å ¥ããŸã ã ã¡ãªã¿ã«ããŠãŒã¶ãŒãåŸã§ä»¥åã«éããã¢ããªã±ãŒã·ã§ã³ã®ããããã«æ»ãããšã«ããå Žåã«åããŠãããã€ã¹ã®ã¡ã¢ãªã®ã»ãšãã©ãå æããŸãã
Androidã¯ãããã»ã¹ãæåããéå§ããã®ã¯ããªããªãœãŒã¹ãæ¶è²»ãããããããã»ã¹ãå·Šå³ã«åŒ·å¶çµäºããŸããã ãããã£ãŠãæ°ããé«åªå 床ããã»ã¹ãæ°Žå¹³ç·ã«è¡šç€ºãããå Žåããããã¯ç Žæ£ãããåã«ãã°ããã¡ã¢ãªå ã«æ®ãããšãã§ããŸãã ç Žå£ã¯ãé·ãé䜿çšãããŠããªããã®ãæŒãã€ã¶ãé åºã§çºçããŸããã»ãšãã©äœ¿çšãããªãã£ãããã»ã¹ãæåã«ç Žå£ãããŸãã å¯èŠããã»ã¹/
Activity
ç Žå£ã®å Žåã®ããã«ãããŒã¿ã倱ãããšãªã
Activity
ãæ£ããåäœæã§ããå¿ èŠããããŸãã
空ã®ããã»ã¹
ä»ã®éå±€ãšåæ§ã«ãåžžã«æäžäœã¬ãã«ããããŸãã 空ã®ããã»ã¹ã«ã¯ã¢ã¯ãã£ããªã³ã³ããŒãã³ãã¯ãªããAndroidã¯ãã€ã§ãããããç Žæ£ã§ããŸãããéåžžã¯ãã°ããã®éã¡ã¢ãªå ã«ä¿æãããŸãïŒããã¯ããã¹ãŠãã¯ãªãŒã³ã¢ããããããšã§ã¯ãªããã¡ã¢ãªã®å¹ççãªäœ¿çšã«é¢ãããã®ã§ãïŒè¡ã§ïŒã
泚æãã¹ãããš
ããã»ã¹ã§å®è¡ãããŠããã³ã³ããŒãã³ãã®èŠ³ç¹ããããã»ã¹ã®åªå é äœã«ã€ããŠèª¬æããŸããããã³ã³ããŒãã³ãã¬ãã«ã§ã¯ãªãããã»ã¹ã¬ãã«ã§éå±€ãååšããããšã«æ³šæããŠãã ãã ã ããã»ã¹å šäœããã©ã¢ã°ã©ãŠã³ãããã»ã¹ã®ã«ããŽãªã«å€æããã³ã³ããŒãã³ãã¯1ã€ã ãã§ãïŒããšãã°ããã©ã¢ã°ã©ãŠã³ããµãŒãã¹ïŒã ã»ãšãã©ã®ã¢ããªã±ãŒã·ã§ã³ã¯1ã€ã®ããã»ã¹ã§å®å šã«åäœããŸãããã¢ããªã±ãŒã·ã§ã³ã®ã©ã€ããµã€ã¯ã«ãèµ·åããšã«å€§ããç°ãªãå ŽåããŸãã¯ã¢ããªã±ãŒã·ã§ã³ã«éåžžã«å°é£ãªã¿ã¹ã¯ãããå Žåã¯ãæéãããããŸããã軜éã§ãããæåã®ããã»ã¹ã¯2çªç®ã®ããã»ã¹ã«äŸåããŸãããã€ãŸãã2ã€ã®ããã»ã¹ãåºå¥ããããšã¯çã«ããªã£ãŠããŸãã æåã®ããã»ã¹ã§éãã¿ã¹ã¯ãå®è¡ãã2çªç®ã®ããã»ã¹ã§è»œãã¿ã¹ã¯ãå®è¡ããŸãããã®åŸãã¡ã¢ãªäžè¶³ã®å Žåã¯ãéãã¿ã¹ã¯ãäžæãã軜éãªã¿ã¹ã¯ãç¶è¡ããŸãã
ãŸããããã»ã¹ã®åªå 床ã¯ã³ã³ããŒãã³ãã¬ãã«ã§äœãèµ·ãããã«ãã£ãŠæ±ºãŸãããšãèŠããŠããããšãéèŠã§ã ã ã€ãŸããé·æéå®è¡ããå¿ èŠã®ããéåžžã«éèŠãªã¿ã¹ã¯ãå¥ã®ã¹ã¬ããã§ããã ã
Activity
äžããå®è¡ãããšãããã»ã¹ãçªç¶ããã¯ã°ã©ãŠã³ãã«ãªã£ããšãã«äžå¿«ãªé©ããåŸãããšã«ãªããŸãã å©çšå¯èœãªããŒã«ïŒãµãŒãã¹ãããã³ããšã³ããµãŒãã¹ãªã©ïŒã䜿çšããŠãã·ã¹ãã ãçŸåšå®è¡ããŠããããšã確èªããŸãã
ä»äººã®ããšãå¿ããã«ããŠãŒã¶ãŒã®ããšãèŠããŠãããŠãã ãã
å¿ èŠãªåªå 床ã§åžžã«æ©èœããããã«ã¢ããªã±ãŒã·ã§ã³ãæ§ç¯ããŸã-ãã以äžã§ããã以äžã§ããããŸããã ãŸããéçºè ãšããŠãæã匱ããšã³ããŠãŒã¶ãŒããã€ã¹ãããã¯ããã«åŒ·åãªããã€ã¹ãæã«æã£ãŠããããšãå¿ããªãã§ãã ããããã®ãããç®ã«èŠããããã»ã¹ïŒããã³ãµãŒãã¹ããã»ã¹-ããã«ïŒã®ç Žå£ãèªåã®ç®ã§èŠãããšã¯ã§ããŸãããã決ããŠèµ·ãããªãã
éåžžããã¹ãã«ã¯ã§ããã ã匱ãããã€ã¹ãè³Œå ¥ããããšããå§ãããŸããããã©ãã°ã·ããã§ããã¢ããªã±ãŒã·ã§ã³ãç Žå£ãããå Žåã®åäœããã¹ãã§ããŸã ã ã¢ããªã±ãŒã·ã§ã³ãïŒãã®ãã¹ãŠã®ããã»ã¹ãšãšãã«ïŒç Žæ£ããã«ã¯ã次ã®æé ãå®è¡ããŸãã
adb shell am force-stop com.example.packagename
è€æ°ã®ããã»ã¹ãããå Žåãæåã«æ¬¡ã®ã³ãã³ãã®çµæã®2çªç®ã®åïŒã€ãŸãæåã®æ°åïŒãèŠãŠãå¿ èŠãªããã»ã¹ã®PIDãèŠã€ããããšãã§ããŸãã
adb shell ps | grep com.example.packagename
ãããŠããã®ããã»ã¹ãå©ããŸãïŒ
adb shell kill PID
ããã¯ãã¡ã¢ãªå¶éãçºçãããã©ããã«é¢ä¿ãªããã¢ããªã±ãŒã·ã§ã³ãã»ãšãã©ã®ããã€ã¹ã§æ£ããåäœããããšãä¿èšŒããæåã®ã¹ãããã«ãªããŸãã