报警不起作用

时间:2014-11-01 10:57:54

标签: android android-alarms

警报在项目中无法正常工作,警报设置成功但是设备和模拟器无响应任何人都可以提前帮助我!

DiaryAddRem_BUSINESS.java

public class DiaryAddRem_BUSINESS  extends Activity implements OnClickListener{

EditText business_name_et,business_title_et,business_location_et,business_description_et;
TextView business_reminder_et,business_date_et,business_time_et;
Button business_save,business_cancel;
SQLREM_BUSINESS_Controller dbcon;
private String[] business_reminder_list={"5 mintues","15 minutes","30 minutes",
"1 hour before","hour before","1 day before","2 days before","1 week before"};

private Calendar cal;

private int hour;
private int minute;
private int day;
private int month;
private int year;


private  Calendar mcurrentTime;

final static int RQS_1 = 1;




@Override
protected void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, 
                                    WindowManager.LayoutParams.FLAG_FULLSCREEN);
    setContentView(R.layout.diary_rem_business);


    cal = Calendar.getInstance();
    day = cal.get(Calendar.DAY_OF_MONTH);
    month = cal.get(Calendar.MONTH);
    year = cal.get(Calendar.YEAR);

    business_name_et = (EditText) findViewById(R.id.business_name);
    business_title_et = (EditText) findViewById(R.id.business_title);
    business_date_et = (TextView) findViewById(R.id.business_date);

    business_date_et.setOnClickListener(new View.OnClickListener() {  

        public void onClick(View v) 
          {         

            showDialog(0);      
            }
             });      


    business_time_et = (TextView) findViewById(R.id.business_time);
    business_time_et.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            Calendar mcurrentTime = Calendar.getInstance();

            hour = mcurrentTime.get(Calendar.HOUR_OF_DAY);
            minute = mcurrentTime.get(Calendar.MINUTE);

            TimePickerDialog mTimePicker;
            mTimePicker = new TimePickerDialog(DiaryAddRem_BUSINESS.this, new TimePickerDialog.OnTimeSetListener() {
                @Override
                public void onTimeSet(TimePicker timePicker, int selectedHour, int selectedMinute) {
                    business_time_et.setText( selectedHour + ":" + selectedMinute);

                }
            }, hour, minute, true);//Yes 24 hour time
            mTimePicker.setTitle("Select Time");
            mTimePicker.show();

        }
    });








    business_location_et = (EditText) findViewById(R.id.business_location);
    business_description_et = (EditText) findViewById(R.id.business_description);
    business_reminder_et = (TextView) findViewById(R.id.business_reminder);
    business_reminder_et.setInputType(InputType.TYPE_NULL); //To hide the softkeyboard


    final ArrayAdapter<String> spinner_countries1 = new  ArrayAdapter<String>(DiaryAddRem_BUSINESS.this,android.R.layout.simple_spinner_dropdown_item, business_reminder_list);

    business_reminder_et.setOnClickListener(new OnClickListener() {

        public void onClick(View v) {
            new AlertDialog.Builder(DiaryAddRem_BUSINESS.this)
                  .setTitle("select reminder")
                  .setAdapter(spinner_countries1, new DialogInterface.OnClickListener() {

                    public void onClick(DialogInterface dialog, int which) {
                        business_reminder_et.setText(business_reminder_list[which].toString());

                      dialog.dismiss();
                    }
                  }).create().show();
        }
    });


    business_save = (Button) findViewById(R.id.business_save);
    business_cancel = (Button) findViewById(R.id.business_cancel);
    business_cancel.setOnClickListener(new View.OnClickListener() {                                            
        public void onClick(View v) 
          {             
    Intent launchactivity= new Intent(DiaryAddRem_BUSINESS.this,DiaryRemList_BUSINESS.class);                               
    startActivity(launchactivity);                          
            }
             });  


    dbcon = new SQLREM_BUSINESS_Controller(this);
    dbcon.open();


    business_save.setOnClickListener(this);

}

public void onBackPressed() {
    // Do Here what ever you want do on back press;

      Intent launchactivity= new Intent(DiaryAddRem_BUSINESS.this,DiaryRemList_BUSINESS.class);                               
        startActivity(launchactivity);    

}

public void onClick1(View v) {
    showDialog(0);
}

@Deprecated
protected Dialog onCreateDialog(int id) {
    return new DatePickerDialog(this, datePickerListener, year, month, day);
}
DatePickerDialog.OnDateSetListener datePickerListener = new DatePickerDialog.OnDateSetListener() {
    public void onDateSet(DatePicker view, int selectedYear,
            int selectedMonth, int selectedDay) {
        business_date_et.setText(selectedDay + " / " + (selectedMonth + 1) + " / "
                + selectedYear);


    }
};

