将来在android中安排通知警报

时间:2016-11-19 10:41:22

标签: android android-notifications android-alarms

我是Android开发的新手,并开发了我的第一个应用程序。

我想向从数据库中的日期在后台安排的用户显示通知。我在Google上关注了许多教程,这就是我所做的。

我在主要活动和boradcast接收器类中创建了Calendar alarm = Calendar.getInstance(); alarm.set(Calendar.YEAR, year1); alarm.set(Calendar.MONTH, month1); alarm.set(Calendar.DAY_OF_MONTH, day1); alarm.set(Calendar.HOUR_OF_DAY, hour1); alarm.set(Calendar.MINUTE, min1); alarm.set(Calendar.SECOND, 0); if(alarm.before(Calendar.getInstance())) { alarm.add(Calendar.DATE, 1); Toast.makeText(getActivity(),"You selected the past time",Toast.LENGTH_SHORT).show(); } else { Intent myIntent = new Intent(getActivity(), MyReceiver.class); pendingIntent = PendingIntent.getBroadcast(getActivity(), 0, myIntent, 0); AlarmManager alarmManager = (AlarmManager) getActivity().getSystemService(getActivity().ALARM_SERVICE); alarmManager.set(AlarmManager.RTC, alarm.getTimeInMillis(), pendingIntent); } 功能。

MainActivity.java

setAarm()

ActivityAlarmReceiver.java

package com.example.myapp;

import ...


public class MainActivity extends AppCompatActivity
        implements NavigationView.OnNavigationItemSelectedListener {

    TextView navigationHeaderUserName;
    DatabaseHelper myDB;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        myDB = new DatabaseHelper(this);

        View header = navigationView.getHeaderView(0);
        navigationHeaderUserName = (TextView) header.findViewById(R.id.drawer_layout_user_name);

        // display dashboard when the activity is loaded
        FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
        ft.replace(R.id.content_frame, new ActivityDashboard());
        ft.commit();

        // set alarm
        setAlarm();
    }

    public void setAlarm() {

        // notification service
        boolean alarm = (PendingIntent.getBroadcast(this, 0, new Intent("ALARM"), PendingIntent.FLAG_NO_CREATE) == null);
        if (alarm) {
            Intent intentAlarm = new Intent("ALARM");
            PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, intentAlarm, 0);
            Calendar calendarAlarm = Calendar.getInstance();
            calendarAlarm.setTimeInMillis(System.currentTimeMillis());
            calendarAlarm.add(Calendar.SECOND, 3);
            AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
            alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendarAlarm.getTimeInMillis(), 6000, pendingIntent);
        }
    }

    @SuppressWarnings("StatementWithEmptyBody")
    @Override
    public boolean onNavigationItemSelected(@NonNull MenuItem item) {

        // calling the method displaySelectedScreen()
        displaySelectedScreen(item.getItemId());

        return true;
    }

    private void displaySelectedScreen(int itemId) {

       ....   // some code
       ....   // some code
    }
}

但这不起作用......我敢肯定,我错过了一些但却找不到的东西?

1 个答案:

答案 0 :(得分:1)

link(调度重复警报)可以帮助您。您必须在您的清单中添加以下代码:

<uses-permission android:name="android.permission.WAKE_LOCK"></uses-permission>
<receiver android:process=":remote" android:name=".ActivityAlarmReceiver"></receiver>