我使用警报管理器来停止所有在 android 中播放的音乐。但我不知道如何做到这一点。在主要活动源代码中
Intent intent = new Intent(AlarmActivity.this, OneShotAlarm.class);
PendingIntent sender = PendingIntent.getBroadcast(AlarmActivity.this,
0, intent, 0);
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.add(Calendar.SECOND, 5);
// Schedule the alarm!
AlarmManager am = (AlarmManager)getSystemService(ALARM_SERVICE);
am.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), sender);
和 Alarm 扩展 BroadcastReceiver
public class OneShotAlarm extends BroadcastReceiver
{
@Override
public void onReceive(Context context, Intent intent)
{
// What can I do for stop music of device is running
Toast.makeText(context, R.string.one_shot_received, Toast.LENGTH_SHORT).show();
}
}