为什么我们TaskStackBuilder在创建通知时使用?我没有得到它背后的逻辑.
有人可以解释一下.
public void showText(final String text){
Intent intent = new Intent (this, MainActivity.class);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
stackBuilder.addParentStack(MainActivity.class);
stackBuilder.addNextIntent(intent);
PendingIntent pendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
Notification notification = new Notification.Builder(this)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle(getString(R.string.app_name))
.setAutoCancel(true)
.setPriority(Notification.PRIORITY_MAX)
.setDefaults(Notification.DEFAULT_VIBRATE)
.setContentIntent(pendingIntent)
.setContentText(text)
.build();
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(NOTIFICACTION_ID, notification);
}
Run Code Online (Sandbox Code Playgroud) 我正在尝试在菜单栏上实现“搜索窗口小部件”。但是,我收到错误消息:“无法将null强制转换为非null类型的android.widget.SearchView”。但没有结果。
你能给我一些提示吗?
minSdkVersion 17 targetSdkVersion 26
override fun onCreateOptionsMenu(menu: Menu?): Boolean {
menuInflater.inflate(R.menu.menu, menu)
val searchView = menu!!.findItem(R.id.app_bar_search).actionView as SearchView
val searchManager = getSystemService(Context.SEARCH_SERVICE) as SearchManager
searchView.setSearchableInfo(searchManager.getSearchableInfo(componentName))
searchView.setOnQueryTextListener(object: SearchView.OnQueryTextListener{
override fun onQueryTextSubmit(p0: String?): Boolean {
return false
}
override fun onQueryTextChange(p0: String?): Boolean {
return false
}
})
Run Code Online (Sandbox Code Playgroud)
<item
android:id="@+id/app_bar_search"
android:actionViewClass="android.widget.SearchView"
android:icon="@drawable/ic_search_black_24dp"
android:title="Search"
app:showAsAction="always"/>Run Code Online (Sandbox Code Playgroud)