我是android新手。我正在开发一个Android闹钟应用程序。我希望在每30分钟后的上午8点到晚上8点之间显示警报,并每天重复此警报。 我的代码在这里。 主要活动......
import java.util.Calendar;
import android.app.Activity;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.Switch;
import android.widget.Toast;
public class WaterActivity extends Activity {
Button leftBTN, rightBTN;
Switch toggleSwitch;
Context context = this;
SharedPref pref;
static PendingIntent pendingIntent;
static AlarmManager alarmManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_water);
leftBTN = (Button)findViewById(R.id.leftbtn);
rightBTN = (Button)findViewById(R.id.rightbtn);
toggleSwitch = (Switch)findViewById(R.id.switchbtn);
pref = new SharedPref(context);
if(pref.getValue(pref.getWaterKey()).equals("on"))
toggleSwitch.setChecked(true);
toggleSwitch.setOnCheckedChangeListener(SwitchListener);
leftBTN.setOnClickListener(new Button.OnClickListener() {
@Override
public void onClick(View v) {
leftBTN.setTextColor(getResources().getColor(R.color.water_color));
rightBTN.setTextColor(getResources().getColor(R.color.white_color));
leftBTN.setBackgroundResource(R.drawable.button_bg);
rightBTN.setBackgroundResource(R.drawable.button_right_bg2);
}
});
rightBTN.setOnClickListener(new Button.OnClickListener() {
@Override
public void onClick(View v) {
rightBTN.setTextColor(getResources().getColor(R.color.water_color));
leftBTN.setTextColor(getResources().getColor(R.color.white_color));
rightBTN.setBackgroundResource(R.drawable.button_right_bg);
leftBTN.setBackgroundResource(R.drawable.button_bg2);
}
});
}
private Switch.OnCheckedChangeListener SwitchListener = new Switch.OnCheckedChangeListener(){
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
pref.savePre_value(pref.getWaterKey(), "on");
Toast.makeText(context, "Reminder is on", Toast.LENGTH_SHORT).show();
Intent intentsOpen = new Intent(context, WaterReceiver.class);
pendingIntent = PendingIntent.getBroadcast(context,111, intentsOpen, 0);
alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 10000, pendingIntent);
} else {
pref.savePre_value(pref.getWaterKey(), "off");
Toast.makeText(context, "Reminder is off", Toast.LENGTH_SHORT).show();
alarmManager.cancel(pendingIntent);
}
}
};
@Override
public void onBackPressed() {
// TODO Auto-generated method stub
super.onBackPressed();
finish();
}
}
这是我的接收者。
public class WaterReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context, "Time is up!!!!.",
Toast.LENGTH_LONG).show();
}
}
请帮助我在这里缺少什么? 我怎么能在android.any中做到这一点可以帮助我吗? 谢谢你的开始。
答案 0 :(得分:3)
Preety简单流程:
实现起来相当简单,示例代码
public void setDailyAlarms(Context context){
// Daily Alarm
AlarmManager manager = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, 8);
calendar.set(Calendar.MINUTE, 0);
Intent intent = new Intent("DAIL_ALARM_TRIGGERED");
PendingIntent pIntent = PendingIntent.getBroadcast(context,
100, intent, PendingIntent.FLAG_UPDATE_CURRENT);
manager.setRepeating(AlarmManager.RTC_WAKEUP,
calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pIntent);
}
public void setRepeatingAlarm(Context context){
// Alarm 30 min each'
AlarmManager manager = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent("REPEATING_ALARM_TRIGGERED");
PendingIntent pIntent = PendingIntent.getBroadcast(context,
102, intent, PendingIntent.FLAG_UPDATE_CURRENT);
manager.setRepeating(AlarmManager.RTC_WAKEUP,
System.currentTimeMillis(), 30*60*1000, pIntent);
}
public void cancelRepeatingAlarm(Context context){
AlarmManager manager = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent("REPEATING_ALARM_TRIGGERED");
PendingIntent pIntent = PendingIntent.getBroadcast(context,
102, intent, PendingIntent.FLAG_UPDATE_CURRENT);
manager.cancel(pIntent);
}
现在,您需要一个广播接收器才能接收"DAIL_ALARM_TRIGGERED"
意图,一旦收到此消息,您就拨打setRepeatingAlarm()
,现在这将在您收到此消息后点击广播"REPEATING_ALARM_TRIGGERED"
是8PM是,然后拨打cancelRepeatingAlarm()
;
我想这足以让你实现你的逻辑。