2017-02-27 3 views
1

Ich bin neu in android. Und ich versuche, eine Benachrichtigung mit einer benutzerdefinierten Sounddauer zu erstellen. Ich habe eine Menge Apps mit dieser Funktion gesehen, aber ich kann nicht verstehen, wie man es am nützlichsten macht.Wie man Benachrichtigungssounddauer im richtigen Weg einstellt Android

Für meine Anmeldung verwende ich 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); 
} 
} 

Dank!

Antwort

0

Ich habe eine Lösung gefunden, die für mich

große Werke So sollten Sie einen Thread starten, die, wie Sie so viel Zeit schläft müssen und dann ersetzt Ihre aktuelle Meldung mit dem gleichen, aber ohne Ton/Vibration 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

finde ich keinen Weg, um die Tondauer zu kontrollieren, aber Sie die AudioManager.STREAM_NOTIFICATION unter Code stumm schalten kann: -

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

vergessen Sie nicht, es nach der Notifizierungston Gesamtdauer auf Aufhebung der Stummschaltung ist abgelaufen.

Verwandte Themen