I create an application with alarm service and play an alarm mp3 file when the user set up a Timepicker.Alarm service class start from broadcast receiver with alarm manager...I want when stop alarm service return to main activity and start a function for send data with bluetooth.How i can do this?I try this with second receiver but no work...
public class AlarmService extends IntentService {
public String response="";
MediaPlayer mPlayer;
public AlarmService() {
super("AlarmService");
}
// Will be called asynchronously be Android
@Override
protected void onHandleIntent(Intent intent) {
mPlayer = MediaPlayer.create(AlarmService.this, R.raw.alarm);
mPlayer.start();
String data="start";
Intent intentsend=new Intent("update");//Send data to update UI
intentsend.putExtra( "Alarmdata",data);
sendBroadcast(intentsend);
}
and in main activity....
@Override
public void onResume() {
super.onResume();
updateReceiver=new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intentsend) {
//get extras, do some stuff
String data1 = intentsend.getStringExtra("Alarmdata");
if(data1=="start"){
String data2="1";
Toast.makeText(context, "Test updateReceiver!",Toast.LENGTH_LONG).show();
try {
sendData(data2);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
};
IntentFilter updateIntentFilter=new IntentFilter("update");
registerReceiver(updateReceiver, updateIntentFilter);
}
Use Handler and Messaging to achieve what you want a simple example would be this.
First send a message that your work has completed
Message message = new Message();
message.getData().putString("text",
"exitedtask");
Messenger messenger = new Messenger(
completionHander);
try {
messenger.send(message);
} catch (RemoteException e) {
e.printStackTrace();
}
Then use a Handler to handle what should happen
protected Handler completionHander = new Handler() {
@Override
public void handleMessage(Message msg) {
if (msg.getData().getString("text")
.equalsIgnoreCase("exitedtask")) {
//do you work here
}
}
};
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.