2013-05-13 87 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

相关问题