Question

I'm trying to build a library which pops up persistent notification on the system tray whenever included in the application project. I have made a normal application and tested it on device which is running perfectly fine. Now, I have converted that project into library and referencing that library to android project.

Here is my MainActivity which is nameed as SearchBar of the library project:

public class SearchBar extends Activity {

NotificationManager nm;
public static String PACKAGE_NAME;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.search_bar);

    PACKAGE_NAME = getApplicationContext().getPackageName();
}

public void searchBar() {

    Notification notification = new Notification(R.drawable.ic_stat_notify,
            "STest", System.currentTimeMillis());

    RemoteViews contentView = new RemoteViews(PACKAGE_NAME,
            R.layout.persistent_notification_layout);

    notification.contentView = contentView;

    Intent notificationIntent = new Intent();
    notificationIntent.setClassName("com.example.searchbar",
            "com.example.searchbar.SearchAutoSuggest");

    notificationIntent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP
            | Intent.FLAG_ACTIVITY_CLEAR_TOP);

    nm = (NotificationManager) getSystemService(SearchBar.NOTIFICATION_SERVICE);

    notification.flags |= Notification.FLAG_NO_CLEAR;
    notification.flags |= Notification.FLAG_ONGOING_EVENT;

    PendingIntent contentIntent = PendingIntent.getActivity(this, 2,
            notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);

    notification.contentIntent = contentIntent;

    nm.notify(AppSingleton.NOTIFICATION_ID_ALWAYS, notification);
}

}

And, I'm calling this library into my testProject's MainActivity like this:

public class MainActivity extends Activity {

SearchBar notification;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    notification = new SearchBar();

    notification.searchBar();

}
}

Now, when I run my testProject I get error stating as:

 Caused by: java.lang.IllegalStateException: System services not available to Activities before onCreate()
at android.app.Activity.getSystemService(Activity.java:4463)
at com.example.searchbar.SearchBar.searchBar(SearchBar.java:43)
at com.example.libtest.MainActivity.onCreate(MainActivity.java:19)

On:

nm = (NotificationManager) getSystemService(SearchBar.NOTIFICATION_SERVICE);

I tried/searched several stuff, but non of them is helping me to resolve the problem. What am I doing wrong? Any kind of help will be appreciated.

Was it helpful?

Solution

IllegalStateException: System services not available to Activities before onCreate()

because you are extending Activity in SearchBar class but not registering in AndroidManifest.xml.

so create SearchBar class as normal java class without extending Activity and pass MainActivity context to SearchBar class using class constructor or as method parameters as:

public class SearchBar {

NotificationManager nm;
public static String PACKAGE_NAME;
Context context;
 public  SearchBar(Context context) {
    this.context=context;
    PACKAGE_NAME = context.getPackageName();
}
....

OTHER TIPS

Activities are not meant to be instantiated yourself via the new keyword. The Android OS is supposed to do that. Here's what I would change:

  1. Don't have SearchBar extend Activity.
  2. Give SearchBar a public method (could even be a static method) that takes a Context as a parameter. Use that to obtain the NotificationManager instance.

Like so

public class SearchBar {
    private SearchBar() {/* no instantiation */}

    public static void notifyPersistently(Context context) {
        NotificationManager nm = (NotificationManager)context.getSystemService(SearchBar.NOTIFICATION_SERVICE);
        // all the rest ...
    }
}

agree with @Karakuri aplly his advice and this code try the following code

nm = (NotificationManager) context.getSystemService(SearchBar.NOTIFICATION_SERVICE);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top