我正在制作警报应用程序。它正在使用一些硬编码值,如
calendar.add(Calendar.SECOND,30);
30秒后警报被触发但是它无法使用
cal.set(Calendar.AM_PM,Calendar.PM);
cal.set(Calendar.MONTH,8);
cal.set(Calendar.YEAR,2011);
cal.set(Calendar.DAY_OF_MONTH,4);
cal.set(Calendar.HOUR_OF_DAY,5);
cal.set(Calendar.MINUTE,30);
以下是我的全部代码:
........活动课..................
public class SampleAlarm1Activity extends Activity {
public Context context;
private boolean alarmstarted=true;
Button setalarm;
Button start;
Button cancel;
Edit Text year;
EditText month;
EditText date;
EditText hour;
EditText minute;
Intent i;
PendingIntent pi;
AlarmManager am;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
context=getApplicationContext();
setalarm=(Button)findViewById(R.id.setalarm);
start=(Button) findViewById(R.id.start);
cancel=(Button)findViewById(R.id.cancelalarm);
month=(EditText)findViewById(R.id.month);
date=(EditText)findViewById(R.id.date);
hour=(EditText)findViewById(R.id.hour);
minute=(EditText)findViewById(R.id.minute);
setalarm.setOnClickListener(setalarmListener);
start.setOnClickListener(startalarmListener);
cancel.setOnClickListener(cancelalarm);
start.setVisibility(android.view.View.INVISIBLE);
month.setVisibility(android.view.View.INVISIBLE);
date.setVisibility(android.view.View.INVISIBLE);
hour.setVisibility(android.view.View.INVISIBLE);
minute.setVisibility(android.view.View.INVISIBLE);
}
private OnClickListener setalarmListener=new OnClickListener() {
@Override
public void onClick(View v) {
month.setVisibility(android.view.View.VISIBLE);
date.setVisibility(android.view.View.VISIBLE);
hour.setVisibility(android.view.View.VISIBLE);
minute.setVisibility(android.view.View.VISIBLE);
start.setVisibility(android.view.View.VISIBLE);
}
};
private OnClickListener startalarmListener=new OnClickListener() {
@Override
public void onClick(View v) {
int mon = 0,dat = 0,hr = 0,min = 0;
try{
String mon2=month.getText().toString();
System.out.println("month string entered::::::::::"+mon2);
mon=Integer.parseInt(mon2);
System.out.println("month int valuereturn entered::::::::::"+mon);
String dat2=date.getText().toString();
dat=Integer.parseInt(dat2);
System.out.println("date entered::::::::::"+dat);
String hr2=hour.getText().toString();
hr=Integer.parseInt(hr2);
System.out.println("hour entered::::::::::"+hr);
String min2=minute.getText().toString();
min=Integer.parseInt(min2);
System.out.println("minute entered::::::::::"+min);
}
catch(NumberFormatException exc){
System.out.println("exceptin is :"+exc);
}
Calendar cal=Calendar.getInstance();
cal.setTimeInMillis(System.currentTimeMillis());
cal.set(Calendar.AM_PM,Calendar.PM);
cal.set(Calendar.MONTH,mon);
System.out.println("month string entered::::::::::"+mon);
cal.set(Calendar.YEAR,2011);
cal.set(Calendar.DAY_OF_MONTH,dat);
System.out.println("date string entered::::::::::"+dat);
cal.set(Calendar.HOUR_OF_DAY,hr);
System.out.println("hour entered::::::::::"+hr);
cal.set(Calendar.MINUTE,min);
System.out.println("minute entered::::::::::"+min);
i = new Intent(SampleAlarm1Activity.this, AlarmReceiver.class);
pi=PendingIntent.getBroadcast(SampleAlarm1Activity.this, 0, i, 0);
AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
am.set(AlarmManager.RTC_WAKEUP,cal.getTimeInMillis(),pi);
System.out.println("alarm on in activity ");
Toast.makeText(context, "alarm on activity", Toast.LENGTH_SHORT).show();
}
};
private OnClickListener cancelalarm=new OnClickListener() {
@Override
public void onClick(View v) {
if(alarmstarted)
try{
am.cancel(pi);
}catch(Exception e){
System.out.println("alarm not started"+e);
}
System.out.println("alarm cancel ");
Toast.makeText(context, "alarm cancel", Toast.LENGTH_SHORT).show();
}
};
}
广播接收机代码....在同一个包中..................................... ......
public class AlarmReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent){
System.out.println("alarm start");
Toast.makeText(context, "alarm is working",Toast.LENGTH_SHORT).show();
}
}
清单文件..................................
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.android.SampleAlarm"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="8" />
<uses-permission android:name="android.permission.SET_ALARM"></uses-permission>
<uses-permission android:name="android.permission.WRITE_CALENDAR"></uses-permission>
<uses-permission android:name="com.android.alarm.permission.SET_ALARM"></uses-permission>
<uses-permission android:name="android.permission.SET_TIME"></uses-permission>
<uses-permission android:name="android.permission.SET_TIME_ZONE"></uses-permission>
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".SampleAlarm1Activity"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name=".AlarmReceiver"></receiver>
</application>
</manifest>
答案 0 :(得分:0)
我已经制作了相同的应用程序,但我从Time Picker中选择了时间,但你可以节省时间。这里我发布我的示例应用程序类。它可能对你有所帮助..
SampleReminderActivity.java
package com.reminder;
import java.util.Calendar;
import android.app.Activity;
import android.app.AlarmManager;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class SampleReminderActivity extends Activity {
private Button btn = null;
private AlarmManager alarmManager = null;
Calendar cal = Calendar.getInstance();
private Calendar calDay = Calendar.getInstance();
private Calendar dateStart = Calendar.getInstance();
static final int DIALOG_TIME = 0;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
btn = (Button) findViewById(R.id.btnClick);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v)
{
showDialog(DIALOG_TIME);
}
});
}
@Override
protected Dialog onCreateDialog(int id) {
Dialog dialog = null;
switch(id){
case DIALOG_TIME:
dialog = new TimePickerDialog(this, new TimePickerDialog.OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
Calendar c = Calendar.getInstance();
c.setTimeInMillis(System.currentTimeMillis());
// c.clear();
c.set(Calendar.YEAR, hourOfDay);
c.set(Calendar.HOUR_OF_DAY, hourOfDay);
c.set(Calendar.MINUTE, minute);
c.set(Calendar.SECOND, 0);
Log.d("SampleRemainder", "Before Intent");
Intent intent = new Intent(SampleRemainderActivity.this,AlarmReceiver.class);
// startActivity(intent);
Log.d("SampleRemainder", "After Intent");
Log.d("SampleRemainder", "Before PendingIntent");
PendingIntent pi = PendingIntent.getBroadcast(SampleRemainderActivity.this, 0, intent, 0);
Log.d("SampleRemainder", "After PendingIntent");
alarmManager.set(AlarmManager.RTC_WAKEUP, c.getTimeInMillis(), pi);
Toast.makeText(SampleRemainderActivity.this, "Alarm has been set..", Toast.LENGTH_LONG).show();
}
},cal.get(Calendar.HOUR_OF_DAY),cal.get(Calendar.MINUTE),false);
Log.d("SampleRemainder", "get cal Hour **" + cal.get(Calendar.HOUR_OF_DAY));
Log.d("SampleRemainder", "get cal Minute **" + cal.get(Calendar.MINUTE));
break;
}
return dialog;
}
}
AlarmReceiver.java
package com.reminder;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
public class AlarmReceiver extends BroadcastReceiver
{
@Override
public void onReceive(Context context, Intent intent)
{
Log.d("AlarmReceiver","in AlarmReceiver"+ context.toString());
Intent i = new Intent(context, AlarmActivity.class);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);
}
}
AlarmActivity.java
package com.reminder;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.os.Bundle;
import android.util.Log;
public class AlarmActivity extends Activity
{
@Override
protected void onCreate(Bundle savedInstanceState)
{
Log.d("AlarmActivity","in AlarmActivity");
super.onCreate(savedInstanceState);
new AlertDialog.Builder(AlarmActivity.this).setTitle("Task").setMessage("Time to wake up").setPositiveButton("ok", new OnClickListener()
{
@Override
public void onClick(DialogInterface dialog, int which) {
AlarmActivity.this.finish();
}
}).create().show();
}
}
main.xml中
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<Button
android:id="@+id/btnClick"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Click"
/>
</LinearLayout>
的Manifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.reminder"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="8" />
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".SampleReminderActivity"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".AlarmActivity"></activity>
<receiver android:name="com.reminder.AlarmReceiver" android:process=":remote"></receiver>
</application>
<uses-permission android:name="android.permission.READ_CALENDAR" />
<uses-permission android:name="android.permission.WRITE_CALENDAR" />
<uses-permission android:name="android.permission.INTERNET"/>
</manifest>
答案 1 :(得分:0)
嗨,朋友我的问题解决了。
注意:使用
cal.set(Calendar.AM_PM,Calendar.AM or PM);
和cal.set(Calendar.HOUR,hour);
12小时格式。
否则仅使用cal.set(Calendar.HOUR_OF_DAY, hour)
24小时格式。
其他重要的事情我做错了。
cal.set(Calendar.MONTH, month);
我八月份使用的是8,但它应该是7。 Actualy对于1月恒定值为0。
感谢您的关注。