Note that there are some explanatory texts on larger screens.

plurals
  1. POSending notification to notificationbar
    primarykey
    data
    text
    <p>Im trying to make a Notification Activity that other classes can reuse and put in extra information. What i am struggling with is how to correctly call the method from another class.</p> <p>Code for Notification Activity:</p> <p>public class NotificationTest extends MyActivity{</p> <pre><code>public void showNotification(String s, Context c) { // define sound URI, the sound to be played when there's a notification Uri soundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); // intent triggered, you can add other intent for other actions //Intent intent = new Intent(Sender context, which class to start); Intent intent = new Intent(c, Settings.class); //PendingIntent pIntent = PendingIntent.getActivity(sender context, 0, intent, 0); PendingIntent pIntent = PendingIntent.getActivity(c, 0, intent, 0); // this is it, we'll build the notification! // in the addAction method, if you don't want any icon, just set the first param to 0 Notification mNotification = new Notification.Builder(c) .setContentTitle("New Post!") .setContentText(s) .setSmallIcon(R.drawable.icon) .setContentIntent(pIntent) .setSound(soundUri) //.addAction(R.drawable.icon, "View",pIntent) //.addAction(0, "Remind", pIntent) .build(); Log.d("Vindsiden", "Notification"); NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); // If you want to hide the notification after it was selected, do the code below // myNotification.flags |= Notification.FLAG_AUTO_CANCEL; notificationManager.notify(0, mNotification); } } </code></pre> <p>Code for calling the Notification from for example MyActivity :</p> <pre><code>String s= "Notification"; NotificationTest notificationTest = new NotificationTest(); notificationTest.showNotification(s,MyActivity.this); </code></pre> <p>The error im getting is : </p> <pre><code>12-05 19:20:34.004 22352-22352/com.vindsiden.windwidget E/AndroidRuntime﹕ FATAL EXCEPTION: main Process: com.vindsiden.windwidget, PID: 22352 java.lang.IllegalStateException: System services not available to Activities before onCreate() at android.app.Activity.getSystemService(Activity.java:4531) at com.vindsiden.windwidget.NotificationTest.showNotification(NotificationTest.java:66) at com.vindsiden.windwidget.MyActivity$1.onClick(MyActivity.java:52) at android.view.View.performClick(View.java:4424) at android.view.View$PerformClick.run(View.java:18383) at android.os.Handler.handleCallback(Handler.java:733) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:137) at android.app.ActivityThread.main(ActivityThread.java:4998) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:777) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:593) </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload