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>
然後接受你的回答 – Ichthyocentaurs
我不能那樣做。我需要2天:) –
@XiaoKing兩天過去了我想 – Denny