应该报警时不会报警

时间:2016-03-17 10:33:00

标签: android alarmmanager

我正在AlarmManager工作;我已经创建了一个alarmManager(根据教程),可以设置为在特定时间关闭,但问题是我设置时间并单击TimePickerDialog中的确定按钮无论什么时候警报响起。然而,吐司是在它应该的时候出现的。

我错过了什么?

这是我的代码:

AndroidTimeActivity.java

public class AndroidTimeActivity extends Activity {

NotificationCompat.Builder mBuilder;
TimePicker myTimePicker;
Button buttonstartSetDialog;
TextView textAlarmPrompt;

TimePickerDialog timePickerDialog;

final static int RQS_1 = 1;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

textAlarmPrompt = (TextView)findViewById(R.id.alarmprompt);

buttonstartSetDialog = (Button)findViewById(R.id.startSetDialog);
buttonstartSetDialog.setOnClickListener(new OnClickListener(){

@Override
public void onClick(View v) {
textAlarmPrompt.setText("");
openTimePickerDialog(false);

        }
    });

}


private void openTimePickerDialog(boolean is24r){
    Calendar calendar = Calendar.getInstance();

    timePickerDialog = new TimePickerDialog(
    AndroidTimeActivity.this,
    onTimeSetListener,
    calendar.get(Calendar.HOUR_OF_DAY),
    calendar.get(Calendar.MINUTE),
    is24r);
    timePickerDialog.setTitle("Set Alarm Time");

    timePickerDialog.show();

}

OnTimeSetListener onTimeSetListener
    = new OnTimeSetListener(){

    @Override
    public void onTimeSet(TimePicker view, int hourOfDay, int minute) {

    Calendar calNow = Calendar.getInstance();
    Calendar calSet = (Calendar) calNow.clone();

    calSet.set(Calendar.HOUR_OF_DAY, hourOfDay);
    calSet.set(Calendar.MINUTE, minute);
    calSet.set(Calendar.SECOND, 0);
    calSet.set(Calendar.MILLISECOND, 0);

    if(calSet.compareTo(calNow) <= 0){

    calSet.add(Calendar.DATE, 1);
        }

        setAlarm(calSet);
    }
};

private void setAlarm(Calendar targetCal){

    Intent intent = new Intent(getBaseContext(), AlarmReceiver.class);
    PendingIntent pendingIntent = PendingIntent.getBroadcast(getBaseContext(), RQS_1, intent, 0);
    AlarmManager alarmManager = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
    alarmManager.set(AlarmManager.RTC_WAKEUP, targetCal.getTimeInMillis(), pendingIntent);

    mBuilder =
            new NotificationCompat.Builder(this);
                    mBuilder.setSmallIcon(R.drawable.icon1);
                    mBuilder.setContentTitle("My notification");
                    mBuilder.setContentText("Hello World!");
    mBuilder.setContentIntent(pendingIntent);

    mBuilder.setSound(Uri.parse("android.resource://com.example.applenett.myapplication/raw/ringtone1"));




    NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);




    notificationManager.notify(0, mBuilder.build());


}

}

AlarmReceiver.java

public class AlarmReceiver extends BroadcastReceiver {

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

    Toast.makeText(context, "Alarm received!", Toast.LENGTH_LONG).show();

}

}

的AndroidManifest.xml

<uses-permission android:name="com.android.alarm.permission.SET_ALARM" />


<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/AppTheme" >
    <activity
        android:name=".AndroidTimeActivity"
        android:label="@string/app_name"
        android:theme="@style/AppTheme.NoActionBar" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

    <receiver
        android:name=".AlarmReceiver"
        android:label="@string/title_activity_alarm_receiver"
        android:theme="@style/AppTheme.NoActionBar"
        android:process=":remote">
    </receiver>
</application>

1 个答案:

答案 0 :(得分:1)

您正在呼叫notificationManager.notify(0, mBuilder.build());,以便通知(及其随附的声音)将会触发,然后您要求它进行b / c。您应该将整个通知构建器(和通知)部分移动到警报接收器类,以便它在那时与吐司一起被触发。