2014-12-01 5 views
0

Я получаю «Метод getSystemService (String) не определен для ошибки типа AlarmManagerBroadcastReceiver». Я даже пытался поставить getActivity() перед этим, но это не помогло.Попытка реализовать будильник с уведомлением

код AlarmManagerBroadcastReceiver.java

package com.archana.pocketfriendly; 

import java.text.Format; 
import java.text.SimpleDateFormat; 

import android.app.AlarmManager; 
import android.app.NotificationManager; 
import android.app.PendingIntent; 
import android.app.TaskStackBuilder; 
import android.content.BroadcastReceiver; 
import android.content.Context; 
import android.content.Intent; 
import android.media.MediaPlayer; 
import android.os.Bundle; 
import android.os.PowerManager; 
import android.os.Vibrator; 
import android.support.v4.app.NotificationCompat; 
import android.widget.Toast; 

public class AlarmManagerBroadcastReceiver extends BroadcastReceiver { 

final public static String ONE_TIME = "onetime"; 
MediaPlayer alarm; 
@Override 
public void onReceive(Context context, Intent intent) { 
    PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE); 
    PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "YOUR TAG"); 
    //Acquire the lock 
    wl.acquire(); 

    //You can do the processing here update the widget/remote views. 
    Bundle extras = intent.getExtras(); 
    StringBuilder msgStr = new StringBuilder(); 

    if(extras != null && extras.getBoolean(ONE_TIME, Boolean.FALSE)){ 
     msgStr.append("One time Timer : "); 
    } 
    Format formatter = new SimpleDateFormat("hh:mm:ss a"); 
    alarm = MediaPlayer.create(context, R.raw.alarm); 
    alarm.start(); 
    msgStr.append("Do you want to enter the expenses?\nIgnore if already done!"); 
    Toast.makeText(context, msgStr, Toast.LENGTH_LONG).show(); 

    Vibrator vib=(Vibrator)context.getSystemService(Context.VIBRATOR_SERVICE); 
    vib.vibrate(2000); 

    // notification 
    NotificationCompat.Builder mBuilder = 
      new NotificationCompat.Builder(context) 
      .setSmallIcon(R.drawable.settings) 
      .setContentTitle("My notification") 
      .setContentText("Hello World!"); 
    // Creates an explicit intent for an Activity in your app 
    Intent resultIntent = new Intent(context, MainMenu.class); 

    // The stack builder object will contain an artificial back stack for the 
    // started Activity. 
    // This ensures that navigating backward from the Activity leads out of 
    // your application to the Home screen. 
    TaskStackBuilder stackBuilder = TaskStackBuilder.create(context); 
    // Adds the back stack for the Intent (but not the Intent itself) 
    stackBuilder.addParentStack(MainMenu.class); 
    // Adds the Intent that starts the Activity to the top of the stack 
    stackBuilder.addNextIntent(resultIntent); 
    PendingIntent resultPendingIntent = 
      stackBuilder.getPendingIntent(
       0, 
       PendingIntent.FLAG_UPDATE_CURRENT 
      ); 
    mBuilder.setContentIntent(resultPendingIntent); 
    NotificationManager mNotificationManager = 
     (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); //error in this  `enter code here`line. 
    mNotificationManager.notify(0, mBuilder.build()); 

    //Release the lock 
    wl.release(); 


} 
public void SetAlarm(Context context) 
{ 
    AlarmManager am=(AlarmManager)context.getSystemService(Context.ALARM_SERVICE); 
    Intent intent = new Intent(context, AlarmManagerBroadcastReceiver.class); 
    intent.putExtra(ONE_TIME, Boolean.FALSE); 
    PendingIntent pi = PendingIntent.getBroadcast(context, 0, intent, 0); 
    //After after 30 seconds 
    am.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), 1000 * 86400 , pi); 
} 

public void CancelAlarm(Context context) 
{ 
    Intent intent = new Intent(context, AlarmManagerBroadcastReceiver.class); 
    PendingIntent sender = PendingIntent.getBroadcast(context, 0, intent, 0); 
    AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE); 
    alarmManager.cancel(sender); 
} 
public void setOnetimeTimer(Context context){ 
    AlarmManager am=(AlarmManager)context.getSystemService(Context.ALARM_SERVICE); 
    Intent intent = new Intent(context, AlarmManagerBroadcastReceiver.class); 
    intent.putExtra(ONE_TIME, Boolean.TRUE); 
    PendingIntent pi = PendingIntent.getBroadcast(context, 0, intent, 0); 
    am.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), pi); 
} 
} 

Пожалуйста, дайте мне знать, если требуется любая другая информация.

ответ

1

Так же, как другие ваши звонки в getSystemService, вам необходимо выполнить его на объект контекста:

NotificationManager mNotificationManager = 
     (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); 
Смежные вопросы