我在SO中看过很多关于此的帖子,但无法从服务类调用一个活动方法的确切和最简单的方法.广播接收器只是选项吗?没有捷径 ?我只需要在Service类中准备好媒体播放器后在Activity类中调用以下方法.
活动类:
public void updateProgress() {
// set Progress bar values
songProgressBar.setProgress(0);
songProgressBar.setMax(100);
// Updating progress bar
updateProgressBar();
}
Run Code Online (Sandbox Code Playgroud)
服务类:
@Override
public IBinder onBind(Intent intent) {
Log.d(this.getClass().getName(), "BIND");
return musicBind;
}
@Override
public boolean onUnbind(Intent intent) {
return false;
}
@Override
public void onPrepared(MediaPlayer mp) {
try {
mp.start();
} catch (IllegalStateException e) {
e.printStackTrace();
}
// updateProgress();// Need to call the Activity method here
}
Run Code Online (Sandbox Code Playgroud) 在 中Android 11,当targetSdk设置为30并启用语音搜索时,麦克风图标不会显示在 上SearchView。但是,如果targetSdk设置为 29 ,它可以正常工作。它也适用于Android 10带有targetSdk 30.
有什么额外的事情需要做30吗?
菜单文件
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/action_search"
android:title="Search"
app:actionViewClass="androidx.appcompat.widget.SearchView"
app:showAsAction="always" />
</menu>
Run Code Online (Sandbox Code Playgroud)
清单文件
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<meta-data
android:name="android.app.searchable"
android:resource="@xml/searchable" />
<intent-filter>
<action android:name="android.intent.action.SEARCH" />
</intent-filter>
</activity>
Run Code Online (Sandbox Code Playgroud)
可搜索文件
<searchable xmlns:android="http://schemas.android.com/apk/res/android"
android:label="@string/app_name"
android:hint="Search"
android:imeOptions="actionSearch"
android:voiceSearchMode="showVoiceSearchButton|launchRecognizer"/>
Run Code Online (Sandbox Code Playgroud)
主要活动
override fun onCreateOptionsMenu(menu: Menu): Boolean {
menuInflater.inflate(R.menu.search, menu)
val searchManager = getSystemService(Context.SEARCH_SERVICE) as SearchManager
val …Run Code Online (Sandbox Code Playgroud)