我正在尝试在学院的android中创建一个小项目,用户可以选择从时间选择器对话框中选择时间,并根据所选时间向用户发出饮水通知并每15次重复一次但是,警报管理员模糊地使用了问题通知。有时候通知会按时发出,并且每隔15分钟就会重复一次,但是在午夜之后通知会停止。有时会有延迟发出通知。但是要弄清楚原因!帮助赞赏..这是必要的xml和java文件 activity_water.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/relative"
tools:context="com.example.priyam.databaselogin.Water">
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="@android:color/darker_gray"
android:layout_marginTop="70dp"
/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginTop="15dp">
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/reminderWaterCheckbox"
android:buttonTint="@color/red"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:text="Remind me once at"
android:textStyle="bold"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="170dp"
android:text="09:30 AM"
android:textColor="@color/red"
android:id="@+id/textViewTime"/>
</LinearLayout>
</RelativeLayout>
water.java
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.app.TimePickerDialog;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.CheckBox;
import android.widget.TextView;
import android.widget.TimePicker;
import android.widget.Toast;
import java.util.Calendar;
public class Water extends AppCompatActivity implements View.OnClickListener {
TextView time;
CheckBox waterReminder;
int h,m;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_water);
setTitle("Water Reminder");
getId();
Toast.makeText(getApplicationContext(),"this",Toast.LENGTH_LONG).show();
}
private void getId(){
time=(TextView)findViewById(R.id.textViewTime);
waterReminder=(CheckBox)findViewById(R.id.reminderWaterCheckbox);
time.setOnClickListener(this);
}
private void showDialog(){
TimePickerDialog timePickerDialog = new TimePickerDialog(this,
new TimePickerDialog.OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker view, int hourOfDay,
int minute) {
h=hourOfDay;
m=minute;
Calendar calendar=Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY,h);
calendar.set(Calendar.MINUTE,m);
Intent intent=new Intent(getApplicationContext(),Notification_receiver.class);
PendingIntent pendingIntent=PendingIntent.getBroadcast(getApplicationContext(),100,intent,PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager alarmManager= (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,calendar.getTimeInMillis(),AlarmManager.INTERVAL_FIFTEEN_MINUTES,pendingIntent);
time.setText(h+":"+m);
}
}, h, m, false);
timePickerDialog.show();
}
@Override
public void onClick(View v) {
showDialog();
}
}
Notification_receiver.java
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;
public class Notification_receiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
NotificationManager notificationManager= (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
Intent intent1=new Intent(context,DashBoard.class);
intent1.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent=PendingIntent.getActivity(context,100,intent1,PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder builder=new NotificationCompat.Builder(context)
.setContentIntent(pendingIntent)
.setSmallIcon(R.drawable.logo)
.setContentTitle("Hitfit")
.setContentText("Its time you gulped a glass of water!Stay hydrated,stay fit!")
.setAutoCancel(true);
builder.setVibrate(new long[] { 1000, 1000});
Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
builder.setSound(alarmSound);
notificationManager.notify(100,builder.build());
}
}
使用的最小sdkVersion是18
自从几天就坚持这一点。谢谢你!
答案 0 :(得分:0)
在AlarmManager
的文档中,您可以看到以下声明:
注意:从API 19开始,所有重复警报都不准确。如果您的应用程序需要精确的交付时间,那么它必须使用一次性精确警报,每次重新安排如上所述。 targetSdkVersion早于API 19的旧应用程序将继续将所有警报(包括重复警报)视为完全警报。
我认为你实际上就是这种情况,并且你的警报并不准确,因为这是系统保持电池API 19的方式。