2017-02-27 154 views
1

我是Android新手。我正在尝试创建一个自定义声音持续时间的通知。我已经看到很多应用程序具有此功能,但我无法理解如何以最有用的方式制作它。如何以正确的方式设置通知声音持续时间android

对于我通知我用BroadcastReceiver

public class NotificationAlarmReceiver extends BroadcastReceiver{ 
@Override 
public void onReceive(Context context, Intent intent) { 

setNotification(context); 

} 

private void setNotification(Context context) { 
    Intent intent = new Intent(context, MainActivity.class); 
    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP | 
      Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK); 
    PendingIntent pendingIntent = PendingIntent.getActivity(context, 
      (int) System.currentTimeMillis(), intent, 0); 
    Notification notification = new Notification.Builder(context). 
      setTicker("Hi!"). 
      setAutoCancel(true). 
      setContentTitle("Bla bla lba!"). 
      setContentText("Hi hi!"). 
      setSmallIcon(R.mipmap.ic_launcher). 
      setContentIntent(pendingIntent).build(); 

    NotificationManager notificationManager = (NotificationManager) 
      context.getSystemService(Context.NOTIFICATION_SERVICE); 

    notification.defaults |= Notification.DEFAULT_SOUND; 
    notification.defaults |= Notification.DEFAULT_VIBRATE; 
    notification.defaults |= Notification.DEFAULT_LIGHTS; 

    notification.flags |= Notification.FLAG_INSISTENT; 

    notificationManager.notify((int) System.currentTimeMillis(), notification); 
} 
} 

谢谢!

回答

0

我发现这对我来说

的伟大工程,所以,你应该开始,因为你需要,睡多少时间一个线程,然后用相同的一个,但没有任何声音/振动ECT取代您当前的通知解决方案。

new Thread() { 
      public void run() { 
       try { 
        sleep(duration * DateUtils.SECOND_IN_MILLIS); 
       } catch (InterruptedException e) { 
        e.printStackTrace(); 
       } 
       notificationManager.cancel(id); 

       //start the same notification without sound 
       soundlessNotification(context); 
      } 
     }.start(); 
1

我没有找到任何办法来控制声音的持续时间,但你可以使用下面的代码静音AudioManager.STREAM_NOTIFICATION: -

final AudioManager am = (AudioManager) getSystemService(Context.AUDIO_SERVICE); 
am.setStreamMute(AudioManager.STREAM_NOTIFICATION, true or false); 

不要忘了通知声音总持续时间后取消静音它是过去。

相关问题