developerxmod
developerxmod

Reputation: 21

Launch another app from android 11 widget?

I can't launch widgets on android 11.

This code works on my android 9 phone, but android 11 Google Pixel 3a emulator not working. what should I do for this. Ok, so what I want to do is create a widget that will simply launch another application when the widget is pressed.

Widget 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>

Widget Class

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, appWidgetManager: AppWidgetManager,
        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, 0, launchIntent122, 0)
        val pendingIntent2 = PendingIntent.getActivity(context, 0, 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)
    }

}

Upvotes: 2

Views: 876

Answers (3)

developerxmod
developerxmod

Reputation: 21

widget permissions

<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>

Upvotes: 0

developerxmod
developerxmod

Reputation: 21

WİDGET Activity

  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, appWidgetManager: AppWidgetManager,
    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)
}

Upvotes: 0

CommonsWare
CommonsWare

Reputation: 1006604

First, you will need to add package visibility rules to your manifest. As it stands, getLaunchIntentForPackage() is probably returning null. After fixing this, fully uninstall and reinstall the app before continuing.

Also:

  • Use unique IDs for your PendingIntent objects (they are both set for 0 in the second parameter to getActivity())

  • Consider using PendingIntent.FLAG_UPDATE_CURRENT for the fourth parameter to getActivity()

Upvotes: 1

Related Questions