2014-03-31 18 views
1

我搜索很多次,但我沒能找到我真正需要的, 有計算器上類似thisthis ,但我需要實現我的代碼爲api level 8不。那麼如何在我的代碼中實現它?如何將刪除意圖設置爲我的代碼?

我的代碼是在這裏:

.... 

mNotification = new Notification(icon, tickerText, when); 

//create the content which is shown in the notification pulldown 
mContentTitle = mContext.getString(R.string.noti_comes_t_l); //Full title of the notification in the pull down 
CharSequence contentText = "click to see notification"; //Text of the notification in the pull down 

//you have to set a PendingIntent on a notification to tell the system what you want it to do when the notification is selected 
//I don't want to use this here so I'm just creating a blank one 
mContentIntent = PendingIntent.getActivity(mContext, 0, new Intent(mContext, notify.class), 0); 

//add the additional content and intent to the notification 
mNotification.setLatestEventInfo(mContext, mContentTitle, contentText, mContentIntent); 

//make this notification appear in the 'Ongoing events' section 
mNotification.flags = Notification.DEFAULT_LIGHTS | Notification.FLAG_AUTO_CANCEL ; 

//show the notification 
mNotificationManager.notify(NOTIFICATION_ID, mNotification); 

.... 

我如何能實現呢?

回答

0

我認爲你需要NotificationCompat從支持庫,你可以使用此代碼生成一個通知,因爲你需要

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

    NotificationCompat.Builder mBuilder = 
      new NotificationCompat.Builder(context) 
    .setSmallIcon(icon) 
    .setContentTitle(context.getString(R.string.app_name)) 
    .setContentText(message) 
      .setDeleteIntent(PendingIntent); 

    mBuilder.setWhen(when); 

    Intent notificationIntent = new Intent(context, YOURCLASS.class);  
    notificationIntent.putExtra("url", url); 
    notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | 
      Intent.FLAG_ACTIVITY_SINGLE_TOP); 

    PendingIntent intent = PendingIntent.getActivity(context, random, notificationIntent, PendingIntent.FLAG_ONE_SHOT); 
    mBuilder.setContentIntent(intent); 
    Notification notification = mBuilder.build(); 

    notification.flags |= Notification.FLAG_AUTO_CANCEL; 

    // Play default notification sound 
    notification.defaults |= Notification.DEFAULT_SOUND; 

    // Vibrate if vibrate is enabled 
    notification.defaults |= Notification.DEFAULT_VIBRATE; 
    notificationManager.notify(random, notification); 
+0

感謝的答案,但這裏是刪除意圖適應呢? –

+0

我的意思是我想檢測用戶何時清除我的通知 –

+0

我編輯了代碼以專門添加該行,只需創建另一個PendingIntent並將其放在那裏 –

相關問題