我正在创建一个电话提醒程序,用户输入他/她想要打电话的人的姓名和电话号码以及他/她想要提醒的日期和时间。当我运行应用程序时,我不要改变 DatePicker视图,事件发生在正确的时间。但是当我更改DatePicker视图时, 事件在点击提交按钮后立即发生。 我认为这里的问题是在onDateSet()和onTimeSet()方法中创建了Calendar的新对象,而变量timePicked并没有获得onDateSet()和onTimeSet()方法的值!你能帮助我吗?在onDateSet()方法? 这对我来说非常重要。
MainActivity.java
public class MainActivity extends Activity {
public static EditText text;
public static EditText text2;
//public static int hour,minute,month,day,year;
public static long timePicked = -1;
static Calendar c;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
text = (EditText)findViewById(R.id.editText1);
text2 = (EditText)findViewById(R.id.editText2);
final Button btn = (Button)findViewById(R.id.button3);
c= new GregorianCalendar();
btn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
timePicked = c.getTimeInMillis();
if (timePicked != -1) {
MyBroadcastReceiver alarm = new MyBroadcastReceiver();
if(alarm != null) alarm.setOnetimeTimer(MainActivity.this,timePicked);
btn.setText("ALARM SET");
Log.d("test", "ALARM SET!!!");
} else {
//warn the user for ALARM not set?
Log.w("test", "ALARM not SET!!!");
}
}
});
}
public static class TimePickerFragment extends DialogFragment implements TimePickerDialog.OnTimeSetListener {
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
// Use the current time as the default values for the picker
//final Calendar c = Calendar.getInstance();
int hour = c.get(Calendar.HOUR_OF_DAY);
int minute = c.get(Calendar.MINUTE);
// Create a new instance of TimePickerDialog and return it
return new TimePickerDialog(getActivity(), this, hour, minute,DateFormat.is24HourFormat(getActivity()));
}
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
// Do something with the time chosen by the user
//Calendar c = new GregorianCalendar();
c.set(Calendar.HOUR_OF_DAY, hourOfDay);
c.set(Calendar.MINUTE, minute);
// timePicked = c.getTimeInMillis();
Log.d("test", "timePicked: " + timePicked);
Log.d("test", "current: " + System.currentTimeMillis() );
}
}
public void showTimePickerDialog(View v) {
DialogFragment newFragment = new TimePickerFragment();
newFragment.show(getFragmentManager(),"timePicker");
}
public static class DatePickerFragment extends DialogFragment implements DatePickerDialog.OnDateSetListener {
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
// Use the current date as the default date in the picker
//final Calendar c = Calendar.getInstance();
int year = c.get(Calendar.YEAR);
int month = c.get(Calendar.MONTH);
int day = c.get(Calendar.DAY_OF_MONTH);
//long millis=c.getTimeInMillis();
// Create a new instance of DatePickerDialog and return it
return new DatePickerDialog(getActivity(), this, year, month, day);
}
public void onDateSet(DatePicker view, int year, int month, int day) {
// Do something with the date chosen by the user
//Calendar c = new GregorianCalendar();
c.set(Calendar.YEAR, year);
c.set(Calendar.MONTH, month);
c.set(Calendar.DAY_OF_MONTH, month);
//datePicked = c.getTimeInMillis();
Log.d("test", "timePicked: " + timePicked);
Log.d("test", "current: " + System.currentTimeMillis() );
}
}
public void showDatePickerDialog(View v) {
DialogFragment newFragment = new DatePickerFragment();
newFragment.show(getFragmentManager(),"datePicker");
}
}
MyBroadCastReceiver.java
public class MyBroadcastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "test");
//Acquire the lock
wl.acquire();
Toast.makeText(context, "Call"+MainActivity.text.getText()+MainActivity.text2.getText(), Toast.LENGTH_LONG).show();
Log.i("test", "ALARM!!!");
showNotification(context);
Vibrator vibrator = (Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE);
vibrator.vibrate(2000);
//Release the lock
wl.release();
}
private void showNotification(Context context) {
PendingIntent contentIntent = PendingIntent.getActivity(context, 0,
new Intent(context, MainActivity.class), 0);
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(context)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle("My notification")
.setContentText("Call"+MainActivity.text.getText()+MainActivity.text2.getText());
mBuilder.setContentIntent(contentIntent);
mBuilder.setDefaults(Notification.DEFAULT_SOUND);
mBuilder.setAutoCancel(true);
NotificationManager mNotificationManager =
(NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(1, mBuilder.build());
}
public void setOnetimeTimer(Context context, long when) {
AlarmManager am = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(context, MyBroadcastReceiver.class);
PendingIntent pi = PendingIntent.getBroadcast(context, 0, intent, 0);
am.set(AlarmManager.RTC_WAKEUP, when, pi);
}
}
XML
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginTop="28dp"
android:layout_toLeftOf="@+id/editText1"
android:text="NAME" />
<EditText
android:id="@+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="@+id/button2"
android:layout_below="@+id/textView1"
android:ems="10" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView1"
android:layout_below="@+id/editText1"
android:text="Phone" />
<EditText
android:id="@+id/editText2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/textView2"
android:layout_marginTop="14dp"
android:layout_toRightOf="@+id/textView1"
android:ems="10"
android:inputType="phone" >
<requestFocus />
</EditText>
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView2"
android:layout_alignTop="@+id/button1"
android:text="Set Reminder Time" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginRight="16dp"
android:onClick="showTimePickerDialog"
android:text="Select Time" />
<TextView
android:id="@+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView3"
android:layout_below="@+id/button1"
android:layout_marginTop="41dp"
android:text="Set Reminder Date" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/button1"
android:layout_below="@+id/textView4"
android:onClick="showDatePickerDialog"
android:text="Select Date" />
<Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/button2"
android:layout_centerHorizontal="true"
android:layout_marginTop="21dp"
android:text="Submit" />
</RelativeLayout>
答案 0 :(得分:0)
尝试添加到您的清单
<uses-permission android:name="android.permission.WAKE_LOCK"/>
<uses-permission android:name="android.permission.VIBRATE" />
和
<receiver android:name="YOUR.PACKAGE.NAME.MyBroadcastReceiver" />
最后一行需要接收器的外部类文件:
所以,MyBrodcastReceiver.java
现在是:
public class MyBroadcastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "test");
//Acquire the lock
wl.acquire();
Toast.makeText(context, "Call", Toast.LENGTH_LONG).show();
Log.i("test", "ALARM!!!");
Vibrator vibrator = (Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE);
vibrator.vibrate(2000);
//Release the lock
wl.release();
}
public void setOnetimeTimer(Context context, long when) {
AlarmManager am = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(context, MyBroadcastReceiver.class);
PendingIntent pi = PendingIntent.getBroadcast(context, 0, intent, 0);
am.set(AlarmManager.RTC_WAKEUP, when, pi);
}
}
然后拨打setOnetimeTimer
在MainActivity
onCreate
{/ p>中设置闹钟:
public static long timePicked = -1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Button btn = (Button)findViewById(R.id.button3);
btn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if (timePicked != -1) {
MyBroadcastReceiver alarm = new MyBroadcastReceiver();
if(alarm != null) alarm.setOnetimeTimer(MainActivity.this, timePicked);
btn.setText("ALARM SET");
Log.d("test", "ALARM SET!!!");
} else {
//warn the user for ALARM not set?
Log.w("test", "ALARM not SET!!!");
}
}
});
}
最后和重要,从TimePickerFragment
更改onTimeSet
到此时间:
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
Calendar c = new GregorianCalendar();
c.set(Calendar.HOUR_OF_DAY, hourOfDay);
c.set(Calendar.MINUTE, minute);
timePicked = c.getTimeInMillis();
Log.d("test", "timePicked: " + timePicked);
Log.d("test", "current: " + System.currentTimeMillis() );
}
经过测试和工作。 快乐的编码。
PS:请注意,此时不考虑日期选择器。