[英]How to get notification at a specific time?
我试图在特定时间发出通知。 我AlarmManager
使用了AlarmManager
和BroadcastReceiver
在主要活动中, AlarmManager
调用了BroadcastReceiver
但是我无法从该类获取触发通知。 这在行nm=(NotificationManager)getSystemService(NOTIFICATION_SERVICE);
给我一个错误nm=(NotificationManager)getSystemService(NOTIFICATION_SERVICE);
我不知道该怎么办。
这是我的代码
主要活动
public class MainActivity extends Activity implements OnClickListener{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@SuppressWarnings("deprecation")
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
long current_time=System.currentTimeMillis();
Calendar time9=Calendar.getInstance();
time9.set(Calendar.HOUR_OF_DAY,16);
time9.set(Calendar.MINUTE,30);
time9.set(Calendar.SECOND,0);
Intent intent=new Intent(MainActivity.this,ScheduledReciever.class);
PendingIntent pintent=PendingIntent.getActivity(getApplicationContext(), 0, intent,0);
AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
long interval = 60 * 1000; //
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,time9.getTimeInMillis(), interval, pintent);
finish();
}}`
广播接收者
`public class ScheduledReciever extends BroadcastReceiver {
static final int uniqueId=1234;
NotificationManager nm;
@SuppressWarnings("deprecation")
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
nm=(NotificationManager)getSystemService(NOTIFICATION_SERVICE);
String body="i hope this works";
String title="trying";
Intent it=new Intent(context,SecondActivity.class);
PendingIntent pit=PendingIntent.getActivity(context, 0, it,0);
Notification n=new Notification(R.drawable.att,body,System.currentTimeMillis());
n.setLatestEventInfo(context, title, body, pit);
n.defaults=Notification.DEFAULT_ALL;
nm.notify(uniqueId, n);
}
}
使用传递的上下文:
nm=(NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
代替:
nm=(NotificationManager)getSystemService(NOTIFICATION_SERVICE);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.