質問編集履歴

3

題名変更

2019/09/03 12:33

投稿

old_dog
old_dog

スコア51

test CHANGED
@@ -1 +1 @@
1
- Android ServiceでThread処理をし、Thread処理の終了前にThreadを破棄してServiceも停止したい
1
+ Android Thread処理の終了前にThreadを破棄したい
test CHANGED
File without changes

2

コード修正

2019/09/03 12:33

投稿

old_dog
old_dog

スコア51

test CHANGED
File without changes
test CHANGED
@@ -172,7 +172,7 @@
172
172
 
173
173
  }).start();
174
174
 
175
- stopSelf();
175
+
176
176
 
177
177
  return START_NOT_STICKY;
178
178
 

1

コード追加

2019/09/03 12:31

投稿

old_dog
old_dog

スコア51

test CHANGED
File without changes
test CHANGED
@@ -1,6 +1,6 @@
1
1
  <質問事項>
2
2
 
3
- ServiceでThread処理する。
3
+ ServiceでThread処理するコードを書いていますコード中で、
4
4
 
5
5
  Threadが稼働しているときに、Serviceを停止し、併せてThreadのタスクも破棄したいと考えています。ServiceとThreadの両方を同時に停止、破棄するには、どのような方法があるでしょうか?
6
6
 
@@ -8,8 +8,186 @@
8
8
 
9
9
  <現状>
10
10
 
11
- しかしながら、Service内でThreadを生成して処理させると、Serviceとは別スレッドになってしまい、Serviceを停止してもThreadのタスクが破棄されず、処理し続けてしまうとう状況です。
11
+ Service内でThreadを生成して処理させると、Serviceとは別スレッドになってしまい、Serviceを停止してもThreadのタスクが破棄されず、処理し続けてしまいす。
12
12
 
13
13
 
14
14
 
15
- ろしくお願いします。
15
+ 以下、コードを記載しますので、ろしくお願いします。
16
+
17
+
18
+
19
+ <MainActivity.java>
20
+
21
+
22
+
23
+ ```MainActivity
24
+
25
+ public class MainActivity extends AppCompatActivity {
26
+
27
+
28
+
29
+ @Override
30
+
31
+ protected void onCreate(Bundle savedInstanceState) {
32
+
33
+ super.onCreate(savedInstanceState);
34
+
35
+ setContentView(R.layout.activity_main);
36
+
37
+
38
+
39
+ Button btn_start = findViewById(R.id.btn_start);
40
+
41
+ Button btn_stop = findViewById(R.id.btn_stop);
42
+
43
+ final Intent intent = new Intent(this,MyService.class);
44
+
45
+
46
+
47
+ btn_start.setOnClickListener(new View.OnClickListener(){
48
+
49
+ @Override
50
+
51
+ public void onClick(View view) {
52
+
53
+ if(Build.VERSION.SDK_INT >=26){
54
+
55
+ startForegroundService(intent);
56
+
57
+ }else{
58
+
59
+ startService(intent);
60
+
61
+ }
62
+
63
+ }
64
+
65
+ });
66
+
67
+
68
+
69
+ btn_stop.setOnClickListener(new View.OnClickListener(){
70
+
71
+ @Override
72
+
73
+ public void onClick(View view) {
74
+
75
+ stopService(intent);
76
+
77
+ }
78
+
79
+ });
80
+
81
+
82
+
83
+ }
84
+
85
+ }
86
+
87
+ ```
88
+
89
+
90
+
91
+ <MyService.java>
92
+
93
+ ```MyService
94
+
95
+ public class MyService extends Service {
96
+
97
+ public MyService() {
98
+
99
+ }
100
+
101
+
102
+
103
+ public int onStartCommand(Intent intent,int flags,int startId){
104
+
105
+ Notification notification;
106
+
107
+ Intent intent_Main = new Intent(this,MainActivity.class);
108
+
109
+ PendingIntent pendingIntent = PendingIntent.getActivity(this,0,intent_Main,0);
110
+
111
+ NotificationCompat.Builder builder = new NotificationCompat.Builder(this,"id");
112
+
113
+ notification = builder.setSmallIcon(R.drawable.notification_icon)
114
+
115
+ .setContentTitle("NotificationTitle")
116
+
117
+ .setContentText("NotificationContent")
118
+
119
+ .setContentIntent(pendingIntent)
120
+
121
+ .build();
122
+
123
+
124
+
125
+ if(Build.VERSION.SDK_INT >=26){
126
+
127
+ int importance = NotificationManager.IMPORTANCE_LOW;
128
+
129
+ NotificationChannel channel = new NotificationChannel("id","name",importance);
130
+
131
+ NotificationManager manager = getSystemService(NotificationManager.class);
132
+
133
+ manager.createNotificationChannel(channel);
134
+
135
+ startForeground(2,notification);
136
+
137
+ }else{
138
+
139
+ notification = builder.setPriority(NotificationCompat.PRIORITY_LOW).build();
140
+
141
+ NotificationManagerCompat managerCompat = NotificationManagerCompat.from(this);
142
+
143
+ managerCompat.notify(1,notification);
144
+
145
+ }
146
+
147
+
148
+
149
+ new Thread(new Runnable(){
150
+
151
+ @Override
152
+
153
+ public void run() {
154
+
155
+ for(int i=0;i<100;i++){
156
+
157
+ try {
158
+
159
+ sleep(1000);
160
+
161
+ } catch (InterruptedException e) {
162
+
163
+ e.printStackTrace();
164
+
165
+ }
166
+
167
+ Log.d("MSG","IN THREAD");
168
+
169
+ }
170
+
171
+ }
172
+
173
+ }).start();
174
+
175
+ stopSelf();
176
+
177
+ return START_NOT_STICKY;
178
+
179
+ }
180
+
181
+
182
+
183
+ @Override
184
+
185
+ public IBinder onBind(Intent intent) {
186
+
187
+ return null;
188
+
189
+ }
190
+
191
+ }
192
+
193
+ ```