کار نکردن آلارم ویجت
لطفا تست کنید و نظرتونو بگید .نمیدونم آلارمش کار میکنه یا نه؟ اگر مشکل دیگه ای هم داشت خوشحال میشم راهنمایی کنید.
کد اینه درست کار میکنه تا چند ساعت ولی بعد متوقف میشه کسی میتونه مشکلو پیدا کنه؟
import java.util.Calendar;
import android.app.AlarmManager;
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.view.View;
import android.widget.RemoteViews;
import android.widget.Toast;
public class WidgetProvider extends AppWidgetProvider {
// our actions for our buttons
public static String REFRESH = "refresh";
public static String SHOMARESH = "shomaresh";
public static String ZEKR_SHOMAR = "ZEKR_SHOMAR";
public static String ZEKR_ROOZ = "ZEKR_ROOZ";
public static String ACTION_UPDATE_WIDGET = "ACTION_UPDATE_WIDGET";
static RemoteViews remoteViews;
static PendingIntent zekrshomarPendingIntent;
static PendingIntent zekrroozPendingIntent;
static PendingIntent refreshPendingIntent;
static PendingIntent shomareshPendingIntent;
static int mCount = 0;
@Override
public void onEnabled(Context context) {
Intent myIntent = new Intent(ACTION_UPDATE_WIDGET);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 2, myIntent, PendingIntent.FLAG_UPDATE_CURRENT);
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, 0);
calendar.set(Calendar.MINUTE, 1);
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.AM_PM, Calendar.AM);
Toast.makeText(context, R.string.well, Toast.LENGTH_LONG).show();
AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
alarmManager.setInexactRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 86400000, pendingIntent);
remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget);
getImageToSet();
pushWidgetUpdate(context, remoteViews);
super.onEnabled(context);
}
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
Intent myIntent = new Intent(ACTION_UPDATE_WIDGET);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 2, myIntent, PendingIntent.FLAG_UPDATE_CURRENT);
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, 0);
calendar.set(Calendar.MINUTE, 1);
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.AM_PM, Calendar.AM);
AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
alarmManager.setInexactRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 86400000, pendingIntent);
remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget);
Intent active = new Intent(context, WidgetProvider.class);
active.setAction(REFRESH);
refreshPendingIntent = PendingIntent.getBroadcast(context, 0, active, PendingIntent.FLAG_UPDATE_CURRENT);
remoteViews.setOnClickPendingIntent(R.id.btnRefresh, refreshPendingIntent);
Intent active1 = new Intent(context, WidgetProvider.class);
active1.setAction(SHOMARESH);
shomareshPendingIntent = PendingIntent.getBroadcast(context, 1, active1, PendingIntent.FLAG_UPDATE_CURRENT);
remoteViews.setOnClickPendingIntent(R.id.btnZekrShomar, shomareshPendingIntent);
Intent active2 = new Intent(context, WidgetProvider.class);
active2.setAction(ZEKR_SHOMAR);
zekrshomarPendingIntent = PendingIntent.getBroadcast(context, 3, active2, PendingIntent.FLAG_UPDATE_CURRENT);
remoteViews.setOnClickPendingIntent(R.id.btnZekr, zekrshomarPendingIntent);
Intent active3 = new Intent(context, WidgetProvider.class);
active3.setAction(ZEKR_ROOZ);
zekrroozPendingIntent = PendingIntent.getBroadcast(context, 4, active3, PendingIntent.FLAG_UPDATE_CURRENT);
remoteViews.setOnClickPendingIntent(R.id.btnRooz, zekrroozPendingIntent);
mCount = 0;
getImageToSet();
appWidgetManager.updateAppWidget(appWidgetIds, remoteViews);
}
private static String getMessage() {
mCount++;
return String.valueOf(mCount);
}
public static void pushWidgetUpdate(Context context, RemoteViews remoteViews) {
ComponentName myWidget = new ComponentName(context, WidgetProvider.class);
AppWidgetManager manager = AppWidgetManager.getInstance(context);
manager.updateAppWidget(myWidget, remoteViews);
}
public static void zekrRoozListener(Context context) {
RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget);
remoteViews.setViewVisibility(R.id.imgZekr, View.VISIBLE);
remoteViews.setViewVisibility(R.id.btnZekr, View.VISIBLE);
remoteViews.setViewVisibility(R.id.btnRooz, View.INVISIBLE);
remoteViews.setViewVisibility(R.id.rlshomaresh, View.INVISIBLE);
getImageToSet();
//REMEMBER TO ALWAYS REFRESH YOUR BUTTON CLICK LISTENERS!!!
remoteViews.setOnClickPendingIntent(R.id.btnRooz, zekrroozPendingIntent);
pushWidgetUpdate(context, remoteViews);
}
public static void zekrShomarListener(Context context) {
RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget);
remoteViews.setViewVisibility(R.id.imgZekr, View.INVISIBLE);
remoteViews.setViewVisibility(R.id.btnZekr, View.GONE);
remoteViews.setViewVisibility(R.id.rlshomaresh, View.VISIBLE);
remoteViews.setViewVisibility(R.id.btnRooz, View.VISIBLE);
// remoteViews.setTextViewText(R.id.btnZekr, "zekrerooz");
mCount = 0;
remoteViews.setTextViewText(R.id.txtCount, "0");
getImageToSet();
//REMEMBER TO ALWAYS REFRESH YOUR BUTTON CLICK LISTENERS!!!
remoteViews.setOnClickPendingIntent(R.id.btnZekr, zekrshomarPendingIntent);
pushWidgetUpdate(context, remoteViews);
}
public static void widgetRefreshListener(Context context) {
remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget);
mCount = 0;
remoteViews.setTextViewText(R.id.txtCount, "0");
//REMEMBER TO ALWAYS REFRESH YOUR BUTTON CLICK LISTENERS!!!
remoteViews.setOnClickPendingIntent(R.id.btnRefresh, refreshPendingIntent);
pushWidgetUpdate(context, remoteViews);
}
public static void updateWidgetPictureAndButtonListener(Context context) {
remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget);
String message = getMessage();
getImageToSet();
remoteViews.setTextViewText(R.id.txtCount, message);
//REMEMBER TO ALWAYS REFRESH YOUR BUTTON CLICK LISTENERS!!!
remoteViews.setOnClickPendingIntent(R.id.btnZekrShomar, shomareshPendingIntent);
pushWidgetUpdate(context, remoteViews);
}
public static void getImageToSet() {
Calendar calendar = Calendar.getInstance();
int getrooz = calendar.get(Calendar.DAY_OF_WEEK);
if (getrooz == 1) {
remoteViews.setImageViewResource(R.id.imgZekr, R.drawable.imag2);
}
if (getrooz == 2) {
remoteViews.setImageViewResource(R.id.imgZekr, R.drawable.imag3);
}
if (getrooz == 3) {
remoteViews.setImageViewResource(R.id.imgZekr, R.drawable.imag4);
}
if (getrooz == 4) {
remoteViews.setImageViewResource(R.id.imgZekr, R.drawable.imag5);
}
if (getrooz == 5) {
remoteViews.setImageViewResource(R.id.imgZekr, R.drawable.imag6);
}
if (getrooz == 6) {
remoteViews.setImageViewResource(R.id.imgZekr, R.drawable.imag7);
}
if (getrooz == 7) {
remoteViews.setImageViewResource(R.id.imgZekr, R.drawable.imag1);
}
}
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals("shomaresh")) {
updateWidgetPictureAndButtonListener(context);
}
else if (intent.getAction().equals("refresh")) {
widgetRefreshListener(context);
}
else if (intent.getAction().equals("ZEKR_SHOMAR")) {
zekrShomarListener(context);
}
else if (intent.getAction().equals("ZEKR_ROOZ")) {
zekrRoozListener(context);
}
else if (intent.getAction().equals("ACTION_UPDATE_WIDGET")) {
updateWidgetPictureAndButtonListener(context);
} else {
super.onReceive(context, intent);
}
}
}
این هم توی فایل xml زدم ممکنه از اینجا باشه؟
android:updatePeriodMillis="86400000"
سرچ کردم راه حلو این داده بود کسی میتونه شفاف برام توضیح بده؟
The problem is that you can't do a partiall update for a widget, you must set all the widget features, such as the set of PendingIntent's every time you push a new remoteView. (Partiall updates are only available for API14 and up...).
The reason your widgets are loosing their pendingIntents is that the android system saves the remoteView, and rebuilds your widget with it, in case it resets the widget (shortage of memmory, TaskManager/taskKiller in use, etc...), so you must set all the update code for the widget in the remoteView in your updateService. Otherwise, it's just won't set the pendingIntents again.
So just add the code setting the pendingIntents to the service and your problem will be solved =]
پاسخگویی و مشاهده پاسخ های این سوال تنها برای اعضای ویژه سایت امکان پذیر است .
چنانچه تمایل دارید به همه بخش ها دسترسی داشته باشید میتوانید از این بخش لایسنس این آموزش را خریداری نمایید .