2011-07-07 51 views
10

我似乎有我的代碼有問題。 我已經創建了一個測試活動,只是爲了看看有什麼問題,我仍然不能。我的代碼有什麼問題 - 通知 - 沒有聲音沒有震動

public class test extends Activity { 
/** Called when the activity is first created. */ 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 

    String extra = "test"; 

    NotificationManager myNotificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); 

    Intent intent = new Intent(this, test.class); 

    Notification notification = new Notification(R.drawable.icon, 
      extra, 
      System.currentTimeMillis()); 
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 
      0, 
      intent, 
      PendingIntent.FLAG_UPDATE_CURRENT); 

    notification.setLatestEventInfo(getApplicationContext(), "title", "text", pendingIntent); 
    notification.flags |= Notification.DEFAULT_SOUND; 
    notification.flags |= Notification.DEFAULT_LIGHTS; 
    notification.flags |= Notification.DEFAULT_VIBRATE; 
    notification.flags |= Notification.FLAG_INSISTENT; 
    notification.flags |= Notification.FLAG_AUTO_CANCEL; 

    myNotificationManager.notify(33, notification); 

} 
} 

當通知彈出時,我聽不到聲音和/或震動。

我看了一下手機的設置,他們沒問題,沒有默認的默認聲音。

回答

17

這...

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

應該是...

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

對於所有默認值(聲音,振動&光)1行的代碼,你可以使用:

notification.defaults = Notification.DEFAULT_ALL; 

這相當於

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

確保你在你的清單

<uses-permission android:name="android.permission.VIBRATE" /> 
設置震動權限