2016-07-31 12 views
0

Ich versuche eine Benachrichtigung zu senden, wenn ich einen bestimmten Alarm in meiner Alarm Receiver-Klasse erhalte, der dann eine Benachrichtigung an einen anderen Dienst sendet, der im Hintergrund läuft.So implementieren Sie Benachrichtigungen in einem Service?

ich diese Störung erhalte:

Attempt to invoke virtual method 'java.lang.String android.content.Context.getPackageName()' on a null object reference 

Ich habe viel zu diesem Thema recherchiert, aber keine der Lösungen, die ich gefunden habe, gearbeitet.

Dies ist Alarmempfängerklasse

public void onReceive(Context context, Intent intent) 
    { 
     switch(intent.getIntExtra("DosageIdentifier",-1)) { 
      case 1: 
      { 
       for(int i = 0; i < SetMyAlarm.countSeqWithDatabaseList.size(); i++) 
       { 
        prescriptionString = SetMyAlarm.prescriptionList.get(i); 
        myNotification.acceptNotificationString(prescriptionString); 

       } 
       break; 
      } 

Dies ist mein SetMyAlarm Dienst

public void acceptNotificationString(String text) 
    { 
     sendNotification(text); 
    } 
    private void sendNotification(String text) { 
     try { 
      Intent notificationIntent = new Intent(this, MainActivity.class); 
      notificationIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 

      int flags = PendingIntent.FLAG_UPDATE_CURRENT; 
      PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, flags); 

      int icon = R.mipmap.ic_launcher; 
      CharSequence tickerText = "ALERT"; 
      CharSequence contentTitle = "Notification"; 
      CharSequence contentText = text; 

      Notification notification = new Notification.Builder(this) 
        .setSmallIcon(icon) 
        .setTicker(tickerText) 
        .setContentTitle(contentTitle) 
        .setContentText(contentText) 
        .setContentIntent(pendingIntent) 
        .setAutoCancel(true) 
        .build(); 

      NotificationManager manager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE); 
      final int NOTIFICATION_ID = 1; 
      manager.notify(NOTIFICATION_ID, notification); 
     } 
     catch (NullPointerException e) 
     { 
      e.printStackTrace(); 
     } 
    } 

Dies ist der Fehler Ich erhalte:

java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.content.Context.getPackageName()' on a null object reference 
    at android.content.ContextWrapper.getPackageName(ContextWrapper.java:133) 
    at android.content.ComponentName.<init>(ComponentName.java:128) 
    at android.content.Intent.<init>(Intent.java:4449) 
    at timertesting.kaad.timertesting.SetMyAlarm.sendNotification(SetMyAlarm.java:564) 
    at timertesting.kaad.timertesting.SetMyAlarm.acceptNotificationString(SetMyAlarm.java:560) 
    at timertesting.kaad.timertesting.AlarmReceiver.onReceive(AlarmReceiver.java:23) 
    at android.app.ActivityThread.handleReceiver(ActivityThread.java:2725) 
    at android.app.ActivityThread.-wrap14(ActivityThread.java) 
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1421) 
    at android.os.Handler.dispatchMessage(Handler.java:102) 
    at android.os.Looper.loop(Looper.java:148) 
    at android.app.ActivityThread.main(ActivityThread.java:5417) 
    at java.lang.reflect.Method.invoke(Native Method) 
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 

Irgendwelche Vorschläge?

+0

acceptNotificationString() ist jede Klasse Teil? Aktivität, Service? Fügen Sie mehr Code ein – W0rmH0le

Antwort

0

Könnten Sie bitte den folgenden Code ausprobieren?

public void onReceive(Context context, Intent intent) { 
    ... 
    for(int i = 0; i < SetMyAlarm.countSeqWithDatabaseList.size(); i++) { 
     prescriptionString = SetMyAlarm.prescriptionList.get(i); 
     myNotification.acceptNotificationString(context, prescriptionString); 
    } 
    ... 
} 


public void acceptNotificationString(Context context, String text) { 
    sendNotification(context, text); 
} 

private void sendNotification(Context context, String text) { 
    try { 
     Intent notificationIntent = new Intent(context, MainActivity.class); 
     .... 
     PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, notificationIntent, flags); 
     ... 
     Notification notification = new Notification.Builder(context) 
     .... 
     NotificationManager manager = (NotificationManager) context.getSystemService(NOTIFICATION_SERVICE); 
    } 
} 
+0

Ich habe meinen Code wie gesagt geändert, aber jetzt liegt meine NullPointerException im Code 'NotificationManager Manager = (NotificationManager) getSystemService (NOTIFICATION_SERVICE);' –

+0

Versuchen Sie dies: NotificationManager Manager = (NotificationManager) context.getSystemService (NOTIFICATION_SERVICE); – W0rmH0le

+0

Vielen Dank, es hat funktioniert. Aber kannst du mir bitte erklären, warum wir das machen mussten? –

Verwandte Themen