我创建了一个带有警报服务的应用程序并在用户设置 Timepicker 时播放警报 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);
}