0
  1. 我实现了一个广播接收器,在 OnReceive() 方法中编写了通知管理器代码。
  2. 自定义铃声正在通知中播放。单击通知时声音停止

我没有做的问题,如果应用程序打开,通知将不会显示在通知栏上。期待通知我将更新我的视图的 UI [即我将显示 STOP 按钮来停止铃声。]

通知代码

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);
    }

在接收

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();

    }

谢谢,
萨利克

4

1 回答 1

3

您可以使用静态变量:

public static boolean sIsActive = false;

项目中的任何位置,并将其设置为trueon you activityonResume()和 to falseon your activity onPause()

比在您的接收器中,您可以检查:

if (MyClass.sIsActive) { return; } 

如果未激活,请继续您的通知。

于 2013-05-13T07:33:57.113 回答