通知每天发射不止一次

时间:2014-12-10 03:33:26

标签: android

我的alarmManager似乎每小时发出一次通知,而不是每天发一次。我希望我的应用程序每天发出一次通知,用户将点击通知并转到新活动,在那里他们将学习新的单词/定义。

我没有包含Word.class和Definition.class,我认为问题可能要做wuth定义类,没有"退出"唯一的选择是点击主页按钮或返回

package com.example.awordaday;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.support.v4.app.NotificationCompat;

public class MyAlarmService extends Service {

// private NotificationManager mManager;

@Override
public IBinder onBind(Intent arg0) {
    // TODO Auto-generated method stub
    return null;
}

@Override
public void onCreate() {
    // TODO Auto-generated method stub
    super.onCreate();
}

@SuppressWarnings({ "static-access", "deprecation" })
@Override
public void onStart(Intent intent, int startId) {
    super.onStart(intent, startId);

    // mManager = (NotificationManager)
    //   this.getApplicationContext().getSystemService(this.getApplicationContext().NOTIFICATION_SERVICE);
    Intent intent1 = new Intent(this.getApplicationContext(), Word.class);// this
                                                                            // is
                                                                            // what
                                                                            // starts

    PendingIntent showWord = PendingIntent.getActivity(this, 0, intent1, 0);

    /*
     * Notification notification = new
     * Notification(R.drawable.ic_launcher,"Daily Word",
     * System.currentTimeMillis());
     * intent1.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP|
     * Intent.FLAG_ACTIVITY_CLEAR_TOP);
     */

    Notification notification = new NotificationCompat.Builder(this)
            .setTicker("Daily Word").setSmallIcon(R.drawable.ic_launcher)
            .setContentTitle("Daily Word")
            .setContentText("Your Word is ready")
            .setContentIntent(showWord).setAutoCancel(true).build();

    /*
     * PendingIntent pendingNotificationIntent = PendingIntent.getActivity(
     * this.getApplicationContext(),0,
     * intent1,PendingIntent.FLAG_UPDATE_CURRENT); notification.flags |=
     * Notification.FLAG_AUTO_CANCEL;
     * notification.setLatestEventInfo(this.getApplicationContext(),
     * "Daily Word" , "Your Word is ready", pendingNotificationIntent);
     */

    NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

    notificationManager.notify(0, notification);

    // mManager.notify(0, notification);

}

@Override
public void onDestroy() {
    // TODO Auto-generated method stub
    super.onDestroy();
  }

}

package com.example.awordaday;

import java.util.Calendar;

import android.app.Activity;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.os.Bundle;
import android.text.format.Time;

public class MainActivity extends Activity {

private PendingIntent pendingIntent;

@Override
public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    // setContentView(R.layout.activity_main);

    Calendar calendar = Calendar.getInstance();

    /*
     * time to set alarm calendar.set(Calendar.MONTH, 6);
     * calendar.set(Calendar.YEAR, 2013);
     * calendar.set(Calendar.DAY_OF_MONTH, 13);
     */

    calendar.set(Calendar.HOUR_OF_DAY, 9);
    calendar.set(Calendar.MINUTE, 21);
    calendar.set(Calendar.SECOND, 00);
    calendar.set(Calendar.AM_PM, Calendar.PM);

    Intent myIntent = new Intent(MainActivity.this, MyReceiver.class);
    pendingIntent = PendingIntent.getBroadcast(MainActivity.this, 0,
            myIntent, 0);
    // year day
    // added this
    AlarmManager alarmManager = (AlarmManager) this
            .getSystemService(ALARM_SERVICE);
    // alarmManager.set(AlarmManager.RTC, calendar.getTimeInMillis(),
    // pendingIntent);
    alarmManager.setRepeating(1, calendar.getTimeInMillis(),
            AlarmManager.INTERVAL_DAY, pendingIntent);

  } // end onCreate
}

1 个答案:

答案 0 :(得分:1)

以下是使用RTC的一个示例。

大约21:21发出警报,同时每天重复一次:

// Set the alarm to start at approximately 09:21 p.m.
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.set(Calendar.HOUR_OF_DAY, 21);
calendar.set(Calendar.MINUTE, 21);

// With setInexactRepeating(), you have to use one of the AlarmManager interval
// constants--in this case, AlarmManager.INTERVAL_DAY.

alarmMgr.setInexactRepeating(AlarmManager.RTC, calendar.getTimeInMillis(),
        AlarmManager.INTERVAL_DAY, alarmIntent);

documentation