انجمن سایت کلیدستان


رتبه موضوع:
  • 1 رای - 3 میانگین
  • 1
  • 2
  • 3
  • 4
  • 5
مشکل ساخت نوتیفیکیشن در کلاس (در برنامه نویسی اندروید)

مشکل ساخت نوتیفیکیشن در کلاس (در برنامه نویسی اندروید)

#1
سلام، من هرجوری سعی میکنم کد ایجاد نوتیفیکیشن رو توی کلاس نمیتونم بنویسم،
لطفاً راهنمایی کنید چه تغییری باید داد تا بتونم توی یک کلاس جدا و مخصوص به خودش بنویسمش

کد پی‌اچ‌پی:
public static void Notificationm(Context context,String Title,String Detils,String packge){
             
NotificationManager nm=(NotificationManagercontext.getSystemService(Context.NOTIFICATION_SERVICE);
             
Notification notify=new Notification(R.drawable.ic_launcher,"شما یک پیام دارید",System.currentTimeMillis());
             
notify.flags |= Notification.FLAG_AUTO_CANCEL//Do not clear the notification
             
notify.flags |= Notification.FLAG_NO_CLEAR//Do not clear the notification
             
notify.flags |= Notification.FLAG_SHOW_LIGHTS//Do not clear the notification
             
notify.defaults |= Notification.DEFAULT_LIGHTS// LED
             
notify.defaults |= Notification.DEFAULT_SOUND// Sound
             
CharSequence title=Title;
             
CharSequence detils=Detils;
              
Intent nazar = new Intent(
                     
Intent.ACTION_VIEW,
                     
Uri.parse("http://cafebazaar.ir/app/"+packge+"/?l=fa"));
             
//Intent intent=new Intent(context,send.class );
             
PendingIntent pend=PendingIntent.getActivity(context0nazar0);
             
notify.setLatestEventInfo(contexttitledetilspend);
             
nm.notify(0,notify);
         } 

یا این کد...

کد پی‌اچ‌پی:
NotificationCompat.Builder mBuilder =   new NotificationCompat.Builder(this)
.
setSmallIcon(R.drawable.ic_launcher// notification icon
.setContentTitle("Notification!"// title for notification
.setContentText("kelidestan.com"// message for notification
.setAutoCancel(true); // clear notification after click
Intent intent1 = new Intent(thisAkhbar.class);
PendingIntent pi PendingIntent.getActivity(this,0,intent1,Intent.FLAG_ACTIVITY_NEW_TASK);
mBuilder.setContentIntent(pi);
NotificationManager mNotificationManager =
            (
NotificationManagergetSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(0mBuilder.build()); 


 
پاسخ

مشکل ساخت نوتیفیکیشن در کلاس (در برنامه نویسی اندروید)

#2
دوستان کد اولی که گذاشتم درست بود
میتونید برای کلاس هاتون از این کد استفاده کنید

کد پی‌اچ‌پی:
public static void Notificationm(Context context,String Title,String Detils,String packge){
             
NotificationManager nm=(NotificationManagercontext.getSystemService(Context.NOTIFICATION_SERVICE);
             
Notification notify=new Notification(R.drawable.ic_launcher,"شما یک پیام دارید",System.currentTimeMillis());
             
notify.flags |= Notification.FLAG_AUTO_CANCEL//Do not clear the notification
             
notify.flags |= Notification.FLAG_NO_CLEAR//Do not clear the notification
             
notify.flags |= Notification.FLAG_SHOW_LIGHTS//Do not clear the notification
             
notify.defaults |= Notification.DEFAULT_LIGHTS// LED
             
notify.defaults |= Notification.DEFAULT_SOUND// Sound
             
CharSequence title=Title;
             
CharSequence detils=Detils;
              
Intent nazar = new Intent(
                     
Intent.ACTION_VIEW,
                     
Uri.parse("http://cafebazaar.ir/app/"+packge+"/?l=fa"));
             
//Intent intent=new Intent(context,send.class );
             
PendingIntent pend=PendingIntent.getActivity(context0nazar0);
             
notify.setLatestEventInfo(contexttitledetilspend);
             
nm.notify(0,notify);
         } 
پاسخ
 سپاس شده توسط admin ، Mohsen.95

مشکل ساخت نوتیفیکیشن در کلاس (در برنامه نویسی اندروید)

#3
دوستان در همین رابطه من کد نوتیفیکیشن رو توی یه کلاس به نام Notif به صورت زیر نوشتم
این کد باید باعث بشه تا ده ثانیه دیر تر نوتیفیکیشن از کلاس Notif اجرا بشه

کد پی‌اچ‌پی:
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;

public class 
Notif {

     public static 
void Notificationm(Context context){
         
NotificationManager nm=(NotificationManagercontext.getSystemService(Context.NOTIFICATION_SERVICE);
         
Notification notify=new Notification(R.drawable.ic_launcher,"شما یک پیام دارید",System.currentTimeMillis());
         
notify.flags |= Notification.FLAG_AUTO_CANCEL//Do not clear the notification
         
notify.flags |= Notification.FLAG_NO_CLEAR//Do not clear the notification
         
notify.flags |= Notification.FLAG_SHOW_LIGHTS//Do not clear the notification
         
notify.defaults |= Notification.DEFAULT_LIGHTS// LED
         
notify.defaults |= Notification.DEFAULT_SOUND// Sound
         
CharSequence title="Title";
         
CharSequence detils="Detils";
          
Intent nazar = new Intent(
                 
Intent.ACTION_VIEW,
                 
Uri.parse("http://cafebazaar.ir/"));
         
//Intent intent=new Intent(context,send.class );
         
PendingIntent pend=PendingIntent.getActivity(context0nazar0);
         
notify.setLatestEventInfo(contexttitledetilspend);
         
nm.notify(0,notify);
         
     }
    


و حالا میخوام با کد زیر توی Main Activity اجراش کنم

کد پی‌اچ‌پی:
new CountDownTimer(10000,1000){
             @
Override
             
public void onTick(long millisUntilFinished){}

             @
Override
                 
public void onFinish(){
                 
// your code
                 
                 
                 
                 
Notif.Notificationm(MainActivity.this);
                 
                 
             }
        }.
start(); 

ولی سر ده ثانیه Force Close میده........ مشکل رو چطور حل کنم؟؟؟؟
(دقت کنید که این کد بدون قسمت تاخییر به درستی اجرا میشه،،، فقط توی کد تاخیر که میذارمش force close میده)


پاسخ

مشکل ساخت نوتیفیکیشن در کلاس (در برنامه نویسی اندروید)

#4
سلام.
درون کد شمارنده زمان، شما ابتدا باید یک شیء (object) از روی کلاس Notif ساخته و سپس از روش Notificationm از آن شیء استفاده کنید.
توصیه می کنم کلید زیر را بخوانید :

کلید شماره 2801

یعنی مشابه کد زیر :

کد پی‌اچ‌پی:
Notif myNotif = new Notif();
myNotif.Notificationm(MainActivity.this); 

bookbook 
لطفا برای درج کد، از دکمه مخصوص درج کد در ادیتور انجمن استفاده کنید.
در مورد برنامه نویسی، مدیران تنها راهنمایی می کنند و نوشتن برنامه نهایی، به عهده کاربران می باشد (اینجا محلی برای یادگیری است، نه سفارش کدنویسی).
کاربران باید ابتدا خود به خطایابی برنامه بپردازند، نه اینکه به محض دیدن خطا، کدها را در انجمن، copy و paste کرده و از مدیران انتظار بررسی داشته باشند.
پاسخ
 سپاس شده توسط kousha.nikkar ، Mohsen.95

مشکل ساخت نوتیفیکیشن در کلاس (در برنامه نویسی اندروید)

#5
درسته جناب ادمین آبجکت رو ایجاد نکرده بودم...
mmamnooooooooooooooooooooooooooooooooooonn
 

 
پاسخ
 سپاس شده توسط admin

مشکل ساخت نوتیفیکیشن در کلاس (در برنامه نویسی اندروید)

#6
(۱۳۹۴/۰۱/۰۹, ۰۳:۴۸ ب.ظ)'kousha.nikkar' نوشته: درسته جناب ادمین آبجکت رو ایجاد نکرده بودم...
فقط این رو توی سرویس که مینویسم اجرا نمیشه... مگه توی سرویس نمیشه این طور نوشت؟؟
 

 

اگر سرویس (Service) هم داشته باشیم، یعنی ترتیب به این صورت می شود :

Activity --> Service --> Class

کدی که در کلاس اجرا می شود، باید Context را داشته باشد، بنابراین باید Context را از Activity به سرویس ارسال کنیم (هنگام ساخت و اجرای سرویس)، بعد در سرویس که از کلاس، یک شیء می سازیم، باید همان Context دریافت شده را برای ساخت شیء از روی کلاس ذکر کنیم.
البته ممکن است در مورد نکته دیگری اشتباه کرده باشید، ولی اون چیزی که به نظرم مهم بود رو خدمتتون عرض کردم. 

bookbook 
لطفا برای درج کد، از دکمه مخصوص درج کد در ادیتور انجمن استفاده کنید.
در مورد برنامه نویسی، مدیران تنها راهنمایی می کنند و نوشتن برنامه نهایی، به عهده کاربران می باشد (اینجا محلی برای یادگیری است، نه سفارش کدنویسی).
کاربران باید ابتدا خود به خطایابی برنامه بپردازند، نه اینکه به محض دیدن خطا، کدها را در انجمن، copy و paste کرده و از مدیران انتظار بررسی داشته باشند.
پاسخ
 سپاس شده توسط kousha.nikkar ، شماره مجازی امارات ، تلگرام ضد فیلتر 2023


پرش به انجمن:


کاربران در حال بازدید این موضوع: 1 مهمان