我制作了一个BroadcastReceiver来根据用户设置的时间向用户发出通知,而设置时间时,我会收到关于当前时间以及当前时间与选定时间之间的时差的完美正确日志,但是在运行应用程序时,它将发送关于选定时间的通知! (例如:-如果当前时间是6:00 PM,并且我需要在1分钟后设置闹钟,则我将选择6:01 PM,我的日志显示差异为60000 mSeconds,这是正确的!但是在移动设备中,闹钟会在约5秒后立即运行我将其设置为6:01 PM或12:00 PM) 我在Main或addNewMedicine.class和AlarmReceiver.class中的代码:-
package com.example.a2879.dialogs.MainActivities;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.app.TimePickerDialog;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.TimePicker;
import com.example.a2879.dialogs.BroadcastReceivers.AlarmReceiver;
import com.example.a2879.dialogs.DataBase.Model.Medicine;
import com.example.a2879.dialogs.DataBase.Model.User_Medicine_Join;
import com.example.a2879.dialogs.DataBase.MyDataBase;
import com.example.a2879.dialogs.MainActivities.BaseActivity;
import com.example.a2879.dialogs.MainActivities.HomePage;
import com.example.a2879.dialogs.R;
import java.util.Calendar;
import static com.example.a2879.dialogs.MainActivities.Login.userIdPublic;
public class addNewMedicine extends BaseActivity {
private EditText medicineTextView;
private EditText medicineNotesTextView;
private TextView timeToTake;
private Button buttonadd;
TimePickerDialog mTimePicker;
Calendar mcurrentTime;
int hour;
int minute;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add_new_medicine);
getSupportActionBar().hide();
initView();
timeToTake.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
/* mcurrentTime = Calendar.getInstance();
final int hour = mcurrentTime.get(Calendar.HOUR_OF_DAY);
final int minute = mcurrentTime.get(Calendar.MINUTE);*/
mTimePicker = new TimePickerDialog(activity, new TimePickerDialog.OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker timePicker, int selectedHour, int selectedMinute) {
hour = selectedHour;
Log.e("hour","is : "+hour);
minute = selectedMinute;
Log.e("mintune","is : "+minute);
timeToTake.setText(hour + ":" + minute);
}
}, Calendar.getInstance().get(Calendar.HOUR_OF_DAY), Calendar.getInstance().get(Calendar.MINUTE), false);
mTimePicker.setTitle("Select Time");
mTimePicker.show();
}
});
buttonadd.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String medicineName = medicineTextView.getText().toString();
String Notes = medicineNotesTextView.getText().toString();
String TimePicked = timeToTake.getText().toString();
Medicine newMedicine = new Medicine(medicineName, TimePicked, Notes);
long medicine_id = MyDataBase.getDataBaseInstance(activity).medicineDao().insertToMedicine(newMedicine);
User_Medicine_Join user_medicine_join = new User_Medicine_Join(userIdPublic, medicine_id);
MyDataBase.getDataBaseInstance(activity).user_medicine_dao().insertToUserMedicineJoin(user_medicine_join);
addAlarm(hour, minute);
showMessage("Success!", "Added to List", getResources().getColor(R.color.colorPrimary));
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
Intent intent = new Intent(activity, HomePage.class);
startActivity(intent);
finish();
}
}, 1000);
}
});
}
private void initView() {
medicineTextView = (EditText) findViewById(R.id.medicineTextView);
medicineNotesTextView = (EditText) findViewById(R.id.medicineNotesTextView);
timeToTake = (TextView) findViewById(R.id.timeToTake);
buttonadd = (Button) findViewById(R.id.buttonadd);
}
public void addAlarm(int Hours, int Mintunes) {
Calendar calender = Calendar.getInstance();
int hourofDay = calender.get(Calendar.HOUR_OF_DAY);
int mintuneofDay = calender.get(Calendar.MINUTE);
long triggerTime = ((Hours - hourofDay) * 60 * 60 * 1000) + ((Mintunes - mintuneofDay) * 60 * 1000);
Log.e("difference","is : "+triggerTime);
Intent startIntent = new Intent(activity, AlarmReceiver.class);
startIntent.putExtra("medicineName",medicineTextView.getText().toString());
PendingIntent startPIntent = PendingIntent.getBroadcast(activity, 0, startIntent, 0);
AlarmManager alarm = (AlarmManager) getSystemService(activity.ALARM_SERVICE);
alarm.set(AlarmManager.RTC_WAKEUP, triggerTime, startPIntent);
}
}
----------------------------------------------------------------------
package com.example.a2879.dialogs.BroadcastReceivers;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.media.RingtoneManager;
import android.net.Uri;
import android.support.v4.app.NotificationCompat;
import android.util.Log;
import com.example.a2879.dialogs.R;
public class AlarmReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
String medicineName = intent.getStringExtra("medicineName");
showNotification(context, medicineName);
}
public void showNotification(Context context, String title) {
Uri notification = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM);
long[] vibrate = {0, 100, 200, 300};
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context, "Medicine")
.setSmallIcon(R.drawable.notification_icon)
.setContentTitle(title)
.setSound(notification)
.setVibrate(vibrate)
.setAutoCancel(false)
.setPriority(NotificationCompat.PRIORITY_DEFAULT);
NotificationManager manager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
manager.notify(0, mBuilder.build());
Log.e("Alarm","ss");
}
}