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

質問編集履歴

1

コード追加しました

2018/02/25 10:58

投稿

rainysk
rainysk

スコア27

title CHANGED
File without changes
body CHANGED
@@ -1,1 +1,86 @@
1
- runしたあとの30~40秒の間foregroundのserviceからホームボタンを押したときにIntentServiceを使用できなくなっています。仕様ですか?
1
+ runしたあとの30~40秒の間foregroundのserviceからホームボタンを押したときにIntentServiceを使用できなくなっています。仕様ですか?
2
+
3
+ #コード1service
4
+
5
+ @Override
6
+ public void onCreate() {
7
+
8
+ super.onCreate();
9
+ final SharedPreferences sn=getSharedPreferences("today",MODE_MULTI_PROCESS);
10
+ final SharedPreferences.Editor editor=sn.edit();
11
+ editor.putInt("start",1).commit();
12
+
13
+ m_HomeButtonReceive = new HomeButtonReceive();
14
+ IntentFilter iFilter = new IntentFilter();
15
+ iFilter.addAction(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
16
+ this.registerReceiver(m_HomeButtonReceive, iFilter);
17
+
18
+
19
+
20
+
21
+ }
22
+
23
+ public int onStartCommand(Intent intent, int flags, int startId) {
24
+ Notification notification=new NotificationCompat.Builder(this)
25
+ .setSmallIcon(R.drawable.icon)
26
+ .setContentText("×××" )
27
+ .setContentTitle("×××").build();
28
+ NotificationManager manager=(NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
29
+ manager.notify(1,notification);
30
+
31
+
32
+ startForeground(1,notification);
33
+
34
+
35
+ return START_STICKY;
36
+
37
+
38
+ }
39
+
40
+ @Override
41
+ public IBinder onBind(Intent intent) {
42
+ return null;
43
+ }
44
+
45
+ public class HomeButtonReceive extends BroadcastReceiver {
46
+
47
+
48
+ @Override
49
+ public void onReceive(Context arg0, Intent arg1) {
50
+
51
+ Bundle bundle = (arg1 == null) ? null : arg1.getExtras();
52
+ Object o = (bundle == null) ? null : bundle.get("reason");
53
+ Toast.makeText(arg0.getApplicationContext(),(String)o,Toast.LENGTH_LONG).show();
54
+ if (((String)o).equals("homekey")) {//o instanceof String &&
55
+
56
+ if (sn.getInt("×××", 0) != 1)
57
+ startService(new Intent(getApplicationContext(), APPintentservice.class));
58
+ }
59
+ }
60
+ }
61
+
62
+
63
+
64
+ #コード2 intentservice
65
+ public class APPintentservice extends IntentService {
66
+
67
+ public APPintentservice(String name) {
68
+ super(name);
69
+ }
70
+
71
+ public APPintentservice() {
72
+ super("APPintentservice");
73
+ }
74
+
75
+
76
+ @Override
77
+ protected void onHandleIntent(Intent data) {
78
+ SystemClock.sleep(100);
79
+ Intent intent = new Intent();
80
+ intent.setAction("×××");
81
+ sendBroadcast(intent);
82
+ }
83
+ }
84
+
85
+
86
+ 自分のコードから抜粋してきたので抜けがあるかもしてません