teratail header banner
teratail header banner
質問するログイン新規登録

質問編集履歴

3

題名変更

2019/09/03 12:33

投稿

old_dog
old_dog

スコア51

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

2

コード修正

2019/09/03 12:33

投稿

old_dog
old_dog

スコア51

title CHANGED
File without changes
body CHANGED
@@ -85,7 +85,7 @@
85
85
  }
86
86
  }
87
87
  }).start();
88
- stopSelf();
88
+
89
89
  return START_NOT_STICKY;
90
90
  }
91
91
 

1

コード追加

2019/09/03 12:31

投稿

old_dog
old_dog

スコア51

title CHANGED
File without changes
body CHANGED
@@ -1,8 +1,97 @@
1
1
  <質問事項>
2
- ServiceでThread処理する。
2
+ ServiceでThread処理するコードを書いていますコード中で、
3
3
  Threadが稼働しているときに、Serviceを停止し、併せてThreadのタスクも破棄したいと考えています。ServiceとThreadの両方を同時に停止、破棄するには、どのような方法があるでしょうか?
4
4
 
5
5
  <現状>
6
- しかしながら、Service内でThreadを生成して処理させると、Serviceとは別スレッドになってしまい、Serviceを停止してもThreadのタスクが破棄されず、処理し続けてしまうとう状況です。
6
+ Service内でThreadを生成して処理させると、Serviceとは別スレッドになってしまい、Serviceを停止してもThreadのタスクが破棄されず、処理し続けてしまいす。
7
7
 
8
- ろしくお願いします。
8
+ 以下、コードを記載しますので、ろしくお願いします。
9
+
10
+ <MainActivity.java>
11
+
12
+ ```MainActivity
13
+ public class MainActivity extends AppCompatActivity {
14
+
15
+ @Override
16
+ protected void onCreate(Bundle savedInstanceState) {
17
+ super.onCreate(savedInstanceState);
18
+ setContentView(R.layout.activity_main);
19
+
20
+ Button btn_start = findViewById(R.id.btn_start);
21
+ Button btn_stop = findViewById(R.id.btn_stop);
22
+ final Intent intent = new Intent(this,MyService.class);
23
+
24
+ btn_start.setOnClickListener(new View.OnClickListener(){
25
+ @Override
26
+ public void onClick(View view) {
27
+ if(Build.VERSION.SDK_INT >=26){
28
+ startForegroundService(intent);
29
+ }else{
30
+ startService(intent);
31
+ }
32
+ }
33
+ });
34
+
35
+ btn_stop.setOnClickListener(new View.OnClickListener(){
36
+ @Override
37
+ public void onClick(View view) {
38
+ stopService(intent);
39
+ }
40
+ });
41
+
42
+ }
43
+ }
44
+ ```
45
+
46
+ <MyService.java>
47
+ ```MyService
48
+ public class MyService extends Service {
49
+ public MyService() {
50
+ }
51
+
52
+ public int onStartCommand(Intent intent,int flags,int startId){
53
+ Notification notification;
54
+ Intent intent_Main = new Intent(this,MainActivity.class);
55
+ PendingIntent pendingIntent = PendingIntent.getActivity(this,0,intent_Main,0);
56
+ NotificationCompat.Builder builder = new NotificationCompat.Builder(this,"id");
57
+ notification = builder.setSmallIcon(R.drawable.notification_icon)
58
+ .setContentTitle("NotificationTitle")
59
+ .setContentText("NotificationContent")
60
+ .setContentIntent(pendingIntent)
61
+ .build();
62
+
63
+ if(Build.VERSION.SDK_INT >=26){
64
+ int importance = NotificationManager.IMPORTANCE_LOW;
65
+ NotificationChannel channel = new NotificationChannel("id","name",importance);
66
+ NotificationManager manager = getSystemService(NotificationManager.class);
67
+ manager.createNotificationChannel(channel);
68
+ startForeground(2,notification);
69
+ }else{
70
+ notification = builder.setPriority(NotificationCompat.PRIORITY_LOW).build();
71
+ NotificationManagerCompat managerCompat = NotificationManagerCompat.from(this);
72
+ managerCompat.notify(1,notification);
73
+ }
74
+
75
+ new Thread(new Runnable(){
76
+ @Override
77
+ public void run() {
78
+ for(int i=0;i<100;i++){
79
+ try {
80
+ sleep(1000);
81
+ } catch (InterruptedException e) {
82
+ e.printStackTrace();
83
+ }
84
+ Log.d("MSG","IN THREAD");
85
+ }
86
+ }
87
+ }).start();
88
+ stopSelf();
89
+ return START_NOT_STICKY;
90
+ }
91
+
92
+ @Override
93
+ public IBinder onBind(Intent intent) {
94
+ return null;
95
+ }
96
+ }
97
+ ```