如何在Android中以编程方式设置闹钟?
以下是我的代码,任何人都可以告诉我为什么它不起作用。我从 学到了它本教程。但它不起作用,任何帮助都是可观的。
我的代码是
import java.util.Calendar;
import android.app.Activity;
import android.app.AlarmManager;
import android.app.ListActivity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Toast;
public class Notify extends Activity {
Button btn;
/** Called when the activity is first created. */
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.nit);
Calendar cal=Calendar.getInstance();
cal.set(Calendar.MONTH,6);
cal.set(Calendar.YEAR,2011);
cal.set(Calendar.DAY_OF_MONTH,29);
cal.set(Calendar.HOUR_OF_DAY,17);
cal.set(Calendar.MINUTE,30);
//String[] dude=new String[] {"nitin","avi","aman","rahul","pattrick","minkle","manmohan","nitin","nitin"};
//setListAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_expandable_list_item_1,dude));
//getListView().setTextFilterEnabled(true);
//String[] dude1=new String[] {"nitin","avi","aman","rahul","pattrick","minkle","manmohan"};
Intent intent = new Intent(this, Mote.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this.getApplicationContext(), 1253, intent, PendingIntent.FLAG_UPDATE_CURRENT| Intent.FILL_IN_DATA);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(),pendingIntent );
Toast.makeText(this, "Alarm worked.", Toast.LENGTH_LONG).show();
}
}
,我的接收器类是
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;
public class Mote extends BroadcastReceiver{
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
Toast.makeText(context, "Alarm worked.", Toast.LENGTH_LONG).show();
int icon = R.drawable.icon;
CharSequence tickerText = "Hello you have to take medicine I am Nitin Sharma";
long when = System.currentTimeMillis();
//Notification notification = new Notification(icon, tickerText,when );
CharSequence contentTitle = "My notification";
CharSequence contentText = "Hello World!";
//notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);
final int NOTIF_ID = 1234;
NotificationManager notofManager = (NotificationManager)context. getSystemService(Context.NOTIFICATION_SERVICE);
// Notification note = new Notification(R.drawable.face,"NEW ACTIVITY", System.currentTimeMillis());
Intent notificationIntent = new Intent(context, Alset.class);
PendingIntent contentIntent = PendingIntent.getActivity(context,0, notificationIntent, 0);
Notification notification = new Notification(icon, tickerText,when );
//Notification notification1 = new Notification(R.drawable.icon, "Wake up alarm", System.currentTimeMillis());
notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);
notification.flags = Notification.FLAG_INSISTENT;
notification.defaults |= Notification.DEFAULT_SOUND;
//notification.setLatestEventInfo(context, "My Activity", "This will runs on button click", contentIntent);
notofManager.notify(NOTIF_ID,notification);
//PendingIntent contentIntent = PendingIntent.getActivity(context, 0, new Intent(context, MainActivity.class), 0);
//notification.setLatestEventInfo(context, "Context Title", "Context text", contentIntent);
//notification.flags = Notification.FLAG_INSISTENT;
}
}
Following is my code can Any body please tell me why it is not working .I have learned it from this tutorial. But it is not working any help will be appreciable.
My code is
import java.util.Calendar;
import android.app.Activity;
import android.app.AlarmManager;
import android.app.ListActivity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Toast;
public class Notify extends Activity {
Button btn;
/** Called when the activity is first created. */
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.nit);
Calendar cal=Calendar.getInstance();
cal.set(Calendar.MONTH,6);
cal.set(Calendar.YEAR,2011);
cal.set(Calendar.DAY_OF_MONTH,29);
cal.set(Calendar.HOUR_OF_DAY,17);
cal.set(Calendar.MINUTE,30);
//String[] dude=new String[] {"nitin","avi","aman","rahul","pattrick","minkle","manmohan","nitin","nitin"};
//setListAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_expandable_list_item_1,dude));
//getListView().setTextFilterEnabled(true);
//String[] dude1=new String[] {"nitin","avi","aman","rahul","pattrick","minkle","manmohan"};
Intent intent = new Intent(this, Mote.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this.getApplicationContext(), 1253, intent, PendingIntent.FLAG_UPDATE_CURRENT| Intent.FILL_IN_DATA);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(),pendingIntent );
Toast.makeText(this, "Alarm worked.", Toast.LENGTH_LONG).show();
}
}
and my Receiver class is
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;
public class Mote extends BroadcastReceiver{
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
Toast.makeText(context, "Alarm worked.", Toast.LENGTH_LONG).show();
int icon = R.drawable.icon;
CharSequence tickerText = "Hello you have to take medicine I am Nitin Sharma";
long when = System.currentTimeMillis();
//Notification notification = new Notification(icon, tickerText,when );
CharSequence contentTitle = "My notification";
CharSequence contentText = "Hello World!";
//notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);
final int NOTIF_ID = 1234;
NotificationManager notofManager = (NotificationManager)context. getSystemService(Context.NOTIFICATION_SERVICE);
// Notification note = new Notification(R.drawable.face,"NEW ACTIVITY", System.currentTimeMillis());
Intent notificationIntent = new Intent(context, Alset.class);
PendingIntent contentIntent = PendingIntent.getActivity(context,0, notificationIntent, 0);
Notification notification = new Notification(icon, tickerText,when );
//Notification notification1 = new Notification(R.drawable.icon, "Wake up alarm", System.currentTimeMillis());
notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);
notification.flags = Notification.FLAG_INSISTENT;
notification.defaults |= Notification.DEFAULT_SOUND;
//notification.setLatestEventInfo(context, "My Activity", "This will runs on button click", contentIntent);
notofManager.notify(NOTIF_ID,notification);
//PendingIntent contentIntent = PendingIntent.getActivity(context, 0, new Intent(context, MainActivity.class), 0);
//notification.setLatestEventInfo(context, "Context Title", "Context text", contentIntent);
//notification.flags = Notification.FLAG_INSISTENT;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我认为您想将闹钟设置为 6 月 26 日而不是 7 月 26 日。如果是这样,则将
cal.set(Calendar.MONTH,6);
更改为cal.set(Calendar.MONTH,5);
因为月份是从零开始的。如果您打算在 7 月 26 日触发警报,则预计警报将在日期时间为 2011 年 7 月 26 日 17:30 时触发I think you want to set the alarm for 26th June and not 26th July. If so then change
cal.set(Calendar.MONTH,6);
tocal.set(Calendar.MONTH,5);
because the months are zero-based. if you intend the alarm to fire on 26th july then it is expected that the alarm will fire when the date-time is 26th July 2011, 17:301.将其添加到清单文件中
创建一个活动
3 . xml文件包含
4个.make报警管理器类
1.add this in manifest file
make a activity
3 . xml file include
4 .make alarm manager class
您是否在清单文件中设置了接收器?
像这样的东西
Have you set the receiver in your manifest file?
Something like this
希望这会有所帮助
hope this will help