public void onClick(View v){


    switch (v.getId()) {

    case R.id.business_save:
        String business_name = business_name_et.getText().toString();   
        String business_title = business_title_et.getText().toString(); 
        String business_date       = business_date_et.getText().toString();
        String business_time   = business_time_et.getText().toString();
        String business_location      = business_location_et.getText().toString();
        String business_description       = business_description_et.getText().toString();

        String business_reminder   = business_reminder_et.getText().toString();

        Intent i = new Intent("com.appsinbox.mydiary.demoactivity");
        PendingIntent operation = PendingIntent.getActivity(getBaseContext(), 0, i, Intent.FLAG_ACTIVITY_NEW_TASK);
        AlarmManager alarmManager = (AlarmManager) getBaseContext().getSystemService(ALARM_SERVICE);
        GregorianCalendar calendar = new GregorianCalendar(year,month+1,day, hour, minute+1);
        System.out.println(year);
        System.out.println(month+1);
        System.out.println(day);
        System.out.println(hour);
        System.out.println(minute+1);

        long alarm_time = calendar.getTimeInMillis();
        alarmManager.set(AlarmManager.RTC_WAKEUP  , alarm_time , operation);
        Toast.makeText(getBaseContext(), "Alarm is set successfully",Toast.LENGTH_SHORT).show();




        if(business_name.equals("")||business_title.equals("")||business_date.equals("")||business_time.equals("")||business_location.equals("")||business_reminder.equals(""))
        {
            Toast.makeText(getApplicationContext(), "Please enter mandatory fields", Toast.LENGTH_LONG).show();
            return; 
        }
        else
        {
            dbcon.insertData(business_name,business_title, business_date, business_time,  business_location,business_description,
                    business_reminder);



            Intent in = new Intent(DiaryAddRem_BUSINESS.this, DiaryRemList_BUSINESS.class)

            .setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            startActivity(in);
        }




        break;

    default:
        break;
    }

    /*
    Intent i = new Intent("in.wptrafficanalyzer.servicealarmdemo.demoactivity");

    GregorianCalendar calendar = new GregorianCalendar(year,month+1,day, hour, minute+1);
    System.out.println(year);
    System.out.println(month+1);
    System.out.println(day);
    System.out.println(hour);
    System.out.println(minute+1);
    long alarm_time = calendar.getTimeInMillis();

    PendingIntent operation = PendingIntent.getActivity(getBaseContext(), 0, i, Intent.FLAG_ACTIVITY_NEW_TASK);
    AlarmManager alarmManager = (AlarmManager) getBaseContext().getSystemService(ALARM_SERVICE);
    alarmManager.set(AlarmManager.RTC_WAKEUP  , alarm_time , operation);
    Toast.makeText(getBaseContext(), "Alarm is set successfully",Toast.LENGTH_SHORT).show();
    */

}

}

AlertDemo .java

public class AlertDemo extends DialogFragment {

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {

    /** Turn Screen On and Unlock the keypad when this alert dialog is displayed */
    getActivity().getWindow().addFlags(LayoutParams.FLAG_TURN_SCREEN_ON | LayoutParams.FLAG_DISMISS_KEYGUARD);


    /** Creating a alert dialog builder */
    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());

    /** Setting title for the alert dialog */
    builder.setTitle("Alarm");

    /** Setting the content for the alert dialog */
    builder.setMessage("An Alarm by AlarmManager");

    /** Defining an OK button event listener */
    builder.setPositiveButton("OK", new OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            /** Exit application on click OK */
            getActivity().finish();
        }                       
    });

    /** Creating the alert dialog window */
    return builder.create();
}

/** The application should be exit, if the user presses the back button */ 
@Override
public void onDestroy() {       
    super.onDestroy();
    getActivity().finish();
}}

DemoActivity.java

public class DemoActivity extends FragmentActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {        
    super.onCreate(savedInstanceState); 

    /** Creating an Alert Dialog Window */
    AlertDemo alert = new AlertDemo();

    /** Opening the Alert Dialog Window */
    alert.show(getSupportFragmentManager(), "AlertDemo");       
}}

1 个答案:

答案 0 :(得分:0)

当您设置正在进行广播的AlarmManager.RTC_WAKEUP时,您需要一个广播接收器,在接收时启动报警活动
例如,当您按下设置警报按钮

public void onClick(View v) {
            Intent intent = new Intent(getBaseContext(), AlarmBroadcastReciever.class);
            intent.putExtra("message", "alarm 1 starts");
            PendingIntent alarmIntent = PendingIntent.getBroadcast(getBaseContext(), 0, intent, 0);
            long startTime = ...
            AlarmManager alm = (AlarmManager) getSystemService(ALARM_SERVICE);
            alm.set(AlarmManager.RTC_WAKEUP, startTime, alarmIntent);
            Toast.makeText(.....Toast.LENGTH_LONG).show();
        }
    };

现在你需要AlarmBroadcastReciever

public class AlarmBroadcastReciever extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        String msg = intent.getStringExtra("message");
        Toast.makeText(context, msg, Toast.LENGTH_SHORT).show();
        Intent alarmPage = new Intent(context, Alarm.class);
        alarmPage.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(alarmPage);
    }

}