如何将闹钟时间发送到其他活动

时间:2014-04-07 07:33:00

标签: android facebook

嗨,我是Android的新手,我想创建一个警报,当闹钟时间被发现时响了但不幸的是我无法弥补它请帮我解决这个问题这里是我的MainActivity.class和SoundActivity.class文件任何人都可以告诉我如何设置这个吗?谢谢你包com.toprecur.myalarm;

    package com.toprecur.myalarm;

    import java.util.Calendar;
    import java.util.GregorianCalendar;
    import android.app.Activity;
    import android.app.AlarmManager;
    import android.app.PendingIntent;`enter code here`
    import android.content.Intent;
    import android.os.Bundle;
    import android.view.Menu;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.view.ViewDebug.FlagToString;
    import android.widget.Button;
    import android.widget.TextView;
    import android.widget.TimePicker;
    import android.widget.Toast;

    public class MainActivity extends Activity {

        protected static final TimePicker timePicker1 = null;
        protected static final Calendar calendar = null;
        protected String FLAG_ACTIVITY_NEW_TASK;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            OnClickListener setClickListener = new OnClickListener() {

                private TextView tvDisplayTime;

                @Override
                public void onClick(View v) {

                    AlarmManager alarmManager = (AlarmManager) getBaseContext()
                            .getSystemService(ALARM_SERVICE);

                    TimePicker tpTime = (TimePicker) findViewById(R.id.tp_time);

                    int hour = tpTime.getCurrentHour();
                    int minute = tpTime.getCurrentMinute();

                    GregorianCalendar calendar = new GregorianCalendar(hour,
                            minute, 0);

                    long alarm_time = calendar.getTimeInMillis();

                    Intent intent = new Intent(MainActivity.this,
                            SoundActivity.class);

                    PendingIntent operation = PendingIntent.getActivity(
                            getBaseContext(), 0, intent,
                            Intent.FLAG_ACTIVITY_NEW_TASK);

                alarmManager.set(AlarmManager.RTC_WAKEUP, alarm_time,operation);

         Toast.makeText(getBaseContext(), "Alarm is setsuccessfully",Toast.LENGTH_SHORT).show();
                }
            };
            OnClickListener quitClickListener = new OnClickListener() {
                @Override
                public void onClick(View v) {
                    finish();
                }
            };

            Button btnSetAlarm = (Button) findViewById(R.id.btn_set_alarm);
            btnSetAlarm.setOnClickListener(setClickListener);

            Button btnQuitAlarm = (Button) findViewById(R.id.btn_quit_alarm);
            btnQuitAlarm.setOnClickListener(quitClickListener);
        }

        protected Object pad(int minute) {
            // TODO Auto-generated method stub
            return null;
        }

        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            // Inflate the menu; this adds items to the action bar if it is present.
            getMenuInflater().inflate(R.menu.main, menu);
            return true;
        }

    }


and my SoundActivity is as follows below here i want to play music when the alarm sound would rang i have tried lot of methods to get the alarm setted time but i didn't got from my MainActivity.class file to SoundActivity.class file any body can help me out please. thank you.

package com.toprecur.myalarm;

import android.app.Activity;
import android.app.Notification;
import android.content.Intent;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.view.Menu;

public class SoundActivity extends Activity {

    private MediaPlayer mPlay;
    public Intent intent;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sound);


        MediaPlayer mPlay = MediaPlayer.create(this,R.raw.alarm_rooster);
        mPlay.start();
        Uri soundURI = Uri
                .parse("android.resource://course.examples.Notification.StatusBarWithCustomView/"
                        + R.raw.alarm_rooster);
        Notification.Builder notificationBuilder = new Notification.Builder(
                getApplicationContext()).setSound(soundURI);

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.sound, menu);
        return true;
    }
}

2 个答案:

答案 0 :(得分:0)

您应该使用putExtra方法发送数据,并在其他活动用户getExtra方法中接收数据。 http://developer.android.com/reference/android/content/Intent.html#putExtra(java.lang.String,float [])

答案 1 :(得分:0)

我得到了我的问题的答案使用putExtra和getIntExtra最后我得到了输出。无论如何,感谢farooq arshad。

Intent intent = new Intent(AlarmClock.ACTION_SET_ALARM)
                    //.putExtra(AlarmClock.EXTRA_MESSAGE, message)
                    .putExtra(AlarmClock.EXTRA_HOUR, hour)
                    .putExtra(AlarmClock.EXTRA_MINUTES, minute);

                startActivity(intent);