繁体   English   中英

Android通知会在事件中重复几次,而不是一次

[英]Android notification repeats several times at event instead of one time

我希望代码每天早上7 执行一次通知。 我创建了一个调试apk并安装了它,以查看其性能并注意到它实际上在上午 07 发送通知,但是如果您单击该通知并进入应用并在战后关闭它,它将再次发送通知。 有人在代码中看到错误吗?

这是MainActivity.java (通知部分)中的代码:

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    intent_anmeldeActivity = new Intent(this, anmeldeActivity.class);
    intent_WebViewActivity = new Intent(this, WebViewActivity.class);

    prefs = getSharedPreferences("prefs", Context.MODE_PRIVATE);
    prefsEditor = prefs.edit();


    Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.HOUR_OF_DAY, 07);
        calendar.set(Calendar.MINUTE, 00);
        calendar.set(Calendar.SECOND, 00);

    intent_notification = new Intent(this, NotificationClass.class);

    Intent intent1 = new Intent(MainActivity.this, NotificationClass.class);
    PendingIntent pendingIntent = PendingIntent.getBroadcast(MainActivity.this, 0, intent1, PendingIntent.FLAG_UPDATE_CURRENT);
    AlarmManager am = (AlarmManager) MainActivity.this.getSystemService(MainActivity.this.ALARM_SERVICE);
    am.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pendingIntent);

它还随机发送通知。

提前致谢!

编辑:

public class NotificationClass extends BroadcastReceiver{

@Override
public void onReceive(Context context, Intent intent) {
    // TODO Auto-generated method stub

    loadText loadText = new loadText();
    loadText.startAsyncTask(context);
} }

在AsyncTask类loadText中,类NotificationBuilding在onPostExecute执行:

public class NotificationBuilding {

Context mContext = null;

int ID = 1;
Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
long when = System.currentTimeMillis();

public void startNotificationBuilding(Context con, String title, String text) {

    this.mContext = con;

    NotificationManager notificationManager = (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE);

    Intent notificationIntent = new Intent(mContext, MainActivity.class);
    notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

    PendingIntent pendingIntent = PendingIntent.getActivity(mContext, 0,
            notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);

    NotificationCompat.Builder mNotifyBuilder = (NotificationCompat.Builder) new NotificationCompat.Builder(
            mContext)
            .setSmallIcon(R.drawable.ic_stat_name)
            .setColor(Color.argb(255, 234, 146, 21))
            .setContentTitle(title)
            .setContentText(text)
            .setSound(alarmSound)
            .setAutoCancel(true)
            .setWhen(when)
            .setContentIntent(pendingIntent)
            .setVibrate(new long[]{1000, 1000, 1000, 1000, 1000})
            .setLights(Color.argb(255, 234, 146, 21), 1000, 10000)
            .setStyle(new NotificationCompat.BigTextStyle().bigText(text));

    notificationManager.notify(ID, mNotifyBuilder.build());
    ID++;
} }
// Set the alarm to start at approximately 7:00 a.m.
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.set(Calendar.HOUR_OF_DAY, 07);

       // With setInexactRepeating(), you have to use one of the AlarmManager interval
        // constants--in this case, AlarmManager.INTERVAL_DAY.
        alarmMgr.**setInexactRepeating**(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
                AlarmManager.INTERVAL_DAY, alarmIntent);

确定警报的精确度
如上所述,选择警报类型通常是创建警报的第一步。 进一步的区别是您需要警报的精确度。 对于大多数应用程序,setInexactRepeating()是正确的选择。 使用此方法时,Android会同步多个不精确的重复警报并同时触发它们。 这样可以减少电池的电量消耗。

对于具有严格时间要求的稀有应用程序(例如,警报需要精确地在上午8:30触发,此后每小时每小时触发一次),请使用setRepeating()。 但是,如果可能,应避免使用确切的警报。

使用setInexactRepeating(),不能像使用setRepeating()那样指定自定义间隔。 您必须使用时间间隔常量之一,例如INTERVAL_FIFTEEN_MINUTES,INTERVAL_DAY等。 有关完整列表,请参见AlarmManager。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM