Виджет не отображается
При добавление виджет на мгновение появляется и сразу пропадает. Раньше у меня все работало, но после переустановки приложения виджет перестал работать. Я откатил на старую версию, но все равно не работает. Я поудалял весь код, который как-то может помешать работать виджет, но безуспешно.
Код виджета:
package ---.----.-------.---;
import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.widget.RemoteViews;
/**
* Implementation of App Widget functionality.
* App Widget Configuration implemented in {@link wdgetConfigureActivity wdgetConfigureActivity}
*/
public class wdget extends AppWidgetProvider {
static void updateAppWidget(Context context, AppWidgetManager appWidgetManager,
int appWidgetId) {
CharSequence widgetText = wdgetConfigureActivity.loadTitlePref(context, appWidgetId);
// Construct the RemoteViews object
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.wdget);
views.setTextViewText(R.id.appwidget_text, widgetText);
// Instruct the widget manager to update the widget
appWidgetManager.updateAppWidget(appWidgetId, views);
// Create an Intent to launch MainActivity
Intent intent = new Intent(context, wdgetConfigureActivity.class);
intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
// This is needed to make this intent different from its previous intents
intent.setData(Uri.parse("tel:/"+ (int)System.currentTimeMillis()));
// Creating a pending intent, which will be invoked when the user
// clicks on the widget
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0,
intent,PendingIntent.FLAG_UPDATE_CURRENT);
// Attach an on-click listener to the clock
views.setOnClickPendingIntent(R.id.appwidget_text, pendingIntent);
// Tell the AppWidgetManager to perform an update on the current app 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);
}
}
@Override
public void onDeleted(Context context, int[] appWidgetIds) {
// When the user deletes the widget, delete the preference associated with it.
for (int appWidgetId : appWidgetIds) {
wdgetConfigureActivity.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
}
}
Конфигуратор виджета:
package ---.-----.----.---.--;
import android.app.Activity;
import android.appwidget.AppWidgetManager;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import java.util.Random;
/**
* The configuration screen for the {@link wdget wdget} AppWidget.
*/
public class wdgetConfigureActivity extends Activity {
private static final String PREFS_NAME = "duck.duck.go.search.wdget";
private static final String PREF_PREFIX_KEY = "appwidget_";
int mAppWidgetId = AppWidgetManager.INVALID_APPWIDGET_ID;
public wdgetConfigureActivity() {
super();
}
static void saveTitlePref(Context context, int appWidgetId, String text) {
SharedPreferences.Editor prefs = context.getSharedPreferences(PREFS_NAME, 0).edit();
prefs.putString(PREF_PREFIX_KEY + appWidgetId, text);
prefs.apply();
}
static String loadTitlePref(Context context, int appWidgetId) {
SharedPreferences prefs = context.getSharedPreferences(PREFS_NAME, 0);
String titleValue = prefs.getString(PREF_PREFIX_KEY + appWidgetId, null);
if (titleValue != null) {
return titleValue;
} else {
return context.getString(R.string.appwidget_text);
}
}
static void deleteTitlePref(Context context, int appWidgetId) {
SharedPreferences.Editor prefs = context.getSharedPreferences(PREFS_NAME, 0).edit();
prefs.remove(PREF_PREFIX_KEY + appWidgetId);
prefs.apply();
}
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.wdget_configure);
setResult(RESULT_CANCELED);
final Context context = wdgetConfigureActivity.this;
AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
ComponentName thisAppWidget = new ComponentName(context, wdget.class);
int[] appWidgetIds = appWidgetManager.getAppWidgetIds(thisAppWidget);
String widgetText = String.valueOf(new Random().nextInt(99999999));
for (int mAppWidgetId: appWidgetIds) {
saveTitlePref(context, mAppWidgetId, widgetText);
wdget.updateAppWidget(context, appWidgetManager, mAppWidgetId);
}
Intent resultValue = new Intent();
resultValue.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, mAppWidgetId);
setResult(RESULT_OK, resultValue);
finish();
}
}
Виджет:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:theme="@style/ThemeOverlay.Glance.AppWidgetContainer">
<Button
android:id="@+id/appwidget_text"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerInParent="true"
android:background="#8600FF00"
android:contentDescription="@string/appwidget_text"
android:text="@string/appwidget_text"
android:textColor="#FFFFFF"
android:textSize="24sp"
android:textStyle="bold|italic" />
</RelativeLayout>