Я пытаюсь получить предупреждение в определенное время. Я использую AlarmManager
и BroadcastReceiver
для этого. В основном действии AlarmManager
звонит BroadcastReceiver
, но я не могу получить уведомление, чтобы стрелять из этого класса. Это дает мне ошибку в строке nm=(NotificationManager)getSystemService(NOTIFICATION_SERVICE);
Я не знаю, что делать.Как получить уведомление в определенное время?
Вот мой код
MainActivity
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();
}}`
BroadcastReciever
`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);
}
}
отправить сообщение об ошибке log cat –
заменить вашу строку: nm = (NotificationManager) getSystemService (NOTIFICATION_SERVICE); с этим: nm = (NotificationManager) контекст \t \t \t \t .getSystemService (Activity.NOTIFICATION_SERVICE); – Vijju
BY Activity ... вы имеете в виду, какой вид деятельности ... тот, в котором у меня есть диспетчер аварийных сообщений или текущий или тот, который он собирается открыть ??? – bhakti123