仅当应用程序未处于活动状态时才显示通知给用户是否可行。 (打开但不活动)。目前,我有以下代码,显示通知。但是,当用户在应用程序中时,它也会显示出来,这是毫无意义的。Android显示应用程序未打开时的通知?
public void notifyNewOrders() {
sp.play(soundId2, 1, 1, 0, 0, 1);
int notificationId = 001;
Intent viewIntent = new Intent(this, ListActivity.class);
viewIntent.putExtra("01", "12");
PendingIntent viewPendingIntent = PendingIntent.getActivity(this, 0,
viewIntent, 0);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(
this)
.setSmallIcon(R.drawable.coffee_tea_icon)
.setContentTitle("New Orders available!")
.setContentText(
"New orders are available. Want to make some easy money?")
.setContentIntent(viewPendingIntent);
NotificationManagerCompat notificationManager = NotificationManagerCompat
.from(this);
notificationManager.notify(notificationId, notificationBuilder.build());
}
我可以检查活动是否正在查看。如果没有,显示通知?如何才能做到这一点。谢谢。
建议。这是否正确:
@Override
public void onResume() {
super.onResume();
user_viewing = true;
}
@Override
public void onPause() {
super.onPause();
user_viewing = false;
}
@Override
public void onStop() {
super.onStop();
user_viewing = false;
}
@Override
public void onWindowFocusChanged(boolean hasFocus) {
super.onWindowFocusChanged(hasFocus);
if (hasFocus) {
user_viewing = true;
} else {
user_viewing = false;
}
}
使用BroadcastReceiver为您的要求...谷歌它.. – mithileshssingh 2014-10-07 13:00:29