0

In my simple example app i have 2 activity:

  1. MainActivity.java
  2. NewAppWidgetConfigureActivity.java

Also in my app i have class NewAppWidget.java who extends AppWidgetProvider

What i want to accomplish is this:

When i add home widget to screen, i want to open activity NewAppWidgetConfigureActivity.java first. It is ok till now, its working nice.

When i add home widget to screen, my widget has 2 buttons and one TextView. What i want is this:

  1. when i click first button, i want to open MainActivity.java
  2. when i click second button, i want to open NewAppWidgetConfigureActivity.java.

However, its not working. By clicking buttons nothing happens. I try to implement solutions from other questions but i it does not working.

Here are codes:

Manifest file code:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.petar.mywidgetwitbuttons">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <receiver android:name=".NewAppWidget">
            <intent-filter>
                <action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
            </intent-filter>

            <meta-data
                android:name="android.appwidget.provider"
                android:resource="@xml/new_app_widget_info" />
        </receiver>

        <activity android:name=".NewAppWidgetConfigureActivity">
            <intent-filter>
                <action android:name="android.appwidget.action.APPWIDGET_CONFIGURE" />
            </intent-filter>
        </activity>
    </application>

This is code for new_ap_widget_info.xml, where i add all necessary info for my widget, including line of code where i automatically open activity NewAppWidgetConfigureActivity.java when i add widget.

<?xml version="1.0" encoding="utf-8"?>
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
    android:configure="com.example.petar.mywidgetwitbuttons.NewAppWidgetConfigureActivity"
    android:initialKeyguardLayout="@layout/new_app_widget"
    android:initialLayout="@layout/new_app_widget"
    android:minHeight="110dp"
    android:minWidth="180dp"
    android:previewImage="@drawable/example_appwidget_preview"
    android:resizeMode="horizontal|vertical"
    android:updatePeriodMillis="86400000"
    android:widgetCategory="home_screen" />

This is code for NewAppWidget.java class who extends AppWidgetProvider

package com.example.petar.mywidgetwitbuttons;

import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.widget.RemoteViews;

/**
 * Implementation of App Widget functionality.
 * App Widget Configuration implemented in {@link NewAppWidgetConfigureActivity NewAppWidgetConfigureActivity}
 */
public class NewAppWidget extends AppWidgetProvider {

    static void updateAppWidget(Context context, AppWidgetManager appWidgetManager,
                                int appWidgetId) {

        CharSequence widgetText = NewAppWidgetConfigureActivity.loadTitlePref(context, appWidgetId);
        // Construct the RemoteViews object
        RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.new_app_widget);
        views.setTextViewText(R.id.appwidget_text, widgetText);

        Log.d("widget", "onUpdatePrvo: Widget testiramo");

        // Instruct the widget manager to update the widget
        appWidgetManager.updateAppWidget(appWidgetId, views);
    }

    @Override
    public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
        // There may be multiple widgets active, so update all of them
        for (int appWidgetId : appWidgetIds) {
            updateAppWidget(context, appWidgetManager, appWidgetId);

            Intent intent = new Intent(context, MainActivity.class);
            Intent intent2 = new Intent(context, NewAppWidgetConfigureActivity.class);
            PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);
            PendingIntent pendingIntent2 = PendingIntent.getActivity(context, 0, intent2, 0);


            // Get the layout for the App Widget and attach an on-click listener
            // to the button
            RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.new_app_widget);
            views.setOnClickPendingIntent(R.id.buttonCLICK, pendingIntent);
            views.setOnClickPendingIntent(R.id.buttonCLICK2, pendingIntent2);


            appWidgetManager.updateAppWidget(appWidgetId, views);
        }
    }

    @Override
    public void onDeleted(Context context, int[] appWidgetIds) {
        // When the user deletes the widget, delete the preference associated with it.
        for (int appWidgetId : appWidgetIds) {
            NewAppWidgetConfigureActivity.deleteTitlePref(context, appWidgetId);
        }
    }

    @Override
    public void onEnabled(Context context) {
        // Enter relevant functionality for when the first widget is created
    }

    @Override
    public void onDisabled(Context context) {
        // Enter relevant functionality for when the last widget is disabled
    }
}

