我为2个按钮写了一个简单的线性布局。一个用于启用通知的按钮和另一个用于取消通知的按钮:
<LinearLayout
android:layout_width="80dp"
android:layout_height="40dp"
android:gravity="center"
android:visibility="visible"
android:layout_marginTop="20dp"
android:layout_marginBottom="20dp"
android:background="@color/black_60_opacity"
android:layout_gravity="center">
<TextView
android:layout_gravity="center"
android:gravity="center"
android:id="@+id/submitDailyTime"
android:textColor="@color/white"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Submit"/>
</LinearLayout>
<LinearLayout
android:layout_width="80dp"
android:layout_height="40dp"
android:gravity="center"
android:visibility="visible"
android:layout_marginTop="20dp"
android:layout_marginBottom="20dp"
android:background="@color/black_60_opacity"
android:layout_gravity="center">
<TextView
android:layout_gravity="center"
android:gravity="center"
android:id="@+id/cancelNot"
android:textColor="@color/white"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Cancel"/>
</LinearLayout>
然后我写了Notification code
如下:
@Override
public void onClick(View view) {
//CustomAlertDialog.showProgressDialog(getActivity(),"Requesting...");
Intent intent;
switch (view.getId()){
case R.id.submitDailyTime:
/* onBackPressed = true;
userAccessTask = new KronosAsyncTask(getActivity(), utility.PunchIn(weekDay,calendar.getTime().toString(),"Out Punch"),this,AppConstants.urls.KRONOS_PUNCH,"json");
userAccessTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);*/
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY,14);
calendar.set(Calendar.MINUTE,18);
intent = new Intent(getActivity(),NotificationReciever.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(getActivity(),0,intent, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager alarmManager = (AlarmManager)getActivity().getSystemService(ALARM_SERVICE);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,calendar.getTimeInMillis(),alarmManager.INTERVAL_DAY,pendingIntent);
break;
case R.id.cancelNot:
String ns = getActivity().NOTIFICATION_SERVICE;
NotificationManager notificationManager = (NotificationManager) getActivity().getSystemService(ns);
notificationManager.cancel(100);
break;
}
}
这是我的notification receiver
代码:
package com.dell.kronos;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.media.RingtoneManager;
import android.net.Uri;
import android.support.v4.app.NotificationCompat;
import com.dell.kronos.activities.MainActivity;
public class NotificationReciever extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
Intent intent1 = new Intent(context,LaunchActivity.class);
intent1.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
//if we want ring on notifcation then uncomment below line//
Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
PendingIntent pendingIntent = PendingIntent.getActivity(context,100,intent1,PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder builder = new NotificationCompat.Builder(context).
setSmallIcon(R.drawable.kronos_logo).
setContentIntent(pendingIntent).
setContentText("Time to fill TimeSheet").
setContentTitle("Hurry!!!").
setSound(alarmSound).
setAutoCancel(true);
notificationManager.notify(100,builder.build());
}
}
当我点击Submit
按钮时,我成功收到通知。但是,当我点击Cancel
按钮时,通知不会被取消。如何解决?
答案 0 :(得分:0)
我忘了声明cancelNot
并将click listener
添加到cancelNot
。所以添加了这些行。
private TextView cancel;
cancel = ( TextView ) view.findViewById(R.id.cancelNot);
cancel.setOnClickListener(this);
它有效