我想在一段时间内使用推送通知。我正在使用这些代码;
这是我的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 =新意图(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。谢谢你帮忙。
发布于 2012-05-23 07:20:11
这可能对你有帮助。首先,将此代码用于您的警报接收类。
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-24 05:55:41
然后尝试使用此代码,将此代码从onCreate()受保护的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;在那里使用挂起的intent.this将对您有所帮助。
https://stackoverflow.com/questions/10715124
复制相似问题