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

时间:2015-03-15 19:17:16

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

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

MainActivity:

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);

    }



}

AlarmReceiver:

    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);
    }
}

AlarmService:

  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.");

            }

        }

1 个答案:

答案 0 :(得分:1)

也许你的媒体音量低或静音

尝试MediaPlayer,它有很多选项

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