2014-02-20 2 views
0

Я пытаюсь получить предупреждение в определенное время. Я использую 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); 

    } 

} 
+0

отправить сообщение об ошибке log cat –

+0

заменить вашу строку: nm = (NotificationManager) getSystemService (NOTIFICATION_SERVICE); с этим: nm = (NotificationManager) контекст \t \t \t \t .getSystemService (Activity.NOTIFICATION_SERVICE); – Vijju

+0

BY Activity ... вы имеете в виду, какой вид деятельности ... тот, в котором у меня есть диспетчер аварийных сообщений или текущий или тот, который он собирается открыть ??? – bhakti123

ответ

0

Используйте контекст, передаваемый:

nm=(NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); 

вместо:

nm=(NotificationManager)getSystemService(NOTIFICATION_SERVICE); 
+0

это не работает. Он показывает ошибку. – bhakti123

+0

Не могли бы вы вывести stacktrace или ошибку из logcat? Также попробуйте мое редактирование в ответе – youssefhassan

Смежные вопросы