{"id":524,"date":"2017-09-17T01:30:03","date_gmt":"2017-09-16T16:30:03","guid":{"rendered":"http:\/\/www.typea.info\/blog\/index.php\/2017\/09\/17\/android_service_vibrator\/"},"modified":"2017-09-17T01:30:03","modified_gmt":"2017-09-16T16:30:03","slug":"android_service_vibrator","status":"publish","type":"post","link":"https:\/\/www.typea.info\/blog\/index.php\/2017\/09\/17\/android_service_vibrator\/","title":{"rendered":"Android Service \u30b5\u30f3\u30d7\u30eb(Vibrator)"},"content":{"rendered":"<p><a href=\"https:\/\/www.typea.info\/blog\/wp-content\/uploads\/image\/Android-Service-Vibrator_12A0\/android_vibrate.jpg\"><img loading=\"lazy\" decoding=\"async\" width=\"178\" height=\"316\" title=\"android_vibrate\" style=\"display: inline; background-image: none;\" alt=\"android_vibrate\" src=\"https:\/\/www.typea.info\/blog\/wp-content\/uploads\/image\/Android-Service-Vibrator_12A0\/android_vibrate_thumb.jpg\" border=\"0\"><\/a><\/p>\n<p><\/p>\n<p><a href=\"https:\/\/www.typea.info\/blog\/index.php\/2017\/09\/17\/android_vibrator\">Fragment\u304b\u3089Vibrator\u3092\u5b9f\u884c\u3057\u305f<\/a>\u304c\u3001Activity\u3092\u7d42\u4e86\u3059\u308b\u3068\u3001\u30d0\u30a4\u30d6\u30ec\u30fc\u30b7\u30e7\u30f3\u3082\u7d42\u4e86\u3057\u3066\u3057\u307e\u3046\u305f\u3081\u3001\u30b5\u30fc\u30d3\u30b9\u306b\u51e6\u7406\u3092\u79fb\u884c\u3057\u52d5\u304d\u7d9a\u3051\u308b\u3088\u3046\u306b\u3059\u308b\u3002<\/p>\n<p><a title=\"https:\/\/github.com\/pppiroto\/KaigiUtil\/tree\/8d52606b8d69f2dd689888a186dda50ae530bdff\" href=\"https:\/\/github.com\/pppiroto\/KaigiUtil\/tree\/8d52606b8d69f2dd689888a186dda50ae530bdff\">https:\/\/github.com\/pppiroto\/KaigiUtil\/tree\/8d52606b8d69f2dd689888a186dda50ae530bdff<\/a><\/p>\n<h2>1.\u30b5\u30fc\u30d3\u30b9<\/h2>\n<p><a href=\"https:\/\/www.typea.info\/blog\/index.php\/2017\/09\/17\/android_vibrator\">Fragment\u3067\u3084\u3063\u3066\u3044\u305f\u51e6\u7406<\/a>\u3092\u3001\u30b5\u30fc\u30d3\u30b9\u306b\u6301\u3063\u3066\u304f\u308b<\/p>\n<pre class=\"prettyprint linenums\">package info.typea.kaigiutil;\r\n\r\nimport android.app.Service;\r\nimport android.content.Context;\r\nimport android.content.Intent;\r\nimport android.os.IBinder;\r\nimport android.os.Vibrator;\r\nimport android.widget.Toast;\r\n\r\npublic class SleepDefenderService extends Service {\r\n    private Vibrator vibrator;\r\n\r\n    public SleepDefenderService() {\r\n    }\r\n\r\n    @Override\r\n    public IBinder onBind(Intent intent) {\r\n        throw null;\r\n    }\r\n\r\n    @Override\r\n    public int onStartCommand(Intent intent, int flags, int startId) {\r\n        vibrator = (Vibrator)this.getSystemService(Context.VIBRATOR_SERVICE);\r\n        if (!vibrator.hasVibrator()) {\r\n            Toast.makeText(this, \"no vibrator.\", Toast.LENGTH_LONG).show();\r\n        } else {\r\n            Toast.makeText(this, \"start vibe as service\", Toast.LENGTH_SHORT).show();\r\n            long pattern[] = {1000, 100};\r\n            int repeatIndex = 0; \/\/ \u7e70\u308a\u8fd4\u3057\u958b\u59cb\u4f4d\u7f6e -1\u306e\u5834\u5408\u7e70\u308a\u8fd4\u3057\u306a\u3057\r\n            vibrator.vibrate(pattern, repeatIndex);\r\n        }\r\n        return START_STICKY;\r\n    }\r\n\r\n    @Override\r\n    public void onDestroy() {\r\n        super.onDestroy();\r\n        vibrator.cancel();\r\n        Toast.makeText(this, \"stop vibe as service\", Toast.LENGTH_SHORT).show();\r\n    }\r\n}\r\n<\/pre>\n<h3>1.1 AndroidManifest.xml \u306e application \u30a8\u30f3\u30c8\u30ea\u30fc\u306e\u5b50\u8981\u7d20\u306b\u3001\u4ee5\u4e0b\u3092\u8ffd\u52a0<\/h3>\n<pre class=\"prettyprint linenums\">        &lt;service\r\n            android:name=\".SleepDefenderService\"\r\n            android:enabled=\"true\"\r\n            android:exported=\"true\"&gt;&lt;\/service&gt;\r\n<\/pre>\n<h2>2.Fragment<\/h2>\n<p><a href=\"https:\/\/www.typea.info\/blog\/index.php\/2017\/09\/17\/android_vibrator\">Vibrator\u3092\u64cd\u4f5c\u3057\u3066\u3044\u305f\u3001Fragment<\/a>\u3092\u3001\u30b5\u30fc\u30d3\u30b9\u306e\u547c\u3073\u51fa\u3057\u3068\u505c\u6b62\u306b\u5909\u66f4<\/p>\n<pre class=\"prettyprint linenums\">package info.typea.kaigiutil;\r\n\r\nimport android.content.Intent;\r\nimport android.os.Bundle;\r\nimport android.support.annotation.Nullable;\r\nimport android.support.v4.app.Fragment;\r\nimport android.view.LayoutInflater;\r\nimport android.view.View;\r\nimport android.view.ViewGroup;\r\n\r\n\r\npublic class SleepDefenderFragment extends Fragment implements View.OnClickListener {\r\n    private static final String ARG_MSG = \"message\";\r\n\r\n    public SleepDefenderFragment() {\r\n    }\r\n\r\n    public static SleepDefenderFragment newInstance(String message) {\r\n        SleepDefenderFragment fragment = new SleepDefenderFragment();\r\n        Bundle args = new Bundle();\r\n        args.putString(ARG_MSG, message);\r\n        fragment.setArguments(args);\r\n        return fragment;\r\n    }\r\n\r\n    @Override\r\n    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {\r\n        super.onCreateView(inflater, container, savedInstanceState);\r\n        return inflater.inflate(R.layout.fragment_sleep_defender, container, false);\r\n    }\r\n\r\n    @Override\r\n    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {\r\n        super.onViewCreated(view, savedInstanceState);\r\n\r\n        view.findViewById(R.id.btn_vib_start).setOnClickListener(this);\r\n        view.findViewById(R.id.btn_vib_stop).setOnClickListener(this);\r\n    }\r\n\r\n    @Override\r\n    public void onClick(View v) {\r\n        Intent intent = new Intent(this.getContext(), SleepDefenderService.class);\r\n\r\n        switch(v.getId()) {\r\n            case R.id.btn_vib_start:\r\n                this.getContext().startService(intent);\r\n                break;\r\n\r\n            case R.id.btn_vib_stop:\r\n                this.getContext().stopService(intent);\r\n                break;\r\n        }\r\n    }\r\n}\r\n<\/pre>\n<p><a href=\"https:\/\/www.amazon.co.jp\/T2-8-0-PRO-%E2%80%BBWi-Fi%E3%83%A2%E3%83%87%E3%83%AB-16GB%E3%80%90%E6%97%A5%E6%9C%AC%E6%AD%A3%E8%A6%8F%E4%BB%A3%E7%90%86%E5%BA%97%E5%93%81%E3%80%91\/dp\/B01MTSP60Q\/ref=as_li_ss_il?s=computers&amp;ie=UTF8&amp;qid=1505579295&amp;sr=1-4&amp;keywords=android+%E3%82%BF%E3%83%96%E3%83%AC%E3%83%83%E3%83%88&amp;linkCode=li2&amp;tag=typea09-22&amp;linkId=a810b9a8f123810194b176922868ae8d\" target=\"_blank\"><img decoding=\"async\" src=\"\/\/ws-fe.amazon-adsystem.com\/widgets\/q?_encoding=UTF8&amp;ASIN=B01MTSP60Q&amp;Format=_SL160_&amp;ID=AsinImage&amp;MarketPlace=JP&amp;ServiceVersion=20070822&amp;WS=1&amp;tag=typea09-22\" border=\"0\"><\/a><img loading=\"lazy\" decoding=\"async\" width=\"1\" height=\"1\" style=\"margin: 0px !important; border: currentcolor !important; border-image: none !important;\" alt=\"\" src=\"https:\/\/ir-jp.amazon-adsystem.com\/e\/ir?t=typea09-22&amp;l=li2&amp;o=9&amp;a=B01MTSP60Q\" border=\"0\"><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Fragment\u304b\u3089Vibrator\u3092\u5b9f\u884c\u3057\u305f\u304c\u3001Activity\u3092\u7d42\u4e86\u3059\u308b\u3068\u3001\u30d0\u30a4\u30d6\u30ec\u30fc\u30b7\u30e7\u30f3\u3082\u7d42\u4e86\u3057\u3066\u3057\u307e\u3046\u305f\u3081\u3001\u30b5\u30fc\u30d3\u30b9\u306b\u51e6\u7406\u3092\u79fb\u884c\u3057\u52d5\u304d\u7d9a\u3051\u308b\u3088\u3046\u306b\u3059\u308b\u3002 https:\/\/github.com\/pppiroto\/ [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"sns_share_botton_hide":"","vkExUnit_sns_title":"","_vk_print_noindex":"","sitemap_hide":"","_veu_custom_css":"","veu_display_promotion_alert":"","vkexunit_cta_each_option":"","footnotes":""},"categories":[39],"tags":[],"class_list":["post-524","post","type-post","status-publish","format-standard","hentry","category-android"],"veu_head_title_object":{"title":"","add_site_title":""},"_links":{"self":[{"href":"https:\/\/www.typea.info\/blog\/index.php\/wp-json\/wp\/v2\/posts\/524","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.typea.info\/blog\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.typea.info\/blog\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.typea.info\/blog\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.typea.info\/blog\/index.php\/wp-json\/wp\/v2\/comments?post=524"}],"version-history":[{"count":0,"href":"https:\/\/www.typea.info\/blog\/index.php\/wp-json\/wp\/v2\/posts\/524\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.typea.info\/blog\/index.php\/wp-json\/wp\/v2\/media?parent=524"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.typea.info\/blog\/index.php\/wp-json\/wp\/v2\/categories?post=524"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.typea.info\/blog\/index.php\/wp-json\/wp\/v2\/tags?post=524"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}