[英]Can I perform an action if a notification gets touched?
我看過一個演示手機在屏幕上方的通知被下拉時執行操作的演示。 通常,當您在Android上執行此操作時,通知菜單會打開。 它適用於所有應用程序,因此不僅適用於特定受支持的應用程序。 我曾嘗試實現此功能,但找不到任何方法來執行此操作。
.addAction
只允許我添加按鈕,但不允許這樣。 理想情況下,我認為我需要在所有通知中實現類似onTouchEvent的東西。
下面的代碼創建一個通知,如果您向它添加一個意圖(在我的情況下為空),則在單擊通知時可以執行一個操作。
public void buttonClick(View v){
final Intent emptyIntent = new Intent();
PendingIntent pendingIntent = PendingIntent.getActivity(MyActivitity.this, 0, emptyIntent, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle("My notification")
.setContentText("Hello World!")
.setAutoCancel(true) //when clicked it disappears
.setContentIntent(pendingIntent); //Required on Gingerbread and below
NotificationManager notificationManager = (NotificationManager) MyActivitity.this.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(1, mBuilder.build());
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.