僅當應用程序未處於活動狀態時才顯示通知給用戶是否可行。 (打開但不活動)。目前,我有以下代碼,顯示通知。但是,當用戶在應用程序中時,它也會顯示出來,這是毫無意義的。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