首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Android使用警报管理器在特定时间推送通知

Android使用警报管理器在特定时间推送通知
EN

Stack Overflow用户
提问于 2012-05-23 07:13:31
回答 2查看 3.5K关注 0票数 1

我想在一段时间内使用推送通知。我正在使用这些代码;

这是我的AlarmReceiver课程:

代码语言:javascript
复制
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);

}
};

这是我的主要班级的一部分;

意图alarmintent =新意图(getApplicationContext(),AlarmReceiver.class);alarmintent.putExtra(“标题”,“测试”);alarmintent.putExtra(“注意”,“测试消息”);PendingIntent发送方= 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(),发件人);

如果我的应用程序是向后工作的,当我强制关闭通知不起作用时,推送通知是工作的correctly.but。谢谢你帮忙。

EN

回答 2

Stack Overflow用户

发布于 2012-05-23 07:20:11

这可能对你有帮助。首先,将此代码用于您的警报接收类。

代码语言:javascript
复制
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);        
         }
    }

然后将这些代码放入您的活动中。

代码语言:javascript
复制
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);  
票数 0
EN

Stack Overflow用户

发布于 2012-05-24 05:55:41

然后尝试使用此代码,将此代码从onCreate()受保护的void onUserLeaveHint() { // TODO自动生成的方法存根super.onUserLeaveHint();exitAppAnimate();}中删除。

代码语言:javascript
复制
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;

在那里使用挂起的intent.this将对您有所帮助。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/10715124

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档