You can do this with Tasker, which I thoroughly recommend. It is very powerful although it isn't the easiest program to use.
It's a paid app but it's well worth the money. It's one of the best Android apps I've ever bought. I have no connection with the developer BTW, I'm just a satisfied customer.