使用AlarmManager无法正确设置警报

时间:2017-02-22 22:59:27

标签: android alarmmanager

我对Android开发相当新,我的顶点项目团队希望我们的项目由Android应用程序警报控制。 我在网上跟踪了一些入门指南,我可以打开应用程序,所有按钮都能正常工作,但闹钟本身并没有触发。 我用了

String nextAlarm = Settings.System.getString(getContentResolver(), Settings.System.NEXT_ALARM_FORMATTED);

和logcat显示闹钟,设置后,我刚设置的闹钟没有显示,而是显示明天早上的闹钟。

这是我的主要方法:

public class pancake_alarm extends Activity {

AlarmManager alarmManager;
private PendingIntent pendingIntent;
private TimePicker alarmTimePicker;
private static pancake_alarm inst;
private TextView alarmTextView;

int pancake_quant = 0;

public static pancake_alarm instance() {
    return inst;
}

@Override
public void onStart() {
    super.onStart();
    inst = this;
}

@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_pancake_alarm);
    alarmTimePicker = (TimePicker) findViewById(R.id.alarmTimePicker);
    alarmTextView = (TextView) findViewById(R.id.alarmText);
    ToggleButton alarmToggle = (ToggleButton) findViewById(R.id.alarmToggle);
    alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);

    final Button OneButton;
    final Button TwoButton;
    final Button ThreeButton;
    final Button FourButton;

    OneButton = (Button) findViewById(R.id.one_button);
    OneButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            pancake_quant = 1;
        }
    });

    TwoButton = (Button) findViewById(R.id.two_button);
    TwoButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            pancake_quant = 2;
        }
    });

    ThreeButton = (Button) findViewById(R.id.three_button);
    ThreeButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            pancake_quant =3;
        }
    });

    FourButton = (Button) findViewById(R.id.four_button);
    FourButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            pancake_quant = 4;
        }
    });
}

public void onToggleClicked(View view) {
    // for debugging
    String str = "TAG";
    Log.v(str, "Toggle Clicked");

    if (((ToggleButton) view).isChecked()) {
        Log.d("MyActivity", "Alarm On");
        // Update pancake quantity string
        setPancakeQuant(pancake_quant);
        setPancakeTime(1);

        Calendar calendar = Calendar.getInstance();
        if (Build.VERSION.SDK_INT >= 23 ) {
            calendar.set(Calendar.HOUR_OF_DAY, alarmTimePicker.getHour());
            Log.v(str, "Finding Hour, SDK 23+");
        }else {
            calendar.set(Calendar.HOUR_OF_DAY, alarmTimePicker.getCurrentHour());
            Log.v(str, "Finding Hour, SDK < 23");
        }
        if (Build.VERSION.SDK_INT >= 23 ) {
            calendar.set(Calendar.HOUR_OF_DAY, alarmTimePicker.getMinute());
            Log.v(str, "Finding Minute, SDK 23+");
        }else {
            calendar.set(Calendar.HOUR_OF_DAY, alarmTimePicker.getCurrentMinute());
            Log.v(str, "Finding Minute, SDK < 23");
        }
        Intent myIntent = new Intent(pancake_alarm.this, AlarmReciever.class);
        pendingIntent = PendingIntent.getBroadcast(pancake_alarm.this, 0, myIntent, 0);
        Log.v(str, "Well it got to this fuckin line at least");
        alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);
        String nextAlarm = Settings.System.getString(getContentResolver(), Settings.System.NEXT_ALARM_FORMATTED);
        Log.v(str, nextAlarm);
    } else {
        alarmManager.cancel(pendingIntent);
        setAlarmText("");
        Log.d("MyActivity", "Alarm Off");
        // Update pancake quantity string
        setPancakeQuant(0);
        setPancakeTime(0);
    }

}

我也可以发布我的收件人和服务,但由于它似乎甚至没有找到它们,我不知道它们是否有用。

我在这里错过了一些显而易见的东西吗?

谢谢!

编辑:这是清单

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.javapapers.androidalarmclock">
<uses-permission android:name="android.permission.WAKE_LOCK" />
<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme">
    <activity
        android:name="com.example.dustin.pancake_alarm.pancake_alarm"
        android:label="@string/app_name">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <service
        android:name="com.example.dustin.pancake_alarm.AlarmService"
        android:enabled="true" />
    <receiver android:name="com.example.dustin.pancake_alarm.AlarmReciever" />
</application>

0 个答案:

没有答案