gpt4 book ai didi

android - 为什么闹钟不播放闹钟(声音)?

转载 作者:行者123 更新时间:2023-11-29 01:34:43 28 4
gpt4 key购买 nike

我在android studio中制作了一个闹钟。我可以运行该应用程序,除了播放闹钟铃声外,其他一切正常。实际上,当闹钟时间到来时,没有声音播放。我不知道我的代码有什么问题。请帮我找出错误。

主要 Activity :

package com.mycompany.alarmclock;
//I haven't shown the imported stuff here. they are in my file.
import android.support.v7.app.ActionBarActivity;


public class MainActivity extends Activity {

AlarmManager alarmManager;
private PendingIntent pendingIntent;//an action to be performed by other/foreign application
private TimePicker alarmTimePicker;//In where the user set the time
private static MainActivity inst;
private TextView alarmTextView;//the area where alarm message/notification will be displayed


public static MainActivity instance() {
return inst;
}

public void onStart() {
super.onStart();
inst = this;
}

protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

alarmTimePicker = (TimePicker) findViewById(R.id.alarmTimePicker);
alarmTextView = (TextView) findViewById(R.id.alarmText);
ToggleButton alarmToggle = (ToggleButton) findViewById(R.id.alarmToggle);
alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
}


public void onToggleClicked(View view) {
if (((ToggleButton) view).isChecked()) {//if toggle button is "ON" do the alarming function
Log.d("MainActivity", "Alarm On");
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, alarmTimePicker.getCurrentHour());
calendar.set(Calendar.MINUTE, alarmTimePicker.getCurrentMinute());
Intent myIntent = new Intent(MainActivity.this, AlarmReceiver.class);
pendingIntent = PendingIntent.getBroadcast(MainActivity.this, 0, myIntent, 0);
alarmManager.set(AlarmManager.RTC, calendar.getTimeInMillis(), pendingIntent);

} else {
alarmManager.cancel(pendingIntent);
setAlarmText("");
Log.d("MyActivity", "Alarm Off");
}

}

public void setAlarmText(String alarmText) {
alarmTextView.setText(alarmText);

}



}

报警接收器:

    package com.mycompany.alarmclock;
//I haven't shown the imported stuff here. they are in my file.
import android.support.v4.content.WakefulBroadcastReceiver;

public class AlarmReceiver extends WakefulBroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
MainActivity inst=MainActivity.instance();
inst.setAlarmText("Get Up! Get up!");
Uri alarmUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM);

if (alarmUri == null) {
alarmUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
}
Ringtone ringtone = RingtoneManager.getRingtone(context, alarmUri);
ringtone.play();

//this will send a notification message
ComponentName comp = new ComponentName(context.getPackageName(),
AlarmService.class.getName());
startWakefulService(context, (intent.setComponent(comp)));
setResultCode(Activity.RESULT_OK);
}
}

报警服务:

  package com.mycompany.alarmclock;
//I haven't shown the imported stuff here. they are in my file.
import android.support.v4.app.NotificationCompat;


public class AlarmService extends IntentService {

private NotificationManager alarmnotificationManager;

public AlarmService(){
super("AlarmService");
}

@Override
protected void onHandleIntent(Intent intent) {
sendNotification("Get up! Get up!");

}
private void sendNotification(String msg){

Log.d("AlarmService","Sending notification...:"+msg);
alarmnotificationManager=(NotificationManager)this.getSystemService(Context.NOTIFICATION_SERVICE);

PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
new Intent(this, MainActivity.class), 0);

NotificationCompat.Builder alamNotificationBuilder = new NotificationCompat.Builder(
this).setContentTitle("Alarm").setSmallIcon(R.drawable.ic_launcher)
.setStyle(new NotificationCompat.BigTextStyle().bigText(msg))
.setContentText(msg);


alamNotificationBuilder.setContentIntent(contentIntent);
alarmnotificationManager.notify(1, alamNotificationBuilder.build());
Log.d("AlarmService", "Notification sent.");

}

}

最佳答案

也许您的媒体音量太低或静音

试试 MediaPlayer,它有很多选项

MediaPlayer mediaPlayer = new MediaPlayer();
mediaPlayer.setDataSource(context, ringtone);
mediaPlayer.setAudioStreamType(AudioManager.STREAM_ALARM);
mediaPlayer.setLooping(true);
mediaPlayer.prepare();
mediaPlayer.start();

关于android - 为什么闹钟不播放闹钟(声音)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29065050/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com