无法设置警报

时间:2016-09-09 09:49:22

标签: android

我已经使用the library在径向时钟的帮助下设置时间,并希望在所选时间设置警报。我已经使用AlarmManager来做到这一点。但是在选择时间之后没有警报

AlarmReceiver.java

public class AlarmReceiver extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {

    // For our recurring task, we'll just display a message
    Toast.makeText(context, "I'm running", Toast.LENGTH_SHORT).show();
}

}

DeviceBootReceiver.java

public class DeviceBootReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
    if (intent.getAction().equals("android.intent.action.BOOT_COMPLETED")) {
        /* Setting the alarm here */

        Intent alarmIntent = new Intent(context, AlarmReceiver.class);

        PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, alarmIntent, 0);

        AlarmManager manager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);

        int interval = 1000;

        manager.setInexactRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), interval, pendingIntent);

        Toast.makeText(context, "Alarm Set", Toast.LENGTH_SHORT).show();
    }
}

}

SiteDetailsActivity.java

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

    setTitle("Site Details");

    initComponent();

    /* Retrieve a PendingIntent that will perform a broadcast */
    Intent alarmIntent = new Intent(SiteDetailsActivity.this, AlarmReceiver.class);
    pendingIntent = PendingIntent.getBroadcast(SiteDetailsActivity.this, 0, alarmIntent, 0);
}

public void clickAlert(View view)
{
    alertBtn.setVisibility(View.INVISIBLE);
    alertGrnBtn.setVisibility(View.VISIBLE);
    RadialTimePickerDialogFragment rtpd = new RadialTimePickerDialogFragment()
            .setOnTimeSetListener(SiteDetailsActivity.this)
            .setForced24hFormat();
    rtpd.show(getSupportFragmentManager(), FRAG_TAG_TIME_PICKER);
}

@Override
public void onTimeSet(RadialTimePickerDialogFragment dialog, int hourOfDay, int minute) {
    startAtSelectedTime(hourOfDay,minute);
}

public void startAtSelectedTime(int hr,int min) {
    AlarmManager manager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);

    Calendar calendar = Calendar.getInstance();
    calendar.setTimeInMillis(System.currentTimeMillis());
    calendar.set(Calendar.HOUR_OF_DAY, hr);
    calendar.set(Calendar.MINUTE, min);

    /* Repeating on every 10 minutes interval */
    manager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
            1000 * 60 * 10, pendingIntent);
}

的AndroidManifest.xml

权限

<!-- Permission to start Alarm on device reboot -->
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>

1 个答案:

答案 0 :(得分:0)

使用 // Mention size of the nvarchar column , here i give 500 , you can use its length for @Firstname as you mention in database according to your database SqlCommand command = new SqlCommand("inserting", con); command.CommandType = CommandType.StoredProcedure; command.Parameters.Add(new SqlParameter("@Firstname", SqlDbType.NVarChar,500).Value = TextBox1.Text; ,您必须使用setInexactRepeating()间隔之一  常量。在这种情况下,AlarmManager因为这是最小值,您可以从docs确认这一点,这可以在API 19或之前获得。

AlarmManager.INTERVAL_FIFTEEN_MINUTES

替代方案是

alarmMgr.setInexactRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(),
        AlarmManager.INTERVAL_FIFTEEN_MINUTES, pendingIntent);