如何解决从 android 11 小部件启动另一个应用程序?
我无法在 android 11 上启动小部件。
此代码适用于我的 android 9 手机, 但 android 11 Google Pixel 3a 模拟器不工作。 我该怎么做。 好的,所以我想要做的是创建一个小部件,该小部件将在按下小部件时简单地启动另一个应用程序。
小部件 Xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:text="Whatsapp Launch"
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</Button>
<Button
android:layout_marginLeft="30dp"
android:text="Spotify Launch"
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</Button>
</LinearLayout>
小部件类
class SimpleAppWidget : 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)
}
}
private fun updateAppWidget(
context: Context,appWidgetId: Int
) {
// Construct the RemoteViews object
val views = RemoteViews(context.packageName,R.layout.simple_app_widget)
// Construct an Intent object includes web adresss.
val launchIntent12 = context!!.packageManager.getLaunchIntentForPackage("com.spotify.music")
val launchIntent122 = context!!.packageManager.getLaunchIntentForPackage("com.whatsapp")
// In widget we are not allowing to use intents as usually. We have to use PendingIntent instead of 'startActivity'
val pendingIntent = PendingIntent.getActivity(context,launchIntent122,0)
val pendingIntent2 = PendingIntent.getActivity(context,launchIntent12,0)
// Here the basic operations the remote view can do.
views.setOnClickPendingIntent(R.id.button,pendingIntent)
views.setOnClickPendingIntent(R.id.button2,pendingIntent2)
// Instruct the widget manager to update the widget
appWidgetManager.updateAppWidget(appWidgetId,views)
}
}
解决方法
首先,您需要添加 package visibility rules to your manifest。就目前而言,getLaunchIntentForPackage()
可能会返回 null
。解决此问题后,请完全卸载并重新安装该应用,然后再继续。
还有:
-
为您的
PendingIntent
对象使用唯一 ID(它们都在第二个参数中为0
设置为getActivity()
) -
考虑使用
PendingIntent.FLAG_UPDATE_CURRENT
作为getActivity()
的第四个参数
WİDGET 活动
class SimpleAppWidget : 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)
}
}
@SuppressLint("RemoteViewLayout")
private fun updateAppWidget(
context: Context,appWidgetId: Int
) {
// Construct the RemoteViews object
val views = RemoteViews(context.packageName,R.layout.simple_app_widget)
val launchIntent2 = context!!.packageManager.getLaunchIntentForPackage("com.spotify.music")
val pendingIntent2 = PendingIntent.getActivity(context,1,launchIntent2,PendingIntent.FLAG_UPDATE_CURRENT)
views.setOnClickPendingIntent(R.id.button2,pendingIntent2)
appWidgetManager.updateAppWidget(appWidgetId,views)
}
,
小部件权限
<queries>
<package android:name="com.spotify.music"/>
<package android:name="com.example.widget11" />
<intent>
<action android:name="com.spotify.music" />
</intent>
</queries>
<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/Theme.Widget11">
<receiver android:name=".SimpleAppWidget">
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
</intent-filter>
<meta-data
android:name="android.appwidget.provider"
android:resource="@xml/simple_app_widget_info" />
</receiver>
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。