Ok I kinda got stucked on the basics.I have a method in class that shows a notification in the notification bar. I tried to make it static but if I make it static,some functions won’t work.
So if I have the following function in x.class,how can I access it from y.class? Because I tried with static,and with objects,but all failed.
void notify(String i) {
String ns = Context.NOTIFICATION_SERVICE;
NotificationManager mNotificationManager = (NotificationManager) getSystemService(ns);
int icon = R.drawable.icon; // icon from resources
CharSequence tickerText = "gogu la telefon"; // ticker-text
long when = System.currentTimeMillis(); // notification time
Context context = getApplicationContext(); // application Context
CharSequence contentTitle = "My notification"; // message title
CharSequence contentText = "Hello World!"; // message text
Intent notificationIntent = new Intent(this, MilkyWaySearcherActivity.class);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
// the next two lines initialize the Notification, using the configurations above
Notification notification = new Notification(icon, tickerText, when);
notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);
notification.ledARGB = 0xff00ff00;
notification.ledOnMS = 300;
notification.ledOffMS = 1000;
notification.flags |= Notification.FLAG_SHOW_LIGHTS;
mNotificationManager.notify(BIND_AUTO_CREATE, notification);
}
You either need to create an instance, or make the method static.
Static methods cannot access instance methods unless there’s an instance.
Static methods cannot use the
thiskeyword, because there is no instance to refer to.In this case, it might be enough to pass in a replacement for where you use
this.