Ok 开发者_运维百科so I have a simple Android app that I want it to go through all of my pictures and set a random one to the background of an activity (linear layout). I have a service that give a url when a certain function (getUrl()) is called from the bound service class.
I would now like to create a loop within the service (my idea open to better ones) and every n seconds broadcast a message with the new url for the photo. Then I want to use a listener inside the activity to receive the message from the broadcast receiver and change the background image.
I am having some real trouble trying to figure out how to broadcast from within the impl innerclass of the service. Do I just have to use a synchronous service or something?
use the way
Intent mintent = new Intent();
mintent.setAction("com.action");
mintent.putExtra("url", "yourURl);
sendBroadcast(mintent);
register the same action in your main activity and in the receiver use handler to update the UI or what ever
if you get any sort of run time error in context then make a global context by extending the Application class and define the getMyContext(); which you will access in service to send broadcast like the
getMyContext().sendBroadcast(mintent)
I solved this through the use of Broadcast Receivers and broadcasts. I broadcast from the Picture service and listen for that broadcast on the other service.
精彩评论