android - 创建通知

标签 android broadcastreceiver android-service android-notifications

我是 Android 开发新手。我正在从事一个项目,该项目涉及创建一个“待办事项”列表,用户可以在其中添加即将发生的事件的详细信息。然后这些事件显示在 ListView 中。现在我希望在事件发生之日创建并显示通知。
我有以下问题。

  1. 我无法区分是应该使用 Services 还是 BroadCastReciver 来这样做。
  2. 可以有多个“待办事项”,因此必须为每个事件设置通知。那么我是否必须创建多个服务(或 BroadcastReceivers)?

最佳答案

尝试此方法一次即可通过。这适用于我在特定日期和时间发送事件通知。请调用以下方法。

Calendar calSet = Calendar.getInstance();
                        calSet.setTimeInMillis(System.currentTimeMillis());
                        calSet.clear();
                        calSet.set(Eventyear,Eventmonth,DayEvent,EventTimeHrs,EventTimeMin, 0);

setAlarm(context,calSet);




    private void setAlarm(Context context, Calendar targetCal) {

            //SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss aa");

            Intent intent = new Intent(context, com.newme.main.AlarmReceiver.class);
            intent.putExtra("title", title);
            intent.putExtra("des", des);
            intent.putExtra("startTime", StartTime);
            intent.putExtra("date", dateStr);
            intent.putExtra("venue", venue);
            intent.putExtra("time", timeStr);
            PendingIntent pendingIntent = PendingIntent.getBroadcast(context, RQS_1, intent, 0);
            AlarmManager  alarmManager   = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
            alarmManager.cancel(pendingIntent);
            alarmManager.set(AlarmManager.RTC_WAKEUP, targetCal.getTimeInMillis(), pendingIntent);
        }


public class AlarmReceiver extends BroadcastReceiver {
    int mNotificationId = 001;
    private String des,title,StartTime,date,venue,time;

    @SuppressWarnings({ "deprecation", "static-access" })
    @Override
    public void onReceive(Context context, Intent intent) {

        //Toast.makeText(context, "Show Event", Toast.LENGTH_LONG).show();

        int icon = 0;
        long notificationTime       = 0;

        icon        = R.drawable.app_icon_client;  

        des         =   intent.getStringExtra("des");
        String decStr   =Html.fromHtml(des).toString();
        title       =   intent.getStringExtra("title");
        StartTime   =   intent.getStringExtra("startTime");
        date        =   intent.getStringExtra("date");
        venue       =   intent.getStringExtra("venue");
        time        =   intent.getStringExtra("time");


        notificationTime    = System.currentTimeMillis(); 
        PendingIntent resultPendingIntent =
                PendingIntent.getActivity(
                        context,
                        0,
                        intent,
                        PendingIntent.FLAG_CANCEL_CURRENT
                        );


        NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
                context);
        Notification notification = mBuilder.setSmallIcon(icon).setTicker(title).setWhen(0)
                .setAutoCancel(true)
                .setContentTitle(AppConstants.APP_NAME)
                .setStyle(new NotificationCompat.BigTextStyle().bigText("Reminder"+" "+title+"\ntomorrow. Hope to see you there."))
                .setContentIntent(resultPendingIntent)
                .setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION))
                .build();

        NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
        notificationManager.notify(mNotificationId, notification);

        /**
         * vibration 
         * */
        notification.vibrate=new long[] {100L, 100L, 200L, 500L};

        PushNotificationUtils.notificationReceived=true;
        PowerManager pm     = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
        WakeLock wl         = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP, "TAG");
        wl.acquire();

    }
}

关于android - 创建通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31979701/

相关文章:

android - Geolocation Plugin.geoLocation 在模拟器或设备上没有给出答案

android - 广播接收器中的getIntent undefined android

android - 带有计时器的服务调用另一个服务

android - 在 Android 上保持后台服务活跃

android - 如何让我的按钮在 Fragments,ViewPager 中执行某些操作

android - 如何更改 gridview 中项目的字体颜色(android)

java - 如何从 Android 中的另一个应用程序启动 Activity

android - 如何在android中获取传入的短信?

Android:从未收到广播 ACTION_MY_PACKAGE_REPLACED

android - 更新小部件 onClick,启动服务似乎不起作用