Я пытаюсь отправить намерение обновить виджет главного экрана. Я не могу успешно отправить намерение и получить ошибку
android.content.ActivityNotFoundException: Unable to find explicit activity class {com.stream.homewidgets/com.stream.homewidgets.Parking}; have you declared this activity in your AndroidManifest.xml?
, которая исходит из строки startActivity(intent)
из моего фрагмента.
Я просмотрел несколько сообщений переполнения стека, как показано ниже:
Они не вполне смоглиРешите проблему, мой код выглядит очень похожим.
Вот фрагмент из моего виджета:
class Parking : AppWidgetProvider() {
override fun onUpdate(
context: Context,
appWidgetManager: AppWidgetManager,
appWidgetIds: IntArray
) {
// There may be multiple widgets active, so update all of them
for (appWidgetId in appWidgetIds) {
updateAppWidget(context, appWidgetManager, appWidgetId, currentFloor)
val intent = Intent(context, Parking::class.java)
intent.action = AppWidgetManager.ACTION_APPWIDGET_UPDATE
intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, appWidgetIds)
PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT)
}
}
...
}
Вот фрагмент из вызывающего фрагмента:
class HomeFragment : Fragment() {
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
...
spinner.onItemSelectedListener = object : AdapterView.OnItemSelectedListener{
override fun onNothingSelected(parent: AdapterView<*>?) {}
override fun onItemSelected(
parent: AdapterView<*>?,
view: View?,
position: Int,
id: Long
) {
context?.let {
val componentName = ComponentName(it, Parking::class.java)
val intent = Intent(context, Parking::class.java)
intent.action = AppWidgetManager.ACTION_APPWIDGET_UPDATE
val ids = AppWidgetManager.getInstance(it).getAppWidgetIds(componentName)
intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, ids)
startActivity(intent)
}
}
}
return root
}
}
Вот мой AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.stream.homewidgets">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<receiver android:name=".Parking">
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
</intent-filter>
<meta-data
android:name="android.appwidget.provider"
android:resource="@xml/parking_info" />
</receiver>
<activity
android:name=".MainActivity"
android:label="@string/app_name"
android:theme="@style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Так что я хотел бы иметь возможность отправить намерение обновить мой виджет. Я не уверен, как решить эту проблему. Мой ".Parking" получатель находится в манифесте, хотя это получатель, а не активность. Я не знаю, есть ли лучший способ передать намерение. Если вам известен более правильный способ отправки намерения виджету, сообщите мне.