2

我有一個通知,有一個動作「打開撥號」。我想要點擊「打開撥號」動作。我的通知將消失,撥號將出現。如何取消通知時點擊行動

我試過setAutocancel(true)並設置了notification.flags = Notification.FLAG_AUTO_CANCEL但這些不起作用。

我知道我無法使用SetAutocancel並在爲通知設置動作時設置爲Flag_Auto_Cancel。如果我想這樣做,我必須使用的功能cancel(id)

我的想法是發送一個IntentFilter並使用BroadcastReceiver來接收它點擊「打開撥號」。

但我不知道我該怎麼做,因爲Intent.Action_Dial是一項活動。如果我使用PendingIntent.getBroacast(),則廣播的onReceive()中的功能將起作用,但不會顯示撥號。任何人都知道我能如何解決這個問題?

這是我的商品通知:

NotificationCompat.Builder builder = new NotificationCompat.Builder(this); 
    builder.setContentTitle("Notification"); 
    builder.setContentText("This is notification"); 
    builder.setSmallIcon(R.mipmap.ic_launcher); 
    builder.setTicker("This is ticker"); 
    Intent intent = new Intent(Intent.ACTION_DIAL); 
    PendingIntent pendingIntent = PendingIntent.getActivity(this, MSG_B, intent, PendingIntent.FLAG_UPDATE_CURRENT); 
    builder.addAction(R.mipmap.ic_launcher, "Dial", pendingIntent); 
    builder.setAutoCancel(true); 
    Notification notification = builder.build(); 
    notification.flags = Notification.FLAG_AUTO_CANCEL; 
    NotificationManager manager = (NotificationManager)this.getSystemService(NOTIFICATION_SERVICE); 
    manager.notify(id, notification); 

這是我的接收機

NotificationCompat.Builder builder; 
Notification notification; 
NotificationManager manager; 
@Override 
public void onReceive(Context context, Intent intent) { 
    manager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); 
    if(intent.getAction().equals("download")){ 
     Log.d("LOG","Download"); 
     showNotify(3, context); 
     DownloadFileFromURL downloadFileFromURL = new DownloadFileFromURL(); 
     downloadFileFromURL.execute("http://freebigpictures.com/wp-content/uploads/2009/09/river-edge.jpg"); 
    } 

    else if(intent.getAction().equals("android.intent.action.DIAL")){ 
     Log.d("LOG", "cacel Dial"); 
     manager.cancel(NotificationServices.MSG_B); 
    } 

    else if(intent.getAction().equals("cancelActivity")){ 
     Log.d("LOG","cacel Activity"); 
     manager.cancel(NotificationServices.MSG_B); 
    } 
    else { 
     Log.d("LOG","Fail ABC"); 
    } 
} 

這是mainifest

<application 
    android:allowBackup="true" 
    android:icon="@mipmap/ic_launcher" 
    android:label="@string/app_name" 
    android:supportsRtl="true" 
    android:theme="@style/AppTheme"> 
    <activity 
     android:name=".activity.MainActivity" 
     android:label="@string/app_name" 
     android:theme="@style/AppTheme.NoActionBar"> 
     <intent-filter> 
      <action android:name="android.intent.action.MAIN" /> 
      <category android:name="android.intent.category.LAUNCHER" /> 
     </intent-filter> 
    </activity> 

    <service 
     android:name=".services.NotificationServices" 
     android:enabled="true" 
     android:exported="true"> 
    </service> 
    <receiver android:name=".broadcast.BroadcastReceiverImage"> 

    <intent-filter> 
     <action android:name="download"/> 
     <action android:name="cancelDial"/> 
     <action android:name="cancelActivity"/> 
     <action android:name="android.intent.action.DIAL"></action> 
    </intent-filter> 
    </receiver> 
</application> 
+0

然後接受你的回答 – Ichthyocentaurs

+0

我不能那樣做。我需要2天:) –

+0

@XiaoKing兩天過去了我想 – Denny

回答

0

的解決方法是開始在廣播接收器的活動,取消之後通知。下面是的PendingIntent代碼:

Intent intent = new Intent(); 
intent.setAction("cancelDial"); 
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, MSG_B, intent, PendingIntent.FLAG_UPDATE_CURRENT); 

,併爲廣播接收器的代碼:

else if(intent.getAction().equals("cancelDial")){ 
     Log.d("LOG", "Cancel Dial"); 
     Intent intent = new Intent(Intent.ACTION_DIAL); 
     intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
     context.startActivity(intent); 
     manager.cancel(NotificationServices.MSG_B); 
    } 

希望這將幫助別人的未來。