{"id":6228,"date":"2018-02-27T10:00:39","date_gmt":"2018-02-27T01:00:39","guid":{"rendered":"https:\/\/www.gigas-jp.com\/appnews\/?p=6228"},"modified":"2018-02-26T22:09:36","modified_gmt":"2018-02-26T13:09:36","slug":"%e9%95%b7%e6%9c%9f%e9%96%93service%e3%82%92%e8%b5%b7%e5%8b%95%e3%81%97%e3%81%9f%e3%81%84%e6%99%82","status":"publish","type":"post","link":"https:\/\/www.gigas-jp.com\/appnews\/archives\/6228","title":{"rendered":"\u9577\u671f\u9593Service\u3092\u8d77\u52d5\u3057\u305f\u3044\u6642"},"content":{"rendered":"<p>\u3053\u3093\u306b\u3061\u306fTamamo\u3067\u3059\u3002<br \/>\n \u6bd4\u8f03\u7684\u9577\u3044\u671f\u9593Service\u3092\u8d77\u52d5\u3057\u305f\u3068\u304d\u304c\u3042\u308b\u3068\u601d\u3044\u307e\u3059\u3002<br \/>\n \u3057\u304b\u3057\u3001Android\u306f\u30e1\u30e2\u30ea\u4e0d\u8db3\u306a\u3069\u3067\u81ea\u52d5\u7684\u306bService\u3092kill\u3059\u308b\u5834\u5408\u304c\u3042\u308a\u3001<br \/>\n \u307e\u305f\u3001\u30a2\u30d7\u30ea\u8d77\u52d5\u5c65\u6b74\u304b\u3089\u30b9\u30ef\u30a4\u30d7\u3057\u3066\u6d88\u3057\u305f\u5834\u5408\u3001\u30d7\u30ed\u30bb\u30b9\u307e\u3067kill\u3055\u308c\u3066\u3057\u307e\u3044\u3001<br \/>\n \u9577\u671f\u9593Service\u3092\u8d77\u52d5\u3055\u305b\u7d9a\u3051\u305f\u3044\u5834\u5408\u56f0\u308b\u5834\u5408\u304c\u3042\u308a\u307e\u3059\u3002<\/p>\n<p>\u305d\u3053\u3067\u3001\u7d76\u5bfe\u306bkill\u3055\u308c\u306a\u3044\u9244\u58c1\u306eService\u306f\u4f5c\u308c\u307e\u305b\u3093\u304c\u3001kill\u3055\u308c\u306b\u304f\u3044Service\u3092<br \/>\n \u4f5c\u308b\u65b9\u6cd5\u304c\u3042\u308b\u306e\u3067\u7d39\u4ecb\u3057\u307e\u3059\u3002<\/p>\n<p><!--more--><\/p>\n<h3>Foreground Service\u3092\u4f7f\u7528\u3059\u308b<\/h3>\n<p>\n \u3000Foreground Service\u306f\u901a\u5e38\u306eBackground Service\u3068\u9055\u3044\u3001\u30d5\u30a9\u30a2\u30b0\u30e9\u30a6\u30f3\u30c9\u306eActivity<br \/>\n \u3000\u3068\u540c\u69d8\u306e\u512a\u5148\u5ea6\u3068\u306a\u308a\u3001kill\u3055\u308c\u306b\u304f\u304f\u306a\u308a\u307e\u3059\u3002<br \/>\n \u3000\u307e\u305f\u3001Foreground Service\u306f\u30e1\u30e2\u30ea\u4e0d\u8db3\u306b\u306a\u3063\u3066\u3082\u57fa\u672c\u7684\u306bkill\u3055\u308c\u308b\u3053\u3068\u306f\u306a\u304f\u3001<br \/>\n \u3000\u610f\u56f3\u3057\u306a\u3044Service\u306e\u518d\u8d77\u52d5\u3092\u8a98\u767a\u3059\u308b\u3053\u3068\u306f\u3042\u308a\u307e\u305b\u3093\u3002<br \/>\n \u3000\u3057\u304b\u3057\u3001\u7d76\u5bfe\u306bkill\u3055\u308c\u306a\u3044\u308f\u3051\u3067\u306f\u306a\u3044\u306e\u3067\u6ce8\u610f\u304c\u5fc5\u8981\u3067\u3059\u3002<\/p>\n<h3>Foreground Service\u306e\u4f7f\u3044\u65b9<\/h3>\n<p>\u4ee5\u4e0b\u306e\u30bd\u30fc\u30b9\u30b3\u30fc\u30c9\u3067Foreground Service\u3092\u5b9f\u884c\u3067\u304d\u307e\u3059\u3002<\/p>\n<table>\n<tbody>\n<tr>\n<td>\n<p>public class MonitorLaunchService extends Service<br \/>\n {<br \/>\n \u00a0 \u00a0 public int onStartCommand(Intent intent, int flags, int startId)<br \/>\n \u00a0 \u00a0 {<br \/>\n \u00a0 \u00a0 \u00a0 \u00a0 \/\/ Notification\u306e\u30a4\u30f3\u30b9\u30bf\u30f3\u30b9\u5316<br \/>\n \u00a0 \u00a0 \u00a0 \u00a0 Notification notification = new Notification();<\/p>\n<p>\u00a0 \u00a0 \u00a0 \u00a0 Intent i = new Intent(getApplicationContext(), MainActivity.class);<br \/>\n \u00a0 \u00a0 \u00a0 \u00a0 PendingIntent pi = PendingIntent.getActivity(this, 0, i, <br \/>\n \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0PendingIntent.FLAG_CANCEL_CURRENT);<\/p>\n<p>\u00a0 \u00a0 \u00a0 \u00a0 NotificationCompat.Builder builder = new NotificationCompat.Builder(this);<\/p>\n<p>\u00a0 \u00a0 \u00a0 \u00a0 \/\/Foreground Service<br \/>\n \u00a0 \u00a0 \u00a0 \u00a0 notification = builder.setContentIntent(pi)<br \/>\n \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0.setSmallIcon(R.mipmap.ic_launcher).setTicker(&#8220;&#8221;)<br \/>\n \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0.setAutoCancel(true).setContentTitle(&#8220;\u30bf\u30a4\u30c8\u30eb&#8221;)<br \/>\n \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0.setContentText(&#8220;\u30a2\u30d7\u30ea\u540d&#8221;).build();<\/p>\n<p>\u00a0 \u00a0 \u00a0 \u00a0\u00a0\/\/\u30b9\u30c6\u30fc\u30bf\u30b9\u30d0\u30fc\u306e\u901a\u77e5\u3092\u6d88\u305b\u306a\u3044\u3088\u3046\u306b\u3059\u308b<br \/>\n \u00a0 \u00a0 \u00a0 \u00a0 notification.flags |= Notification.FLAG_ONGOING_EVENT;\u00a0<br \/>\n \u00a0 \u00a0 \u00a0 \u00a0 startForeground(startId, notification); \/\/Foreground Service\u958b\u59cb<\/p>\n<p> \u00a0 \u00a0 \u00a0 \u00a0 return START_STICKY; \/\/\u2460<br \/>\n \u00a0 \u00a0 }<br \/>\n }<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h3>\n onStartCommand\u306e\u623b\u308a\u5024\u306b\u3064\u3044\u3066<\/h3>\n<p>\u3000\u4e0a\u8a18\u306e\u30bd\u30fc\u30b9\u30b3\u30fc\u30c9\u306e\u2460\u306e\u90e8\u5206\u306fService\u304ckill\u3055\u308c\u305f\u3068\u304d\u518d\u8d77\u52d5\u3059\u308b\u304b\u3057\u306a\u3044\u304b\u306a\u3069\u3092\u8a2d\u5b9a\u3057\u307e\u3059\u3002<br \/>\n \u3000\u3044\u304f\u3064\u304b\u306e\u7a2e\u985e\u304c\u3042\u308a\u307e\u3059\u306e\u3067\u7d39\u4ecb\u3057\u307e\u3059\u3002<\/p>\n<p>\u3000\u25cfSTART_NOT_STICKY<br \/>\n \u3000\u3000Service\u304ckill\u3055\u308c\u305f\u5834\u5408\u3001Service\u306f\u518d\u8d77\u52d5\u3057\u307e\u305b\u3093\u3002<\/p>\n<p>\u3000\u25cfSTART_STICKY<br \/>\n \u3000\u3000\u30c7\u30d5\u30a9\u30eb\u30c8\u3067\u306f\u3053\u306e\u8a2d\u5b9a\u306b\u306a\u308a\u307e\u3059\u3002<br \/>\n \u3000\u3000Service\u304ckill\u3055\u308c\u305f\u5834\u5408\u3001\u76f4\u3061\u306bService\u306e\u518d\u8d77\u52d5\u3092\u884c\u3044\u307e\u3059\u3002<br \/>\n \u3000\u3000\u518d\u8d77\u52d5\u6642\u3001\u524d\u56de\u8d77\u52d5\u6642\u306eIntent\u306f\u518d\u914d\u4fe1\u3055\u308c\u305a\u3001<br \/>\n \u3000\u3000\u8907\u6570\u306eService\u304c\u8d77\u52d5\u3057\u3066\u3044\u3066\u3082\u518d\u8d77\u52d5\u3059\u308bService\u306f1\u3064\u3067\u3059\u3002<\/p>\n<p>\u3000\u25cfSTART_REDELIVER_INTENT<br \/>\n \u3000\u3000Service\u304ckill\u3055\u308c\u305f\u5834\u5408\u3001\u76f4\u3061\u306bService\u306e\u518d\u8d77\u52d5\u3092\u884c\u3044\u307e\u3059\u3002<br \/>\n \u3000\u3000\u518d\u8d77\u52d5\u6642\u3001\u6700\u5f8c\u306b\u4f7f\u7528\u3057\u305fIntent\u3092\u4f7f\u7528\u3057\u307e\u3059\u3002<br \/>\n \u3000\u3000\u307e\u305f\u3001\u8907\u6570\u306eService\u304c\u8d77\u52d5\u3057\u3066\u3044\u308b\u5834\u5408\u3001\u3059\u3079\u3066\u518d\u8d77\u52d5\u3057\u307e\u3059\u3002<\/p>\n<p>\u3000\u25cfSTART_STICKY_COMPATIBLILITY<br \/>\n \u3000\u3000START_STICKY\u306e\u4e92\u63db\u30d0\u30fc\u30b8\u30e7\u30f3\u3067\u3059\u3002Android2.0\u672a\u6e80\u3067\u306f\u3053\u308c\u304c\u30c7\u30d5\u30a9\u30eb\u30c8\u8a2d\u5b9a\u3067\u3059\u3002<\/p>\n<h3>\u6700\u5f8c\u306b<\/h3>\n<p>\n \u3000Service\u3092\u4f7f\u7528\u3057\u305f\u30a2\u30d7\u30ea\u3092\u4f5c\u308b\u5fc5\u8981\u304c\u3042\u3063\u305f\u305f\u3081\u3001<br \/>\n \u3000\u6700\u521d\u306b\u4f7f\u7528\u3057\u305f\u306e\u306fBackground Service\u3067\u3057\u305f\u3002<br \/>\n \u3000\u3053\u308c\u304c\u5384\u4ecb\u3067\u3001\u30e1\u30e2\u30ea\u4e0d\u8db3\u3067kill\u3055\u308c\u305f\u308a\u3001\u30a2\u30d7\u30ea\u8d77\u52d5\u5c65\u6b74\u304b\u3089\u30b9\u30ef\u30a4\u30d7\u3057\u3066\u6d88\u3057\u305f\u5834\u5408\u3001<br \/>\n \u3000\u30d7\u30ed\u30bb\u30b9\u307e\u3067kill\u3055\u308c\u305f\u308a\u3067\u3001\u7d50\u69cb\u30cf\u30de\u308a\u307e\u3057\u305f\u3002<br \/>\n \u3000\u76ee\u7684\u306b\u5408\u308f\u305b\u3066Service\u306f\u4f7f\u3044\u5206\u3051\u306a\u3044\u3068\u3044\u3051\u306a\u3044\u3093\u3067\u3059\u306d\u3002\u52c9\u5f37\u306b\u306a\u308a\u307e\u3059\u3002<\/p>\n<p>\u706b\u66dc\u65e5\u62c5\u5f53\uff1aTamamo<\/p>\n<div class='wp_social_bookmarking_light'>\n            <div class=\"wsbl_google_plus_one\"><g:plusone size=\"medium\" annotation=\"none\" href=\"https:\/\/www.gigas-jp.com\/appnews\/archives\/6228\" ><\/g:plusone><\/div>\n            <div class=\"wsbl_hatena_button\"><a href=\"\/\/b.hatena.ne.jp\/entry\/https:\/\/www.gigas-jp.com\/appnews\/archives\/6228\" class=\"hatena-bookmark-button\" data-hatena-bookmark-title=\"\u9577\u671f\u9593Service\u3092\u8d77\u52d5\u3057\u305f\u3044\u6642\" data-hatena-bookmark-layout=\"standard\" title=\"\u3053\u306e\u30a8\u30f3\u30c8\u30ea\u30fc\u3092\u306f\u3066\u306a\u30d6\u30c3\u30af\u30de\u30fc\u30af\u306b\u8ffd\u52a0\"> <img src=\"\/\/b.hatena.ne.jp\/images\/entry-button\/button-only@2x.png\" alt=\"\u3053\u306e\u30a8\u30f3\u30c8\u30ea\u30fc\u3092\u306f\u3066\u306a\u30d6\u30c3\u30af\u30de\u30fc\u30af\u306b\u8ffd\u52a0\" width=\"20\" height=\"20\" style=\"border: none;\" \/><\/a><script type=\"text\/javascript\" src=\"\/\/b.hatena.ne.jp\/js\/bookmark_button.js\" charset=\"utf-8\" async=\"async\"><\/script><\/div>\n            <div class=\"wsbl_twitter\"><a href=\"https:\/\/twitter.com\/share\" class=\"twitter-share-button\" data-url=\"https:\/\/www.gigas-jp.com\/appnews\/archives\/6228\" data-text=\"\u9577\u671f\u9593Service\u3092\u8d77\u52d5\u3057\u305f\u3044\u6642\" data-via=\"GIGASJAPAN_APPS\" data-lang=\"ja\">Tweet<\/a><\/div>\n            <div class=\"wsbl_facebook_like\"><div id=\"fb-root\"><\/div><fb:like href=\"https:\/\/www.gigas-jp.com\/appnews\/archives\/6228\" layout=\"button_count\" action=\"like\" width=\"100\" share=\"false\" show_faces=\"false\" ><\/fb:like><\/div>\n            <div class=\"wsbl_facebook_send\"><div id=\"fb-root\"><\/div><fb:send href=\"https:\/\/www.gigas-jp.com\/appnews\/archives\/6228\" colorscheme=\"light\" ><\/fb:send><\/div>\n    <\/div>\n<br class='wp_social_bookmarking_light_clear' \/>\n","protected":false},"excerpt":{"rendered":"<p>\u3053\u3093\u306b\u3061\u306fTamamo\u3067\u3059\u3002 \u6bd4\u8f03\u7684\u9577\u3044\u671f\u9593Service\u3092\u8d77\u52d5\u3057\u305f\u3068\u304d\u304c\u3042\u308b\u3068\u601d\u3044\u307e\u3059\u3002 \u3057\u304b\u3057\u3001Android\u306f\u30e1\u30e2\u30ea\u4e0d\u8db3\u306a\u3069\u3067\u81ea\u52d5\u7684\u306bService\u3092kill\u3059\u308b\u5834\u5408\u304c\u3042\u308a\u3001 \u307e\u305f\u3001\u30a2\u30d7\u30ea\u8d77\u52d5\u5c65\u6b74\u304b\u3089\u30b9\u30ef\u30a4\u30d7\u3057\u3066\u6d88 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[100,12],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/posts\/6228"}],"collection":[{"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/comments?post=6228"}],"version-history":[{"count":14,"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/posts\/6228\/revisions"}],"predecessor-version":[{"id":6242,"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/posts\/6228\/revisions\/6242"}],"wp:attachment":[{"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/media?parent=6228"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/categories?post=6228"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/tags?post=6228"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}