PDA

View Full Version : صدا زدن یک متد از یک Activity در یک Class



nasr
شنبه 24 مرداد 1394, 16:05 عصر
سلام
من یک Activity دارم با نام LoginActivity
توی این Activity یک متد نوشته ام به این صورت


public void showNotification() {
PendingIntent pi = PendingIntent.getActivity(this, 0, new Intent(this, SmsRecive.class), 0);
Resources r = getResources();
Notification notification = new NotificationCompat.Builder(this)
.setTicker(r.getString(R.string.notification_title ))
.setSmallIcon(android.R.drawable.ic_menu_report_im age)
.setContentTitle(r.getString(R.string.notification _title))
.setContentText(r.getString(R.string.notification_ text))
.setContentIntent(pi)
.setAutoCancel(true)
.build();


NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
notificationManager.notify(0, notification);
}





حالا مثلا روی OnClick یک دکمه در همین Activity وقتی اجرا می کنم به خوبی اجرا میشه و مشکلی ندارم

حالا می خوام همین متد را در یک Class دیگه استفاده کنم
یه کلاس دارم با نام IncommingSMS که وقتی پیامک میاد یک سری کارهایی انجام میده
من به این روش از این متد اسفاده می کنم ولی ظاهرا درست نیستچ
LoginActivity laginAct = new LoginActivity();
laginAct.showNotification()




سوال من اینه که چطور از این showNotification که در LoginActivity است در بقیه برنامه هم استفاده کنم؟

ممنون

smemamian
شنبه 24 مرداد 1394, 16:07 عصر
سلام
کدهاتون قابل خواندن نیست، اصلاح کنید.

nasr
شنبه 24 مرداد 1394, 16:11 عصر
سلام
کدهاتون قابل خواندن نیست، اصلاح کنید.

اصلاح کردم
ممنون میشم راهنمایی کنید

smemamian
شنبه 24 مرداد 1394, 16:24 عصر
یک کلاس تعریف کنید و توابع پر استفاده را داخل اون بذارید. بعد این تابع هم به این روش فراخوانی کنید:


public void showNotification(Context context) {
PendingIntent pi = PendingIntent.getActivity(context, 0, new Intent(context, SmsRecive.class), 0);
Resources r = context.getResources();
Notification notification = new NotificationCompat.Builder(context)
.setTicker(r.getString(R.string.notification_title ))
.setSmallIcon(android.R.drawable.ic_menu_report_im age)
.setContentTitle(r.getString(R.string.notification _title))
.setContentText(r.getString(R.string.notification_ text))
.setContentIntent(pi)
.setAutoCancel(true)
.build();


NotificationManager notificationManager = (NotificationManager)context.getSystemService(cont ext.NOTIFICATION_SERVICE);
notificationManager.notify(0, notification);
}

nasr
شنبه 24 مرداد 1394, 16:31 عصر
این که همون کد بالاست

من می خوام از این کد در کلاسهای دیگه استفاده کنم

آیا این روش من صحیح است


LoginActivity la = New LoginActivity();
la.showNotification();

smemamian
شنبه 24 مرداد 1394, 17:47 عصر
نشد دیگه ! گفتم یک کلاس جدا تعریف کنید و توابع پر استفاده اتون رو داخل اون بذارید مثل زیر :


public class UtilityFunctions {
public UtilityFunctions(){}

public void showNotification(Context context) {
PendingIntent pi = PendingIntent.getActivity(context, 0, new Intent(context, SmsRecive.class), 0);
Resources r = context.getResources();
Notification notification = new NotificationCompat.Builder(context)
.setTicker(r.getString(R.string.notification_title ))
.setSmallIcon(android.R.drawable.ic_menu_report_im age)
.setContentTitle(r.getString(R.string.notification _title))
.setContentText(r.getString(R.string.notification_ text))
.setContentIntent(pi)
.setAutoCancel(true)
.build();


NotificationManager notificationManager = (NotificationManager)context.getSystemService(cont ext.NOTIFICATION_SERVICE);
notificationManager.notify(0, notification);
}

}


حالا به اون روشی که خودتون می خواهید فراخوانی کنید :


UtilityFunctions ufunctions = new UtilityFunctions();

ufunctions.showNotification(this);

nasr
یک شنبه 25 مرداد 1394, 07:17 صبح
آقا بسیار سپاسگذارم

nasr
دوشنبه 26 مرداد 1394, 08:04 صبح
دوستان من تازه با این انروید آشنا شدم
ممنون میشم راهنمایی کنید

چرا من یک متد را وقتی در Activity مینویسم مشکلی نداره ولی وقتی داخل یک کلاس مینویسم مشکل داره

این دوتا عکس را ببینید

smemamian
دوشنبه 26 مرداد 1394, 17:44 عصر
سلام
سوالاتتون رو در مبحث جدید مطرح کنید.

به صورت کلی، بعضی توابع نیاز به Context دارند تا به درستی کار کنند. بخاطره همین من در تابع قبلی که Notification رو صدا میزد Context رو به عنوان پارامتر
تابع قرار دادم. بعضی توابع دیگه باید داخل کلاس Extends شده ی مربوط به خودشان فقط صدا زده بشن.