java - android设置alarm和ringtone没有任何效果。
迷茫
迷茫 2017-04-17 17:45:59
0
1
288

在mainactivity里设置了pendingintent,设置好闹钟的启动时间和铃声,由此打开一个新的activity,在这个新的activity中执行铃声的播放和闹钟提醒,但是没有任何反应

public class AlarmSetActivity extends Activity {

    private Context context;
    private AlarmManager manager;
    @ViewInject(R.id.activity_content_tv)
    private TextView title;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_alarm_set);
        ViewUtils.inject(this);
        context = this;
        title.setText("闹钟设置 ");
        manager = (AlarmManager) getSystemService(ALARM_SERVICE);
    }

    @OnClick({ R.id.time_set_btn, R.id.voice_set_btn, R.id.back_btn })
    public void onClick(View view) {
        switch (view.getId()) {
        case R.id.time_set_btn:
            setTime();
            break;
        case R.id.back_btn:
            onBackPressed();
            break;
        case R.id.voice_set_btn:
            setAlarmRingtone();
            break;
        default:
            break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        switch (requestCode) {
        case 1:
            if (resultCode == RESULT_OK) {
                Uri uri = data
                        .getParcelableExtra(RingtoneManager.EXTRA_RINGTONE_PICKED_URI);
                RingtoneManager.setActualDefaultRingtoneUri(context,
                        RingtoneManager.TYPE_RINGTONE, uri);
            }
            break;

        default:
            break;
        }
    }

    /**
     * 闹钟的时间选择
     */
    private void setTime() {
        // 当前设备上的系统时间
        Calendar cal = Calendar.getInstance();
        // 弹出设置时间的窗口
        new TimePickerDialog(this, new OnTimeSetListener() {

            @Override
            public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
                Intent intent = new Intent(context, AlarmNotifyActivity.class);
                PendingIntent pi = PendingIntent.getActivity(context, 0,
                        intent, 0);
                Calendar setCal = Calendar.getInstance();
                setCal.set(Calendar.HOUR_OF_DAY, hourOfDay);
                setCal.set(Calendar.MINUTE, minute);
                // 设置AlarmManager将在Calendar对应的时间启动指定组件
                // 将在指定的时间(24h)之后再次启动
                manager.setRepeating(AlarmManager.RTC_WAKEUP,
                        setCal.getTimeInMillis(), 1000 * 60 * 60 * 24, pi);
                Toast.makeText(context, "闹铃设置成功", Toast.LENGTH_SHORT).show();
            }
        }, cal.get(Calendar.HOUR_OF_DAY), cal.get(Calendar.MINUTE), true)
                .show();
    }

    /**
     * 选择闹钟铃声
     */
    private void setAlarmRingtone() {
        Intent intent = new Intent(RingtoneManager.ACTION_RINGTONE_PICKER);
        intent.putExtra(RingtoneManager.EXTRA_RINGTONE_TYPE,
                RingtoneManager.TYPE_ALARM);
        intent.putExtra(RingtoneManager.EXTRA_RINGTONE_SHOW_SILENT, false);
        intent.putExtra(RingtoneManager.EXTRA_RINGTONE_TITLE, "请选择闹钟铃声");
        startActivityForResult(intent, 1);
    }
}

第二个activity

public class AlarmNotifyActivity extends Activity {

    private MediaPlayer player;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        player = MediaPlayer.create(this, RingtoneManager
                .getActualDefaultRingtoneUri(this, RingtoneManager.TYPE_ALARM));
        player.setLooping(true);
        player.start();
        new AlertDialog.Builder(AlarmNotifyActivity.this).setTitle("测量提醒")  
        .setMessage("请起床进行体温测量!")  
        .setPositiveButton("好哒~~", new OnClickListener() {  

            @Override  
            public void onClick(DialogInterface dialog, int which) {  
                // 停止音乐播放  
                player.stop();  
                // 结束该Activity  
                AlarmNotifyActivity.this.finish();  
            }  
        }).show();  
    }
}
迷茫
迷茫

业精于勤,荒于嬉;行成于思,毁于随。

reply all(1)
大家讲道理

Twenty-four hours have passed. Maybe your program process has been killed by the system, and naturally there will be no reaction. You can set the time shorter first (such as one or two minutes), because you only have the activity, so it is best to keep your activity running in the foreground and not destroyed within one or two minutes, so that you can verify your program Correct or not. If the alarm sounds after a minute or two, you will have to consider how to prevent the program process from being killed by the system due to being immobile for a long time. It is unreliable to maintain the activity for such a long time just by relying on two activities.

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template