1

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

这是我的 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);

}
};

这是我的主要部分;

意图alarmintent = new Intent(getApplicationContext(), AlarmReceiver.class); alarmintent.putExtra("title", "test"); alarmintent.putExtra("note", "test message"); 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(), sender);

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

4

2 回答 2

0

ok then try to use this code,put this code out of onCreate() protected void onUserLeaveHint() { // TODO Auto-generated method stub 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;

where you using Pending intent.this will help you.

于 2012-05-24T05:55:41.580 回答
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);  
于 2012-05-23T07:20:11.913 回答