بروز خطا
[message]
اشتراک در سوال
رای ها
[dataList]
ویجت شمارنده(حل شد)
این یک ویجت شمارش گره .شمارش انجام میشه . دکمه ریست رو که برای بار اول میزنم شمارنده رو صفر میکنه ولی بعدش دکمه ریست خودش هم مثل شمارنده عمل میکنه یعنی دیگه صفر نمیکنه.این هم کدش (کد اصلاح شد و کار کرد)
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.util.Log;
import android.widget.RemoteViews;
public class ExampleProvider extends AppWidgetProvider {
// our actions for our buttons
public static String REFRESH = "refresh";
public static String SHOMARESH = "shomaresh";
static RemoteViews remoteViews;
static PendingIntent actionPendingIntent;
static PendingIntent sactionPendingIntent;
static int mCount = 0;
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget);
Intent active = new Intent(context, ExampleProvider.class);
active.setAction(REFRESH);
actionPendingIntent = PendingIntent.getBroadcast(context, 0, active, 0);
remoteViews.setOnClickPendingIntent(R.id.btnRefresh, actionPendingIntent);
Intent active1 = new Intent(context, ExampleProvider.class);
active1.setAction(SHOMARESH);
sactionPendingIntent = PendingIntent.getBroadcast(context, 1, active1, 0);
remoteViews.setOnClickPendingIntent(R.id.btnZekrShomar, sactionPendingIntent);
appWidgetManager.updateAppWidget(appWidgetIds, remoteViews);
}
private static String getMessage() {
return String.valueOf(mCount++);
}
public static void pushWidgetUpdate(Context context, RemoteViews remoteViews) {
ComponentName myWidget = new ComponentName(context, ExampleProvider.class);
AppWidgetManager manager = AppWidgetManager.getInstance(context);
manager.updateAppWidget(myWidget, remoteViews);
}
public static void updateWidgetRefreshListener(Context context) {
remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget);
mCount = 0;
remoteViews.setTextViewText(R.id.txtCount, "" + mCount);
//REMEMBER TO ALWAYS REFRESH YOUR BUTTON CLICK LISTENERS!!!
remoteViews.setOnClickPendingIntent(R.id.btnRefresh, actionPendingIntent);
pushWidgetUpdate(context, remoteViews);
}
public static void updateWidgetPictureAndButtonListener(Context context) {
remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget);
String message = getMessage();
remoteViews.setTextViewText(R.id.txtCount, message);
//REMEMBER TO ALWAYS REFRESH YOUR BUTTON CLICK LISTENERS!!!
remoteViews.setOnClickPendingIntent(R.id.btnZekrShomar, sactionPendingIntent);
pushWidgetUpdate(context, remoteViews);
}
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals("shomaresh")) {
updateWidgetPictureAndButtonListener(context);
Log.i("LOG", SHOMARESH);
}
else if (intent.getAction().equals("refresh")) {
updateWidgetRefreshListener(context);
Log.i("LOG", REFRESH);
} else {
super.onReceive(context, intent);
}
}
}
0
0
اگه حل شده کد درست رو قرار بدید لطفا تا بقیه هم استفاده کنن (11 سال پیش)
+1
0
کد بالا اصلاح شده است و برای استفاده دوستان آماده . (11 سال پیش)
برای این سوال پاسخی وجود ندارد.
پاسخگویی و مشاهده پاسخ های این سوال تنها برای اعضای ویژه سایت امکان پذیر است .
چنانچه تمایل دارید به همه بخش ها دسترسی داشته باشید میتوانید از این بخش لایسنس این آموزش را خریداری نمایید .