Я добавляю функциональность виджетов в свое приложение, но всякий раз, когда я пытаюсь добавить виджет на домашний экран, он открывает действия по настройке.
после установки виджета из действия конфигурации закрывается, но виджет не добавляется на домашний экран.
здесь мой код:
AlwaysAlive. xml (AppWidgetProvider класс)
package com.autostamper.datetimestampphoto;
import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.util.Log;
import android.widget.RemoteViews;
import android.widget.Toast;
import com.autostamper.datetimestampphoto.nativehandle.D;
import com.autostamper.datetimestampphoto.nativehandle.L;
import com.autostamper.datetimestampphoto.nativehandle.Q;
import com.autostamper.datetimestampphoto.nativehandle.S;
import com.autostamper.datetimestampphoto.nativehandle.T;
import com.autostamper.datetimestampphoto.nativehandle.W;
import com.autostamper.datetimestampphoto.nativehandle.X;
import com.autostamper.datetimestampphoto.services.ForegroundService;
import com.autostamper.datetimestampphoto.utilitis.HelperClass;
/**
* Implementation of App Widget functionality.
*/
public class AlwaysAlive extends AppWidgetProvider {
static {
System.loadLibrary("Native");
}
HelperClass helperClass = new HelperClass();
RemoteViews remoteViews;
public static void updateAppWidget(Context context, AppWidgetManager appWidgetManager,
int appWidgetId) {
RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.always_alive);
Intent intent = new Intent(context, AlwaysAlive.class);
intent.setAction("ACTION_WIDGET_UPDATE_FROM_WIDGET");
if (T.D() || T.L() || T.S() || T.W() || T.SE()) {
remoteViews.setImageViewResource(R.id.app_widget_imageView, R.drawable.color_icon);
remoteViews.setTextViewText(R.id.app_widget_textView, "ON");
remoteViews.setTextColor(R.id.app_widget_textView, context.getResources().getColor(R.color.green));
Intent intentImageStampService = new Intent(context, ForegroundService.class);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
context.startForegroundService(intentImageStampService);
} else {
context.startService(intentImageStampService);
}
}
remoteViews.setOnClickPendingIntent(R.id.app_widget_imageView, PendingIntent.getBroadcast(context, 0, intent, 0));
appWidgetManager.updateAppWidget(appWidgetId, remoteViews);
}
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
for (int appWidgetId : appWidgetIds) {
updateAppWidget(context, appWidgetManager, appWidgetId);
}
}
@Override
public void onEnabled(Context context) {
super.onEnabled(context);
Log.e("CORONA", "onEnabled");
}
@Override
public void onDisabled(Context context) {
super.onDisabled(context);
Log.e("CORONA", "onDisabled");
}
@Override
public void onReceive(Context context, Intent intent) {
super.onReceive(context, intent);
Log.e("CORONA", "onReceive");
try {
if (intent.getAction().equals("ACTION_WIDGET_UPDATE_FROM_WIDGET")) {
Toast.makeText(context, "onReceive", Toast.LENGTH_SHORT).show();
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.always_alive);
AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
if (!helperClass.isMyServiceRunning(ForegroundService.class, context)) {
views.setImageViewResource(R.id.app_widget_imageView, R.drawable.color_icon);
views.setTextViewText(R.id.app_widget_textView, "ON");
views.setTextColor(R.id.app_widget_textView, context.getResources().getColor(R.color.green));
Intent intentImageStampService = new Intent(context, ForegroundService.class);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
context.startForegroundService(intentImageStampService);
} else {
context.startService(intentImageStampService);
}
appWidgetManager.updateAppWidget(new ComponentName(context,
AlwaysAlive.class), views);
D.T(X.A());
L.T(X.B());
S.T(X.C());
W.T(X.D());
S.SET(X.SE());
if (!T.D() && !T.L() && !T.S() && !T.W() && !T.SE()) {
D.T(true);
Q.A(true);
}
} else {
Log.e("Else_SPEEEEEEE", "Else_SPEEEEE333333333333333");
Q.A(T.D());
Q.B(T.L());
Q.C(T.S());
Q.D(T.W());
Q.SE(T.SE());
D.T(false);
L.T(false);
S.T(false);
W.T(false);
S.SET(false);
views.setImageViewResource(R.id.app_widget_imageView, R.drawable.greyicon);
views.setTextViewText(R.id.app_widget_textView, "OFF");
views.setTextColor(R.id.app_widget_textView, context.getResources().getColor(R.color.wOff));
appWidgetManager.updateAppWidget(new ComponentName(context, AlwaysAlive.class), views);
Intent intentImageStampService = new Intent(context, ForegroundService.class);
context.stopService(intentImageStampService);
}
} else if (intent.getAction().equals("ACTION_WIDGET_UPDATE_FROM_WIDGET_SP")) {
Log.e("Else_SP", "Else_SP");
}
} catch (Exception e) {
}
}
public void uFromSOFF(Context context) {
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.always_alive);
AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
views.setImageViewResource(R.id.app_widget_imageView, R.drawable.greyicon);
views.setTextViewText(R.id.app_widget_textView, "OFF");
views.setTextColor(R.id.app_widget_textView, context.getResources().getColor(R.color.wOff));
appWidgetManager.updateAppWidget(new ComponentName(context,
AlwaysAlive.class), views);
if (helperClass.isMyServiceRunning(ForegroundService.class, context)) {
Intent intentImageStampService = new Intent(context, ForegroundService.class);
context.stopService(intentImageStampService);
}
}
public void uFromSON(Context context) {
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.always_alive);
AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
views.setImageViewResource(R.id.app_widget_imageView, R.drawable.color_icon);
views.setTextViewText(R.id.app_widget_textView, "ON");
views.setTextColor(R.id.app_widget_textView, context.getResources().getColor(R.color.green));
appWidgetManager.updateAppWidget(new ComponentName(context,
AlwaysAlive.class), views);
if (!helperClass.isMyServiceRunning(ForegroundService.class, context)) {
Intent intentImageStampService = new Intent(context, ForegroundService.class);
context.startService(intentImageStampService);
}
}
}
always_alive_info. xml (appwidget-провайдера)
<?xml version="1.0" encoding="utf-8"?>
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
android:configure="com.autostamper.datetimestampphoto.activity.InAppBillingActivity"
android:initialKeyguardLayout="@layout/always_alive"
android:initialLayout="@layout/always_alive"
android:minWidth="40dp"
android:minHeight="40dp"
android:previewImage="@drawable/color_icon"
android:resizeMode="horizontal|vertical"
android:updatePeriodMillis="86400000"
android:widgetCategory="home_screen|keyguard"></appwidget-provider>
InAppBillingActivity. java (операция настройки )
@Override
protected void onResume() {
super.onResume();
if (J.O() != 0) {
Context context = InAppBillingActivity.this;
AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
AlwaysAlive.updateAppWidget(context, appWidgetManager, mAppWidgetId);
}
}