Android Samples
Revision | 9f1575b266817e012ff89d5b7f0893d19716d2de (tree) |
---|---|
Zeit | 2014-06-03 17:13:59 |
Autor | Masahiko, SAWAI <say@user...> |
Commiter | Masahiko, SAWAI |
Added intent-service-hello module
@@ -0,0 +1,24 @@ | ||
1 | +<?xml version="1.0" encoding="utf-8"?> | |
2 | +<manifest xmlns:android="http://schemas.android.com/apk/res/android" | |
3 | + package="com.example.hello.android.intent_service_hello" | |
4 | + android:versionCode="1" | |
5 | + android:versionName="1.0"> | |
6 | + <application android:label="@string/app_name"> | |
7 | + | |
8 | + <!-- Activity Manifest --> | |
9 | + <activity android:name="MainActivity" | |
10 | + android:label="@string/app_name"> | |
11 | + <intent-filter> | |
12 | + <action android:name="android.intent.action.MAIN" /> | |
13 | + <category android:name="android.intent.category.LAUNCHER" /> | |
14 | + </intent-filter> | |
15 | + </activity> | |
16 | + | |
17 | + <!-- Service Manifest --> | |
18 | + <service | |
19 | + android:name="CountDownService" | |
20 | + android:label="Count Down Service" | |
21 | + /> | |
22 | + | |
23 | + </application> | |
24 | +</manifest> |
@@ -0,0 +1,46 @@ | ||
1 | + | |
2 | +all : package | |
3 | + | |
4 | +#################### build | |
5 | + | |
6 | +compile : | |
7 | + mvn $@ | |
8 | + | |
9 | +gen : | |
10 | + mvn android:generate-sources | |
11 | + | |
12 | +package : | |
13 | + mvn $@ | |
14 | + | |
15 | +release : | |
16 | + mvn clean -Prelease package | |
17 | + | |
18 | +install : | |
19 | + mvn $@ | |
20 | + | |
21 | +deploy : | |
22 | + mvn package android:deploy | |
23 | + | |
24 | +undeploy : | |
25 | + mvn android:undeploy | |
26 | + | |
27 | +rebuild : | |
28 | + mvn clean package | |
29 | + | |
30 | +redeploy : | |
31 | + mvn clean package android:deploy | |
32 | + | |
33 | +#################### emulator | |
34 | + | |
35 | +start : | |
36 | + mvn android:emulator-start | |
37 | + | |
38 | +stop : | |
39 | + mvn android:emulator-stop | |
40 | + | |
41 | +#################### | |
42 | +dist : | |
43 | + mvn assembly:assembly | |
44 | + | |
45 | +clean : | |
46 | + mvn $@ |
@@ -0,0 +1,26 @@ | ||
1 | +! intent-service-hello | |
2 | + | |
3 | +IntentService を使って非同期処理を実現するサンプル. | |
4 | + | |
5 | +* http://techbooster.jpn.org/andriod/application/1570/ - 参考 | |
6 | + | |
7 | +* タスクは startService() で呼び出すたびにキューイングされる | |
8 | +* IntentService で同時に作業できるタスクは1つ | |
9 | +* キューにタスクが一つもなくなったらサービスは自動で終了する | |
10 | +* バックグラウンドで動作し続けるというよりはバッチ処理的なものに使う | |
11 | + | |
12 | +!! ビルドターゲット | |
13 | + | |
14 | +http://maven-android-plugin-m2site.googlecode.com/svn/plugin-info.html | |
15 | + | |
16 | +* mvn compile - アプリケーションのコンパイル | |
17 | +* mvn package - apk の作成 | |
18 | +* mvn clean - ビルドファイルの削除 | |
19 | +* mvn install - maven の local リポジトリにインストール | |
20 | + | |
21 | +* mvn android:deploy - エミュレータやデバイスにインストール | |
22 | +* mvn android:generate-sources | |
23 | + | |
24 | +* mvn install - アプリケーションのインストール | |
25 | +* mvn uninstall - アプリケーションのアンインストール | |
26 | + |
@@ -0,0 +1,142 @@ | ||
1 | +<?xml version="1.0" encoding="UTF-8" ?> | |
2 | +<project | |
3 | + xmlns="http://maven.apache.org/POM/4.0.0" | |
4 | + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | |
5 | + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd" | |
6 | +> | |
7 | + <modelVersion>4.0.0</modelVersion> | |
8 | + <groupId>com.example.hello</groupId> | |
9 | + <artifactId>intent-service-hello</artifactId> | |
10 | + <packaging>apk</packaging> | |
11 | + <version>1.0.0</version> | |
12 | + <name>IntentService Hello</name> | |
13 | + <url>http://www.example.com/intent-service-hello/</url> | |
14 | + | |
15 | + <parent> | |
16 | + <groupId>com.example.hello</groupId> | |
17 | + <artifactId>android-samples</artifactId> | |
18 | + <version>1.0.0</version> | |
19 | + </parent> | |
20 | + | |
21 | + <dependencies> | |
22 | + | |
23 | + <dependency> | |
24 | + <groupId>android</groupId> | |
25 | + <artifactId>android</artifactId> | |
26 | + <version>1.5_r4</version> | |
27 | + <!-- | |
28 | + <version>1.5_r3</version> | |
29 | + <version>1.5_r4</version> | |
30 | + <version>1.6_r2</version> | |
31 | + <version>2.1.2</version> | |
32 | + <version>2.1_r1</version> | |
33 | + <version>2.2.1</version> | |
34 | + <version>2.3.1</version> | |
35 | + <version>2.3.3</version> | |
36 | + <version>4.0.1.2</version> | |
37 | + --> | |
38 | + <scope>provided</scope> | |
39 | + </dependency> | |
40 | + | |
41 | + <dependency> | |
42 | + <groupId>junit</groupId> | |
43 | + <artifactId>junit</artifactId> | |
44 | + <scope>test</scope> | |
45 | + </dependency> | |
46 | + </dependencies> | |
47 | + | |
48 | + <build> | |
49 | + <sourceDirectory>src</sourceDirectory> | |
50 | + | |
51 | + <plugins> | |
52 | + <plugin> | |
53 | + <groupId>com.jayway.maven.plugins.android.generation2</groupId> | |
54 | + <artifactId>android-maven-plugin</artifactId> | |
55 | + <configuration> | |
56 | + <sdk> | |
57 | + <!--<path>${env.ANDROID_HOME}</path>--> | |
58 | + <platform>4</platform> | |
59 | + </sdk> | |
60 | + </configuration> | |
61 | + <extensions>true</extensions> | |
62 | + </plugin> | |
63 | + | |
64 | + <!-- mvn compile --> | |
65 | + <plugin> | |
66 | + <groupId>org.apache.maven.plugins</groupId> | |
67 | + <artifactId>maven-compiler-plugin</artifactId> | |
68 | + <configuration> | |
69 | + <source>1.5</source> | |
70 | + <target>1.5</target> | |
71 | + <encoding>UTF-8</encoding> | |
72 | + </configuration> | |
73 | + </plugin> | |
74 | + | |
75 | + <!-- mvn assembly:assembly --> | |
76 | + <plugin> | |
77 | + <groupId>org.apache.maven.plugins</groupId> | |
78 | + <artifactId>maven-assembly-plugin</artifactId> | |
79 | + <configuration> | |
80 | + <descriptorRefs> | |
81 | + <!-- | |
82 | + <descriptorRef>jar-with-dependencies</descriptorRef> | |
83 | + <descriptorRef>bin</descriptorRef> | |
84 | + <descriptorRef>src</descriptorRef> | |
85 | + --> | |
86 | + <descriptorRef>project</descriptorRef> | |
87 | + </descriptorRefs> | |
88 | + </configuration> | |
89 | + </plugin> | |
90 | + | |
91 | + <!-- mvn resources:resources --> | |
92 | + <plugin> | |
93 | + <groupId>org.apache.maven.plugins</groupId> | |
94 | + <artifactId>maven-resources-plugin</artifactId> | |
95 | + <configuration> | |
96 | + <encoding>UTF-8</encoding> | |
97 | + </configuration> | |
98 | + </plugin> | |
99 | + | |
100 | + <!-- mvn site --> | |
101 | + <plugin> | |
102 | + <groupId>org.apache.maven.plugins</groupId> | |
103 | + <artifactId>maven-site-plugin</artifactId> | |
104 | + <configuration> | |
105 | + <locales>en</locales> | |
106 | + <inputEncoding>UTF-8</inputEncoding> | |
107 | + <outputEncoding>UTF-8</outputEncoding> | |
108 | + | |
109 | + <reportPlugins> | |
110 | + <plugin> | |
111 | + <groupId>org.apache.maven.plugins</groupId> | |
112 | + <artifactId>maven-project-info-reports-plugin</artifactId> | |
113 | + </plugin> | |
114 | + | |
115 | + <!-- mvn javadoc:javadoc --> | |
116 | + <plugin> | |
117 | + <groupId>org.apache.maven.plugins</groupId> | |
118 | + <artifactId>maven-javadoc-plugin</artifactId> | |
119 | + <configuration> | |
120 | + <charset>utf-8</charset> | |
121 | + </configuration> | |
122 | + </plugin> | |
123 | + </reportPlugins> | |
124 | + </configuration> | |
125 | + </plugin> | |
126 | + | |
127 | + </plugins> | |
128 | + | |
129 | + <!-- mvn resources:resources --> | |
130 | + <resources> | |
131 | + <resource> | |
132 | + <directory>res</directory> | |
133 | + <filtering>false</filtering> | |
134 | + <includes> | |
135 | + <include>**/*.properties</include> | |
136 | + </includes> | |
137 | + </resource> | |
138 | + </resources> | |
139 | + </build> | |
140 | + | |
141 | + | |
142 | +</project> |
@@ -0,0 +1,22 @@ | ||
1 | +<?xml version="1.0" encoding="utf-8"?> | |
2 | +<LinearLayout | |
3 | + xmlns:android="http://schemas.android.com/apk/res/android" | |
4 | + android:orientation="vertical" | |
5 | + android:layout_width="fill_parent" | |
6 | + android:layout_height="fill_parent" | |
7 | + android:padding="10dip" | |
8 | +> | |
9 | + <Button | |
10 | + android:id="@+id/start_count_down_button" | |
11 | + android:layout_width="fill_parent" | |
12 | + android:layout_height="wrap_content" | |
13 | + android:text="@string/start_count_down" | |
14 | + /> | |
15 | + <Button | |
16 | + android:id="@+id/start_count_down_3_button" | |
17 | + android:layout_width="fill_parent" | |
18 | + android:layout_height="wrap_content" | |
19 | + android:text="@string/start_count_down_3" | |
20 | + /> | |
21 | +</LinearLayout> | |
22 | + |
@@ -0,0 +1,6 @@ | ||
1 | +<?xml version="1.0" encoding="utf-8"?> | |
2 | +<resources> | |
3 | + <string name="app_name">Intent Service Hello</string> | |
4 | + <string name="start_count_down">Start Count Down</string> | |
5 | + <string name="start_count_down_3">Start Count Down 3</string> | |
6 | +</resources> |
@@ -0,0 +1,95 @@ | ||
1 | +/* | |
2 | + * The MIT License | |
3 | + * | |
4 | + * Copyright 2014 Masahiko, SAWAI <masahiko.sawai@gmail.com>. | |
5 | + * | |
6 | + * Permission is hereby granted, free of charge, to any person obtaining a copy | |
7 | + * of this software and associated documentation files (the "Software"), to deal | |
8 | + * in the Software without restriction, including without limitation the rights | |
9 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
10 | + * copies of the Software, and to permit persons to whom the Software is | |
11 | + * furnished to do so, subject to the following conditions: | |
12 | + * | |
13 | + * The above copyright notice and this permission notice shall be included in | |
14 | + * all copies or substantial portions of the Software. | |
15 | + * | |
16 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
17 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
18 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
19 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
20 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
21 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | |
22 | + * THE SOFTWARE. | |
23 | + */ | |
24 | +package com.example.hello.android.intent_service_hello; | |
25 | + | |
26 | +import android.app.IntentService; | |
27 | +import android.content.Intent; | |
28 | +import android.util.Log; | |
29 | + | |
30 | +/** | |
31 | + * | |
32 | + * @author sawai | |
33 | + */ | |
34 | +public class CountDownService extends IntentService | |
35 | +{ | |
36 | + public static final String EXTRA_COUNT = "count"; | |
37 | + private static final long COUNT_INTERVAL_MSEC = 1000; // 1sec | |
38 | + private static final int DEFAULT_COUNT = 10; | |
39 | + private static final String LOG_TAG = "XXX"; | |
40 | + | |
41 | + public CountDownService(String name) | |
42 | + { | |
43 | + super(name); | |
44 | + } | |
45 | + | |
46 | + public CountDownService() | |
47 | + { | |
48 | + super(CountDownService.class.getName()); | |
49 | + } | |
50 | + | |
51 | + @Override | |
52 | + public void onCreate() | |
53 | + { | |
54 | + Log.i(LOG_TAG, "CountDownService#onCreate() : Hello"); | |
55 | + super.onCreate(); | |
56 | + Log.i(LOG_TAG, "CountDownService#onCreate() : Bye"); | |
57 | + } | |
58 | + | |
59 | + @Override | |
60 | + public void onDestroy() | |
61 | + { | |
62 | + Log.i(LOG_TAG, "CountDownService#onDestroy() : Hello"); | |
63 | + super.onDestroy(); | |
64 | + Log.i(LOG_TAG, "CountDownService#onDestroy() : Bye"); | |
65 | + } | |
66 | + | |
67 | + @Override | |
68 | + public void onStart(Intent intent, int startId) | |
69 | + { | |
70 | + Log.i(LOG_TAG, "CountDownService#onStart() : Hello"); | |
71 | + super.onStart(intent, startId); | |
72 | + Log.i(LOG_TAG, "CountDownService#onStart() : bye"); | |
73 | + } | |
74 | + | |
75 | + @Override | |
76 | + protected void onHandleIntent(Intent intent) | |
77 | + { | |
78 | + Log.i(LOG_TAG, "CountDownService#onHandleIntent() : Hello"); | |
79 | + int count = intent.getIntExtra(EXTRA_COUNT, DEFAULT_COUNT); | |
80 | + for (int i = count; i >= 0; i--) | |
81 | + { | |
82 | + Log.i(LOG_TAG, "CountDownService#onHandleIntent() count => " + i); | |
83 | + try | |
84 | + { | |
85 | + Thread.sleep(COUNT_INTERVAL_MSEC); | |
86 | + } | |
87 | + catch (InterruptedException ex) | |
88 | + { | |
89 | + Log.e(LOG_TAG, "sleep failed.", ex); | |
90 | + } | |
91 | + } | |
92 | + | |
93 | + Log.i(LOG_TAG, "CountDownService#onHandleIntent() : Bye "); | |
94 | + } | |
95 | +} |
@@ -0,0 +1,68 @@ | ||
1 | +/* | |
2 | + * The MIT License | |
3 | + * | |
4 | + * Copyright 2014 Masahiko, SAWAI <masahiko.sawai@gmail.com>. | |
5 | + * | |
6 | + * Permission is hereby granted, free of charge, to any person obtaining a copy | |
7 | + * of this software and associated documentation files (the "Software"), to deal | |
8 | + * in the Software without restriction, including without limitation the rights | |
9 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
10 | + * copies of the Software, and to permit persons to whom the Software is | |
11 | + * furnished to do so, subject to the following conditions: | |
12 | + * | |
13 | + * The above copyright notice and this permission notice shall be included in | |
14 | + * all copies or substantial portions of the Software. | |
15 | + * | |
16 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
17 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
18 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
19 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
20 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
21 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | |
22 | + * THE SOFTWARE. | |
23 | + */ | |
24 | +package com.example.hello.android.intent_service_hello; | |
25 | + | |
26 | +import android.app.Activity; | |
27 | +import android.content.Intent; | |
28 | +import android.os.Bundle; | |
29 | +import android.view.View; | |
30 | +import android.view.View.OnClickListener; | |
31 | +import android.widget.Button; | |
32 | +import android.widget.Toast; | |
33 | + | |
34 | +public class MainActivity extends Activity implements OnClickListener | |
35 | +{ | |
36 | + | |
37 | + @Override | |
38 | + protected void onCreate(Bundle savedInstanceState) | |
39 | + { | |
40 | + super.onCreate(savedInstanceState); | |
41 | + setContentView(R.layout.main); | |
42 | + | |
43 | + Button startCountDownButton = (Button) findViewById(R.id.start_count_down_button); | |
44 | + startCountDownButton.setOnClickListener(this); | |
45 | + | |
46 | + Button startCountDown3Button = (Button) findViewById(R.id.start_count_down_3_button); | |
47 | + startCountDown3Button.setOnClickListener(this); | |
48 | + } | |
49 | + | |
50 | + public void onClick(View view) | |
51 | + { | |
52 | + Intent intent; | |
53 | + switch (view.getId()) | |
54 | + { | |
55 | + case R.id.start_count_down_button: | |
56 | + Toast.makeText(getApplicationContext(), "Start Count Down!", Toast.LENGTH_SHORT).show(); | |
57 | + intent = new Intent(MainActivity.this, CountDownService.class); | |
58 | + startService(intent); | |
59 | + break; | |
60 | + case R.id.start_count_down_3_button: | |
61 | + Toast.makeText(getApplicationContext(), "Start Count Down 3!", Toast.LENGTH_SHORT).show(); | |
62 | + intent = new Intent(MainActivity.this, CountDownService.class); | |
63 | + intent.putExtra(CountDownService.EXTRA_COUNT, 3); | |
64 | + startService(intent); | |
65 | + break; | |
66 | + } | |
67 | + } | |
68 | +} |
@@ -72,6 +72,7 @@ | ||
72 | 72 | |
73 | 73 | <module>imageview-hello</module> |
74 | 74 | <module>imageview-zoom</module> |
75 | + <module>intent-service-hello</module> | |
75 | 76 | |
76 | 77 | <module>layout-framelayout-hello</module> |
77 | 78 | <module>layout-tablelayout-hello</module> |