如何在Android中使用通知设置闹钟

时间:2015-10-15 17:50:42

标签: android notifications

我无法针对某个目标设置闹钟。我可以在单击SET按钮时显示通知,但我希望根据用户设置(选择的日期,时间,重复,声音)显示该通知。

SetNotifyActivity布局:

目标名称:(数据显示)

开始日期:(数据显示)

结束日期:(数据显示)

警报\通知设置:

TimePicker [SetTimebutton]

REPEAT: [ToggleButton]如果为false,则启用SetTime按钮 如果为true,则会弹出alertDialog以输入数字并选择类型:(按分钟,按小时,按天)

声音: [ToggleButton]如果错误,振动;如果为真,则在闹钟响起时声音开启。

[设置按钮] < - 提交用户选择的通知/闹钟设置

布局没有问题。

CODE:

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_set_notify);

        Bundle extras = getIntent().getExtras();

        if (extras == null) {
            return;
        }

        goalname = (TextView)findViewById(R.id.goal_name);
        startdate = (TextView)findViewById(R.id.sdate);
        enddate = (TextView)findViewById(R.id.edate);
        showrepeat = (TextView)findViewById(R.id.showRepeat);

        showTime = (TextView) findViewById(R.id.showtime);
        setTime = (Button)findViewById(R.id.settime);
        timePicker = (TimePicker)findViewById(R.id.timePicker);

        setRepeat = (Button)findViewById(R.id.setRepetition);

        goal_id = Integer.parseInt(extras.getString("goalid"));

        //Create database
        dbhandler = new MyDBAdapter(this);

        displayDetails();
        displayTime();
        changeTimeButton();
        selectRepeat();

        //for notifications

        // listener handler
        View.OnClickListener handler = new View.OnClickListener(){
            public void onClick(View v) {
                switch (v.getId()) {
                    case R.id.setnotify:
                        showNotification();
                        done();
                        break;

                    //case R.id.btnCancelNotification:
                        //cancelNotification(0);
                        //break;
                }
            }
        };

        // we will set the listeners
        findViewById(R.id.setnotify).setOnClickListener(handler);
        //findViewById(R.id.btnCancelNotification).setOnClickListener(handler);

    }

//for notifications
    public void showNotification(){
        List<Goals> oneGoal = dbhandler.getLatestGoal(goal_id);

        for (final Goals goals : oneGoal) {
            //to get the first incomplete activity in the goal
            int actid = Integer.parseInt(dbhandler.getFirstIncompleteActivity(goal_id));
            Activities oneAct = dbhandler.getActivity(actid);

            // define sound URI, the sound to be played when there's a notification
            Uri soundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);

            // intent triggered, you can add other intent for other actions
            Intent intent = new Intent(SetNotifyActivity.this, ViewTasksActivity.class);
            intent.putExtra("goalid", Integer.toString(oneAct.getGId()));
            intent.putExtra("activityId", Integer.toString(oneAct.getActId()));
            PendingIntent pIntent = PendingIntent.getActivity(SetNotifyActivity.this, 0, intent, 0);

            // intent triggered, you can add other intent for other actions
            Intent intent2 = new Intent(SetNotifyActivity.this, ViewActActivity.class);
            intent2.putExtra("goalid", Integer.toString(oneAct.getGId()));
            PendingIntent pIntent2 = PendingIntent.getActivity(SetNotifyActivity.this, 0, intent2, 0);


            //String msg = "/n"+oneAct.getActivityName()+" is due on "+oneAct.getEndDate()+".Check the tasks to finish the activity.";

            //assigned a unique id to notifications
            Random random = new Random();
            int m = random.nextInt(9999 - 1000) + 1000;

            String msg ="";

            if(goals.getComplete().equalsIgnoreCase("true")){
                msg = "Complete";
            }else if(goals.getComplete().equalsIgnoreCase("false")){
                msg = "Incomplete";
            }


            // this is it, we'll build the notification!
            // in the addAction method, if you don't want any icon, just set the first param to 0
            Notification mNotification = new Notification.Builder(this)
                    .setContentTitle(goals.getGoalName())
                    .setContentText(goals.getStartDate() + " - " +goals.getEndDate()+" ( "+goals.getPercent()+"%, "+msg+" )")
                    .setSmallIcon(R.drawable.gsoicon)
                    .setContentIntent(pIntent2)
                    .setSound(soundUri)
                            //.addAction(R.drawable.gsoicon, "View", pIntent)
                    //.addAction(0, "View All Activities", pIntent2)
                    //.addAction(0, "Check Tasks", pIntent)
                    .build();

            NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

            // If you want to hide the notification after it was selected, do the code below
             mNotification.flags |= Notification.FLAG_AUTO_CANCEL;

            notificationManager.notify(m, mNotification);
        }

    }

而且,如果我重启应用程序,所有这一切都会消失。我必须单击设置按钮再次显示通知。

如何解决这个问题?

- 而且,我不能使用带有此功能的示例代码或注释 - &gt; notification.setLatestEventInfo()因为它无法识别它。我不知道为什么。

1 个答案:

答案 0 :(得分:0)

收到通知后设置警告对话管理器,你可以设置警报对话管理器的属性,如声音,振动(设置振动许可)等。

 public  String generateNotification(Context context, String message) {
    Log.e("Neuron", "Received Msg:" + message);
    int icon = R.drawable.logo;
    long when = System.currentTimeMillis();
    receiveMessage(message);

    NotificationManager notificationManager = (NotificationManager)
            context.getSystemService(Context.NOTIFICATION_SERVICE);
    Notification notification = new Notification(icon, message, when);

    String title = context.getString(R.string.app_name);

    Intent notificationIntent = new Intent(context, Main.class);
    // set intent so it does not start a new activity
    notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP |
            Intent.FLAG_ACTIVITY_SINGLE_TOP);
    PendingIntent intent =
            PendingIntent.getActivity(context, 0, notificationIntent, 0);
    notification.setLatestEventInfo(context, title, message, intent);
    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(0, notification);

    return message;
}