package com.example.clickactivity;
MianActivit:
import java.util.Calendar;
import android.app.Activity;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.app.TimePickerDialog;
import android.app.TimePickerDialog.OnTimeSetListener;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TimePicker;
import android.widget.Toast;
public class MainActivity extends Activity {
private Button btnsetClock;
private Button btncloseClock;
private AlarmManager alarmManager;
private PendingIntent pi;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnsetClock=(Button)findViewById(R.id.button1);
btncloseClock=(Button)findViewById(R.id.button2);
//获取AlarmManager对象
alarmManager=(AlarmManager)getSystemService(ALARM_SERVICE);
Intent intent=new Intent(MainActivity.this,ClockActivity.class);
Log.d("MainActivity", ">>>>>>>>>1111");
pi = PendingIntent.getActivity(MainActivity.this, 0, intent, 0);
Log.d("MainActivity", ">>>>>>>>>2");
btnsetClock.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
// 弹出一个时间设置的对话框,供用户选择时间
Calendar currentTime=Calendar.getInstance();//和下面的c不是同一对象
new TimePickerDialog(MainActivity.this, 0,
new OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
// TODO Auto-generated method stub
//设置当前时间
Calendar c=Calendar.getInstance();
c.setTimeInMillis(System.currentTimeMillis());
//根据用户选择时间来设置当前Calendar对象
c.set(Calendar.HOUR, hourOfDay);
c.set(Calendar.MINUTE, minute);
//设置AlarmManager在Calendar对应的的时间来启动Activity
alarmManager.set(AlarmManager.RTC_WAKEUP, c.getTimeInMillis(), pi);
Log.d("MainActivity", ">>>>>>>>>3");
Toast.makeText(MainActivity.this, "闹钟设置完毕", Toast.LENGTH_LONG).show();
}
},
currentTime.get(Calendar.HOUR_OF_DAY), currentTime.get(Calendar.MINUTE), false).show();
btncloseClock.setVisibility(View.VISIBLE);
Log.d("MainActivity", ">>>>>>>>>4");
}
});
btncloseClock.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
alarmManager.cancel(pi);
btncloseClock.setVisibility(View.GONE);
Toast.makeText(MainActivity.this, "闹钟已取消", Toast.LENGTH_LONG).show();
}
});
}
@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;
}
}
ClockActivity:
package com.example.clickactivity;
import java.io.IOException;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;
public class ClockActivity extends Activity{
private MediaPlayer mediaPlayer;
protected void onCreate(Bundle savedInstanceState){
Log.d("ClockActivity", ">>>>>>>>>4");
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_clock);
Log.d("ClockActivity", ">>>>>>>>>5");
Toast.makeText(ClockActivity.this, "时间到", Toast.LENGTH_LONG).show();
mediaPlayer=MediaPlayer.create(this, R.raw.heart);
//mediaPlayer.setLooping(true);
//try {
//mediaPlayer.prepare();
//} catch (IllegalStateException e) {
//// TODO Auto-generated catch block
//e.printStackTrace();
//} catch (IOException e) {
//// TODO Auto-generated catch block
//e.printStackTrace();
//}
mediaPlayer.start();
//创建一个闹钟提醒的对话框,点击确定关闭铃声与页面
new AlertDialog.Builder(ClockActivity.this).setTitle("闹钟").setMessage("起床时间到了").setPositiveButton("关闭闹铃", new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
mediaPlayer.stop();
ClockActivity.this.finish();
}
}).show();
}
}
注册很简单,只有intent要求的注册即可,就不贴了
这是一个定时程序,但到时间没反应,不能跳转到ClockActivity,把pi = PendingIntent.getActivity(MainActivity.this, 0, intent, 0);最后的参数0改为PendingIntent的设置值跳转一次
MianActivit:
import java.util.Calendar;
import android.app.Activity;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.app.TimePickerDialog;
import android.app.TimePickerDialog.OnTimeSetListener;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TimePicker;
import android.widget.Toast;
public class MainActivity extends Activity {
private Button btnsetClock;
private Button btncloseClock;
private AlarmManager alarmManager;
private PendingIntent pi;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnsetClock=(Button)findViewById(R.id.button1);
btncloseClock=(Button)findViewById(R.id.button2);
//获取AlarmManager对象
alarmManager=(AlarmManager)getSystemService(ALARM_SERVICE);
Intent intent=new Intent(MainActivity.this,ClockActivity.class);
Log.d("MainActivity", ">>>>>>>>>1111");
pi = PendingIntent.getActivity(MainActivity.this, 0, intent, 0);
Log.d("MainActivity", ">>>>>>>>>2");
btnsetClock.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
// 弹出一个时间设置的对话框,供用户选择时间
Calendar currentTime=Calendar.getInstance();//和下面的c不是同一对象
new TimePickerDialog(MainActivity.this, 0,
new OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
// TODO Auto-generated method stub
//设置当前时间
Calendar c=Calendar.getInstance();
c.setTimeInMillis(System.currentTimeMillis());
//根据用户选择时间来设置当前Calendar对象
c.set(Calendar.HOUR, hourOfDay);
c.set(Calendar.MINUTE, minute);
//设置AlarmManager在Calendar对应的的时间来启动Activity
alarmManager.set(AlarmManager.RTC_WAKEUP, c.getTimeInMillis(), pi);
Log.d("MainActivity", ">>>>>>>>>3");
Toast.makeText(MainActivity.this, "闹钟设置完毕", Toast.LENGTH_LONG).show();
}
},
currentTime.get(Calendar.HOUR_OF_DAY), currentTime.get(Calendar.MINUTE), false).show();
btncloseClock.setVisibility(View.VISIBLE);
Log.d("MainActivity", ">>>>>>>>>4");
}
});
btncloseClock.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
alarmManager.cancel(pi);
btncloseClock.setVisibility(View.GONE);
Toast.makeText(MainActivity.this, "闹钟已取消", Toast.LENGTH_LONG).show();
}
});
}
@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;
}
}
ClockActivity:
package com.example.clickactivity;
import java.io.IOException;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;
public class ClockActivity extends Activity{
private MediaPlayer mediaPlayer;
protected void onCreate(Bundle savedInstanceState){
Log.d("ClockActivity", ">>>>>>>>>4");
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_clock);
Log.d("ClockActivity", ">>>>>>>>>5");
Toast.makeText(ClockActivity.this, "时间到", Toast.LENGTH_LONG).show();
mediaPlayer=MediaPlayer.create(this, R.raw.heart);
//mediaPlayer.setLooping(true);
//try {
//mediaPlayer.prepare();
//} catch (IllegalStateException e) {
//// TODO Auto-generated catch block
//e.printStackTrace();
//} catch (IOException e) {
//// TODO Auto-generated catch block
//e.printStackTrace();
//}
mediaPlayer.start();
//创建一个闹钟提醒的对话框,点击确定关闭铃声与页面
new AlertDialog.Builder(ClockActivity.this).setTitle("闹钟").setMessage("起床时间到了").setPositiveButton("关闭闹铃", new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
mediaPlayer.stop();
ClockActivity.this.finish();
}
}).show();
}
}
注册很简单,只有intent要求的注册即可,就不贴了
这是一个定时程序,但到时间没反应,不能跳转到ClockActivity,把pi = PendingIntent.getActivity(MainActivity.this, 0, intent, 0);最后的参数0改为PendingIntent的设置值跳转一次