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.