2013-05-13 91 views
0
  1. 我在OnReceive()方法中實現了廣播接收器方法編寫通知管理器代碼。
  2. 通知中正在播放自定義鈴聲。在通知的點擊聲停止

問題 我做不到的事情,如果應用程序打開小於告知不會在通知欄來顯示。預計通知我會更新我的視圖的用戶界面[即。我會告訴STOP按鈕停止鈴聲。]Android:通知管理器只在應用程序關閉時通知

通知代碼

public void sendNotification(Context context, String title, String message, String fileName) 
    { 
      final String ns = Context.NOTIFICATION_SERVICE; 
      final NotificationManager notificationManager = (NotificationManager)context.getSystemService(ns); 
      final Notification notification = new Notification(R.drawable.ic_launcher, context.getString(R.string.SalahClock), System.currentTimeMillis()); 

      Intent intent = new Intent(context, Main.class); 
      intent.putExtra("isAlarmPlaying", true); 

      //Pending event to open our Application Screen when this notification is clicked 
      final PendingIntent contentIntent = PendingIntent.getActivity(context, 0, intent , 0); 

      notification.setLatestEventInfo(context, title, message, contentIntent); 
      notification.sound = Uri.parse("android.resource://path.."); 
      notification.flags |= Notification.FLAG_INSISTENT; 
      notificationManager.notify(1, notification); 
    } 

在ReCevie

public void onReceive(Context context, Intent intent) { 

serviceHandler = new Handler(context); 
     PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE); 
     PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "Salah"); 
     wl.acquire(); 

     serviceHandler.sendNotification(context, "", "", ""); 

     wl.release(); 

    } 

謝謝
Salik

+0

我不明白你的問題是:「這是怎麼回事,怎麼解決這個問題」,或者「這是我想要的,怎麼做?」 – auval 2013-05-13 07:17:24

+0

通知當應用程序處於活動狀態[或打開]時也會打開。通知應該只在應用程序關閉時顯示。 – nalaiqChughtai 2013-05-13 07:26:38

+0

如果您有更好的解決方案,請在下面張貼並接受您自己的解決方案。如果我的回答是最好的,不要忘記接受它。 – auval 2013-07-18 06:44:54

回答

3

你可以使用靜態變量:

public static boolean sIsActive = false; 

在任何地方你的項目,並將其設置爲true你活動onResume()false你的活動onPause()

比你的接收器可以檢查:

if (MyClass.sIsActive) { return; } 

,如果不積極,繼續與您的通知。

+0

謝謝,但尋求更好的方法 – nalaiqChughtai 2013-05-13 08:00:42

+0

這是一個很好,簡單的方法來做你想做的事情,它的工作原理。你能寫出這個解決方案中缺少的東西嗎? – auval 2013-05-13 09:09:53

相關問題