程序问答   发布时间:2022-06-02  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了从 android 11 小部件启动另一个应用程序?大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

如何解决从 android 11 小部件启动另一个应用程序??

开发过程中遇到从 android 11 小部件启动另一个应用程序?的问题如何解决?下面主要结合日常开发的经验,给出你关于从 android 11 小部件启动另一个应用程序?的解决方法建议,希望对你解决从 android 11 小部件启动另一个应用程序?有所启发或帮助;

我无法在 androID 11 上启动小部件。

此代码适用于我的 androID 9 手机, 但 androID 11 Google Pixel 3a 模拟器不工作。 我该怎么做。 好的,所以我想要做的是创建一个小部件,该小部件将在按下小部件时简单地启动另一个应用程序。

小部件 Xml

<linearLayout xmlns:androID="http://scheR_289_11845@as.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>

大佬总结

以上是大佬教程为你收集整理的从 android 11 小部件启动另一个应用程序?全部内容,希望文章能够帮你解决从 android 11 小部件启动另一个应用程序?所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。