当用户设置Timepicker.Alarm服务类从带有警报管理器的广播接收器开始时,我创建了一个带有警报服务的应用程序并播放警报mp3文件...我希望当停止警报服务返回主活动并启动时用蓝牙发送数据的功能。我怎么做?我用第二个接收器尝试这个但没有工作......
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);
}
并在主要活动....
@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);
}
答案 0 :(得分:0)
使用处理程序和消息传递来实现您想要的简单示例。
首先发送您的工作已完成的消息
Message message = new Message();
message.getData().putString("text",
"exitedtask");
Messenger messenger = new Messenger(
completionHander);
try {
messenger.send(message);
} catch (RemoteException e) {
e.printStackTrace();
}
然后使用Handler来处理应该发生的事情
protected Handler completionHander = new Handler() {
@Override
public void handleMessage(Message msg) {
if (msg.getData().getString("text")
.equalsIgnoreCase("exitedtask")) {
//do you work here
}
}
};