80,361
社区成员
发帖
与我相关
我的任务
分享
public void setAlarm(long id){
Log.e("AndroidBWL","setAlarm start...");
String noticeDate=etDate.getText().toString();
String noticeTime=etTime.getText().toString();
Calendar calendar=Calendar.getInstance();
calendar.set(Integer.parseInt(noticeDate.split("_")[0]),
Integer.parseInt(noticeDate.split("_")[1])-1,
Integer.parseInt(noticeDate.split("_")[2]),
Integer.parseInt(noticeTime.split(":")[0]),
Integer.parseInt(noticeTime.split(":")[1]));
Log.e("AndroidBWL",""+(calendar.getTimeInMillis()-System.currentTimeMillis()));
alarmManager=(AlarmManager)getSystemService(Context.ALARM_SERVICE);
Intent intent=new Intent(AddBwlActivity.this,AlarmReceiver.class);
Bundle bundle=new Bundle();
bundle.putLong("id",id);
bundle.putString("title",etTitle.getText().toString());
bundle.putString("content",etContent.getText().toString());
bundle.putString("noticeDate",etDate.getText().toString());
bundle.putString("noticeTime",etTime.getText().toString());
intent.putExtras(bundle);
intent.setAction("ALARM_ACTION"+calendar.getTimeInMillis());
PendingIntent pi=PendingIntent.getBroadcast(AddBwlActivity.this,0,intent,PendingIntent.FLAG_UPDATE_CURRENT);
alarmManager.set(AlarmManager.RTC_WAKEUP,calendar.getTimeInMillis()+5000,pi);
Log.d("AndroidBWL",""+calendar.getTimeInMillis());
Log.e("AndroidBWL","setAlarm end...");
}
public void onReceive(Context context,Intent intent){
Log.e("AndroidBWL","AlarmReceiver:onReceive");
mIntent=intent;
Bundle bundle=mIntent.getExtras();
mNotificationManager=(NotificationManager)context.getSystemService(context.NOTIFICATION_SERVICE);
mIntent.setClass(context,AddBwlActivity.class);
mPendingIntent=PendingIntent.getActivity(context,0,mIntent,0);
Notification.Builder builder=new Notification.Builder(context);
builder.setAutoCancel(true);
builder.setContentTitle(bundle.getString("title"));
builder.setContentText(bundle.getString("content"));
builder.setDefaults(Notification.DEFAULT_SOUND|Notification.DEFAULT_VIBRATE);
builder.setContentIntent(mPendingIntent);
Notification notification=builder.getNotification();
notification.flags|=Notification.FLAG_AUTO_CANCEL;
mNotificationManager.notify(1,notification);
}
private void registerBoradcastReceiver() {
IntentFilter myIntentFilter = new IntentFilter();
myIntentFilter.addAction(ACTION_NAME);
myIntentFilter.addAction(ACTION_NAME2);
// 注册广播
registerReceiver(mBroadcastReceiver, myIntentFilter);
}
我网上找的代码,我这样写对吗protected void onResume(){
super.onResume();
Cursor cursor=db.query("tb_bwl",new String[]{"id as _id","title","content","noticeDate","noticeTime"},null,null,null,null,null);
adapter.changeCursor(cursor);
}
这是我的代码,这样算不算注册了广播
网上看到有一个源码里是下面这样写的,我是不是也该这样写
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
// 注册广播
registerBoradcastReceiver();
}
可以详细的说一下吗protected void onResume(){
super.onResume();
Cursor cursor=db.query("tb_bwl",new String[]{"id as _id","title","content","noticeDate","noticeTime"},null,null,null,null,null);
adapter.changeCursor(cursor);
}
这是我的代码,这样算不算注册了广播
网上看到有一个源码里是下面这样写的,我是不是也该这样写
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
// 注册广播
registerBoradcastReceiver();
}
可以详细的说一下吗,我新手,不是很懂