runしたあとの30~40秒の間foregroundのserviceからホームボタンを押したときにIntentServiceを使用できなくなっています。仕様ですか?
#コード1service
@Override
public void onCreate() {
super.onCreate(); final SharedPreferences sn=getSharedPreferences("today",MODE_MULTI_PROCESS); final SharedPreferences.Editor editor=sn.edit(); editor.putInt("start",1).commit(); m_HomeButtonReceive = new HomeButtonReceive(); IntentFilter iFilter = new IntentFilter(); iFilter.addAction(Intent.ACTION_CLOSE_SYSTEM_DIALOGS); this.registerReceiver(m_HomeButtonReceive, iFilter); } public int onStartCommand(Intent intent, int flags, int startId) { Notification notification=new NotificationCompat.Builder(this) .setSmallIcon(R.drawable.icon) .setContentText("×××" ) .setContentTitle("×××").build(); NotificationManager manager=(NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE); manager.notify(1,notification); startForeground(1,notification); return START_STICKY; }
@Override
public IBinder onBind(Intent intent) {
return null;
}
public class HomeButtonReceive extends BroadcastReceiver { @Override public void onReceive(Context arg0, Intent arg1) { Bundle bundle = (arg1 == null) ? null : arg1.getExtras(); Object o = (bundle == null) ? null : bundle.get("reason"); Toast.makeText(arg0.getApplicationContext(),(String)o,Toast.LENGTH_LONG).show(); if (((String)o).equals("homekey")) {//o instanceof String && if (sn.getInt("×××", 0) != 1) startService(new Intent(getApplicationContext(), APPintentservice.class)); } } }
#コード2 intentservice
public class APPintentservice extends IntentService {
public APPintentservice(String name) { super(name); } public APPintentservice() { super("APPintentservice"); } @Override protected void onHandleIntent(Intent data) { SystemClock.sleep(100); Intent intent = new Intent(); intent.setAction("×××"); sendBroadcast(intent); }
}
自分のコードから抜粋してきたので抜けがあるかもしてません
あなたの回答
tips
プレビュー