使用Alarm Manager在特定时间内推送Android推送通知

时间:2012-05-23 07:13:31

标签: android push-notification alarmmanager alarm

我想在特定时间使用推送通知。我正在使用这些代码;

这是我的AlarmReceiver类:

public class AlarmReceiver extends BroadcastReceiver {

private static int NOTIFICATION_ID = 1;

@Override
public void onReceive(Context context, Intent intent) {
    NotificationManager manger = (NotificationManager) context
            .getSystemService(Context.NOTIFICATION_SERVICE);

    Notification notification = new Notification(R.drawable.arrrow_icon,
            "test", System.currentTimeMillis());

    PendingIntent contentIntent = PendingIntent.getActivity(context,
            NOTIFICATION_ID, new Intent(context, AlarmReceiver.class),0);

    Bundle extras = intent.getExtras();
    String title = extras.getString("title");
    String note = extras.getString("note");
    notification.setLatestEventInfo(context, note, title, contentIntent);
    notification.flags = Notification.FLAG_INSISTENT;
    notification.defaults |= Notification.DEFAULT_SOUND;
    manger.notify(NOTIFICATION_ID++, notification);

}
};

这是我的主要部分;

  

Intent alarmintent = new Intent(getApplicationContext(),                         AlarmReceiver.class);                 alarmintent.putExtra(“标题”,“测试”);                 alarmintent.putExtra(“note”,“测试信息”);                 PendingIntent sender = PendingIntent                         .getBroadcast(getApplicationContext(),1,alarmintent,                                 PendingIntent.FLAG_UPDATE_CURRENT                                         | Intent.FILL_IN_DATA);

      AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
          am.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), sender);

如果我的应用程序正在运行,则推送通知正常工作。但是当我强制关闭通知时不起作用。谢谢你的帮助。

2 个答案:

答案 0 :(得分:0)

这可能会对你有所帮助。首先将此代码用于警报接收器类

public class AlarmReceiver extends BroadcastReceiver
{
    NotificationManager nm;
    public void onReceive(Context context, Intent intent) 
    {
        Toast.makeText(context, "Log Out !!", Toast.LENGTH_LONG).show();
//      Vibrator vibrator=(Vibrator)context.getSystemService(Context.VIBRATOR_SERVICE);
//      vibrator.vibrate(1000);
        nm=(NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
        CharSequence ch="Log Out Service";
        CharSequence message="Log Out You have to login again!! ";
        Notification notif = new Notification(R.drawable.ic_launcher,
                    "You Are Logged out...", System.currentTimeMillis());
//       PendingIntent contentIntent = PendingIntent.getActivity(context, 0,
//                  new Intent(), 0);
         Intent notifyintent=new Intent(context,AlarmManagerTestActivity.class);
         PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, 
            notifyintent, android.content.Intent.FLAG_ACTIVITY_NEW_TASK);
         notif.flags |=Notification.FLAG_AUTO_CANCEL;
                  notif.setLatestEventInfo(context, ch, message, pendingIntent);
                  nm.notify(0, notif);        
         }
    }

然后将此代码放入您的活动中

Intent intent = new Intent(your activity,AlarmReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(
AlarmManagerTestActivity.this, 0, intent, 0);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, c.getTimeInMillis(), pendingIntent);  

答案 1 :(得分:0)

然后尝试使用此代码,将此代码放在onCreate()protected void onUserLeaveHint(){         // TODO自动生成的方法存根         super.onUserLeaveHint();         exitAppAnimate();     }

private void exitAppAnimate() {
    ActivityManager activityManager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
    List<RunningTaskInfo> recentTasks = activityManager
            .getRunningTasks(Integer.MAX_VALUE);
    for (int i = 0; i < recentTasks.size(); i++) {
        if (i == 1
                && recentTasks.get(i).baseActivity.toShortString().indexOf(
                        getPackageName()) > -1) {
            // home button pressed
            IShomepressed = true;
            System.out.println("Home buttonpressed..........");

            // android.os.Process.killProcess(android.os.Process.myPid());
            break;
        }
    }
}  and put this if (IShomepressed) {
        IShomepressed = false;

你使用Pending intent.this会帮助你。