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

标签 android android-intent android-activity alarmmanager android-pendingintent

我在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/

相关文章:

android - 如何从 Android 应用程序连接到多个 firebase 数据库

java - 如何实现 ContentProvider 以向 Gmail、Facebook、Evernote 等提供图像

android - 在 Android 应用程序中使用 Intent 来显示另一个 Activity

android - java.lang.UnsupportedOperationException : For security reasons, 特权进程中不允许使用 WebView

Android 显示 ProgressDialog 直到 Activity UI 完成加载

android - 在 Android 中,如何选择联系人并将其显示在我的应用程序中?

Android 后退导航 ParentActivityName 不起作用

android - 获取 GPS 固定卫星的数量?

Java extras 包返回空值

Android 将信息传递给 SingleInstance Activity