我正在尝试构建一个库,只要包含在应用程序项目中,它就会在系统托盘上弹出persistent notification
。我做了一个正常的应用程序,并在运行完美的设备上测试它。现在,我已将该项目转换为库并将该库引用到android项目。
以下是我的MainActivity
,它被命名为图书馆项目的SearchBar
:
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);
}
}
而且,我正在将此库称为testProject's
MainActivity
,如下所示:
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();
}
}
现在,当我运行testProject
时,我收到的错误是:
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)
在:
nm = (NotificationManager) getSystemService(SearchBar.NOTIFICATION_SERVICE);
我尝试/搜索了几个东西,但是没有他们帮助我解决问题。 我究竟做错了什么?任何形式的帮助将不胜感激。
答案 0 :(得分:0)
同意@Karakuri aplly他的建议和这段代码 尝试以下代码
nm = (NotificationManager) context.getSystemService(SearchBar.NOTIFICATION_SERVICE);
答案 1 :(得分:0)
活动并不是要通过new
关键字自行实例化。 Android操作系统应该这样做。以下是我要改变的内容:
Activity
。public
方法(甚至可以是static
方法),将Context
作为参数。用它来获取NotificationManager
实例。喜欢这样
public class SearchBar {
private SearchBar() {/* no instantiation */}
public static void notifyPersistently(Context context) {
NotificationManager nm = (NotificationManager)context.getSystemService(SearchBar.NOTIFICATION_SERVICE);
// all the rest ...
}
}
答案 2 :(得分:0)
IllegalStateException:活动无法使用的系统服务 在onCreate()
之前
因为您正在SearchBar
课程中扩展活动但未在AndroidManifest.xml
中注册。
所以创建SearchBar
类作为普通的java类而不扩展Activity并使用类构造函数将MainActivity上下文传递给SearchBar类,或者作为方法参数传递:
public class SearchBar {
NotificationManager nm;
public static String PACKAGE_NAME;
Context context;
public SearchBar(Context context) {
this.context=context;
PACKAGE_NAME = context.getPackageName();
}
....