回答
至於ICS,問題是簡單的回覆,因爲所需的行爲反映了默認的通知:您可以接近通知其滑動到右側,你可以定義活動用戶發送到時,他按下它簡單地使用PendingIntent
:從http://developer.android.com/guide/topics/ui/notifiers/notifications.html
感謝您的回答,雖然這不是我所要求的。 我想要做的是帶有2個按鈕的通知(已經有佈局),但是我在遇到onClick事件時遇到問題,我希望其中一個關閉** ACTIVITY **,另一個關閉通知的默認操作。 – doronsl
採取
// The PendingIntent to launch our activity if the user selects this
// notification. Note the use of FLAG_CANCEL_CURRENT so that, if there
// is already an active matching pending intent, cancel it and replace
// it with the new array of Intents.
PendingIntent contentIntent = PendingIntent.getActivities(this, 0,
makeMessageIntentStack(this, from, message), PendingIntent.FLAG_CANCEL_CURRENT);
代碼如果你想具體意圖分配到一個按鈕:
views.setOnClickPendingIntent(R.id.your_button_id, pendingIntent);
我想你只需要一個意向單擊該按鈕時發送,所以你必須避免設置主通知意向
notification.contentIntent = yourPendingIntent;
否則(如果設置「notification.contentIntent = pendingIntent;「像往常一樣)這兩個意圖將被調用,這可能不是你想要/用戶期望的。
如果仍想按下通知的其他部分援引一般意圖(或任何其他)可以使用意圖每次觀看分配的上面相同的方法。不要忘記設置
android:clickable="true"
您想跟蹤onClick()的任何視圖。
您可以在他們正在調用的活動中通過他們的額外追蹤來追蹤這些意圖。 如果你喊你主/發射活動比你在這裏追蹤他們(因爲它來自的javadoc此方法):
@Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
Bundle data = intent.getExtras();
if (data != null && data.containsKey(YOUR_INTENT_KEY_SOURCE_CONSTANT)) {
// process your notification intent
}
// go on with smth else
}
我很高興將它張貼!通宵工作後,我發現了一些東西。所以,我們走吧!
1.爲您的通知創建一個xml佈局文件。
2.使用Notification.Builder創建通知。加入你想要的一切後(圖標,聲音等),這樣做:
//R.layout.notification_layout is from step 1
RemoteViews contentView=new RemoteViews(ctx.getPackageName(), R.layout.notification_layout);
setListeners(contentView);//look at step 3
notification.contentView = contentView;
3.創建一個方法setListeners。在此方法中,你必須這樣寫:
//HelperActivity will be shown at step 4
Intent radio=new Intent(ctx, packagename.youractivity.class);
radio.putExtra("AN_ACTION", "do");//if necessary
PendingIntent pRadio = PendingIntent.getActivity(ctx, 0, radio, 0);
//R.id.radio is a button from the layout which is created at step 2 view.setOnClickPendingIntent(R.id.radio, pRadio);
//Follows exactly my code!
Intent volume=new Intent(ctx, tsapalos11598712.bill3050.shortcuts.helper.HelperActivity.class);
volume.putExtra("DO", "volume");</p>
//HERE is the whole trick. Look at pVolume. I used 1 instead of 0.
PendingIntent pVolume = PendingIntent.getActivity(ctx, 1, volume, 0);
view.setOnClickPendingIntent(R.id.volume, pVolume);
4.要求,我想用它響應了主意,HelperActivity。但對於你我不認爲這是必要的。
如果你想要完整的源代碼,你可以瀏覽它或從我的git倉庫下載它。該代碼是供個人使用的,所以不要指望用很多評論來閱讀華麗的代碼。 https://github.com/BILLyTheLiTTle/AndroidProject_Shortcuts
以上所有內容都回答了從不同按鈕捕捉事件的問題。
關於取消通知我在這裏將您重定向
只要記得用你的通知方法解析的ID,當你要求拳頭時間
這在ICS 4.0.4上非常出色!您應該考慮正確地設置您的答案,以便其他用戶可以仔細查看您的答案。 –
你說得對。我應該從開始做起。我希望現在好一點。由於您的評論,我添加了鏈接以下載源代碼。從現在起所有的讀者都應該感謝你,而不是我。快樂的結局和幸福的新年 – LiTTle
這真是太棒了!新年快樂!! –
您可以簡單地添加行爲的通知Notification
中的按鈕通過將動作設置爲Notification.Builder
併爲每個動作定義PendingIntent
以下是示例代碼:
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.notification_icon)
.setContentTitle("My notification")
.setContentText("Hello World!")
.addAction(R.drawable.action_posetive,"posetive",PendingIntent.getActivity(0,intent,0))
.addAction(R.drawable.action_clear,"clear",PendingIntent.getActivity(0,intent,0));
NotificationManager mNotificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(0, mBuilder.build());
非常感謝! – DmitryKanunnikoff
有一個完整的例子這裏爲你
//Add this code to onCreate or some onclick Buttton
NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
NotificationCompat.Builder builder = new NotificationCompat.Builder(getApplicationContext());
long when = System.currentTimeMillis();
builder.setSmallIcon(R.drawable.ic_notification);
Intent notificationIntent = new Intent(getApplicationContext(), notificationActivity.class).putExtra("notification", "1");
PendingIntent contentIntent = PendingIntent.getActivity(getApplicationContext(), 1, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
builder.setContentIntent(contentIntent);
Notification notification = builder.getNotification();
notification.when = when;
RemoteViews remoteViews = new RemoteViews(getApplicationContext().getPackageName(), R.layout.notification_view);
remoteViews.setTextViewText(R.id.tvName, "New Name");
listener(remoteViews,getApplicationContext());
notification.contentView = remoteViews;
notification.flags |= Notification.FLAG_AUTO_CANCEL;
manager.notify(1, notification);
,然後你可以定義監聽器的方法:
public void listener(RemoteViews remoteViews, Context context) {
// you have to make intetns for each action (your Buttons)
Intent intent = new Intent("Accept");
Intent intent2 = new Intent("Reject");
PendingIntent pendingIntent = PendingIntent.getBroadcast(context,1,intent,0);
PendingIntent pendingIntent2 = PendingIntent.getBroadcast(context,1,intent2,0);
// add actions here !
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction("Accept");
intentFilter.addAction("Reject");
BroadcastReceiver receiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
if(intent.getAction().equals("Accept")){
Toast.makeText(context, "Accepted !!", Toast.LENGTH_SHORT).show();
} else if(intent.getAction().equals("Reject")) {
Toast.makeText(context, "Rejected !!", Toast.LENGTH_SHORT).show();
}
}
};
context.registerReceiver(receiver,intentFilter);
remoteViews.setOnClickPendingIntent(R.id.ivRequest,pendingIntent);
remoteViews.setOnClickPendingIntent(R.id.ivReject,pendingIntent2);
}
這裏是notification_view佈局costumize您的通知。
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="16dp">
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:text="Request from "
/>
<TextView
android:id="@+id/tvName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginStart="15dp"
android:layout_toRightOf="@id/textView"
android:text="Amin"
/>
<ImageView
android:id="@+id/ivRequest"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_alignParentEnd="true"
android:layout_centerVertical="true"
android:src="@drawable/notification"
/>
<ImageView
android:id="@+id/ivReject"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_marginEnd="10dp"
android:layout_toLeftOf="@id/ivRequest"
android:layout_centerVertical="true"
android:src="@drawable/trash"
/>
</RelativeLayout>
- 1. android推送通知中的按鈕
- 2. 在Android模擬器上按下按鈕時發出通知
- 3. 帶按鈕的Android通知單擊刪除通知
- 4. 如何直接在Android Wear上通知上添加按鈕
- 5. Android - 果凍豆通知關閉按鈕
- 6. Php按鈕推送通知android
- 7. 正常視圖Android通知按鈕
- 8. 通知取消按鈕在Android中
- 9. 禁用Android主按鈕和通知欄
- 10. NS通知按鈕按下按鈕
- 11. 如何在android中的按鈕上創建通知計數
- 12. Android通知 - 在普通視圖通知(沒有擴大到大視圖)上的動作按鈕
- 13. Android的通知大觀 - 按鈕意圖沒有被解僱
- 14. Android按鈕/帶有通知指示器(iOS風格)的控件
- 15. 帶按鈕的Applescript通知
- 16. Android - 拉伸的EditText上面的按鈕
- 17. 在我的Facebook上沒有Like按鈕的通知?
- 18. Android PopupWindow showAtLocation上面的按鈕
- 19. Android:在狀態欄通知上添加一個按鈕
- 20. android按鈕:android:text上面的android:drawableBottom的按鈕
- 21. Android通知,負荷只有當有一個以上的通知
- 22. 通知框中沒有顯示通知操作按鈕
- 23. 沒有按鈕的Android用戶界面?
- 24. Android通知按鈕(的addAction)敬酒消息按下
- 25. 通知欄關閉按鈕
- 26. 通知和主頁按鈕
- 27. Facebook喜歡按鈕通知
- 28. 通知操作按鈕appereance
- 29. 後退按鈕通知?
- 30. ImageView Android上的按鈕Android
說實話,我不喜歡背後的想法,或者我不明白。 Android中的正常通知在右側有X,如果你點擊它們,你重定向到了活動。 – user61664
我們教他們並向他們展示Android開發人員指南;) –