Also, when i remove this line of code from provider info, then i can open MainActivity.java on the first button click, and still can not open NewAppWidgetConfigureActivity.java by clicking second button. android:configure="com.example.petar.mywidgetwitbuttons.NewAppWidgetConfigureActivity"

And here is full provider info code without android:configure=""

<?xml version="1.0" encoding="utf-8"?>
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
    android:configure="com.example.petar.mywidgetwitbuttons.NewAppWidgetConfigureActivity"
    android:initialKeyguardLayout="@layout/new_app_widget"
    android:initialLayout="@layout/new_app_widget"
    android:minHeight="110dp"
    android:minWidth="180dp"
    android:previewImage="@drawable/example_appwidget_preview"
    android:resizeMode="horizontal|vertical"
    android:updatePeriodMillis="86400000"
    android:widgetCategory="home_screen" />
Cœur
  • 37,241
  • 25
  • 195
  • 267
NoName
  • 273
  • 7
  • 21

2 Answers2

0

These easy steps could help you to resolve your problem.

  1. App widget allows certain UI controls not everything. So check what are allowed and what are not. https://developer.android.com/guide/topics/appwidgets/index.html#CreatingLayout

  2. Do not extend or create a custom UI control class. Ex;- class CustomButton extends Button

  3. I do see your Intents do not have extra parameters. That might confuse the PendingIntent compare logic internally. So add a custom Action or few extra parameters to the intent. EX;-

    Intent a = new Intent("MainActivity"); Intent b = new Intent("ConfigureActivity");

albeee
  • 1,452
  • 1
  • 12
  • 20
  • As i see, everything i use is allowed. All i use in my home widget UI is 2 buttons and Relative layout. I use2 differnet intents like this : Intent intent = new Intent(context, MainActivity.class); Intent intent2 = new Intent(context, NewAppWidgetConfigureActivity.class); in onUpdate() method. Is that correct way? When i remove from provider info android:configure="", then i can actually open MainActivity by clicking first button but i can not open second activity by clicking second button. – NoName Mar 02 '17 at 14:14
  • Did you try setting the different intent action as i said above? Intent a = new Intent("MainActivity-This is the intent action for activity1"); Intent b = new Intent("ConfigureActivity-This is the intent action for activity2"); – albeee Mar 02 '17 at 14:45
  • I try to add this 2 lines of code and still do not work. intent.setAction("MainActivity"); intent2.setAction("NewAppWidgetConfigureActivity"); – NoName Mar 02 '17 at 17:36
0

This works for me.

      public class widget_wordss extends AppWidgetProvider {

private static final String ACTION_SIMPLEAPPWIDGET = "ACTION_BROADCASTWIDGETSAMPLE";
private static final String ACTION_SIMPLEAPPWIDGET1 = "ACTION_BROADCASTWIDGETSAMPLE1";

public boolean widg = false;

static void updateAppWidget(Context context, AppWidgetManager appWidgetManager, int 
appWidgetId) {

    RemoteViews views = new RemoteViews(context.getPackageName(), 
R.layout.widget_main_words);
    Intent intent = new Intent(context, widget_wordss.class);
    Intent intent1 = new Intent(context, widget_wordss.class);
    intent.setAction(ACTION_SIMPLEAPPWIDGET);
    intent1.setAction(ACTION_SIMPLEAPPWIDGET1);
    PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intent, 
PendingIntent.FLAG_UPDATE_CURRENT);
    PendingIntent pendingIntent1 = PendingIntent.getBroadcast(context, 0, intent1, 
PendingIntent.FLAG_UPDATE_CURRENT);
    views.setOnClickPendingIntent(R.id.btnOpen, pendingIntent);
    views.setOnClickPendingIntent(R.id.btnayar, pendingIntent1);
    appWidgetManager.updateAppWidget(appWidgetId, views);
biyoalp
  • 33
  • 7