آموزش نمایش پنجره هشدار (Notification) و سورس پروژه به زبان اندروید
آموزش نمایش پنجره هشدار (Notification) و سورس پروژه به زبان اندروید
سلام در برنامه نویسی اندروید امروز می خواهیم به شما نحوه یک نمایش یک پیام هشدار یا notification را در اندروید نمایش دهیم. فرض کنید میخواهید به مشتری در سربرگ کشویی موبایل پیغامی مبنی بر یک رویداد را نمایش دهید چیزی مثل روز تولد، سالگرد و یا یک هشدار سیستمی مثلا باتری شما در حال اتمام است خو ما برای این کار برنامه Android Studio را باز میکنیم و یک پروژه جدید باز میکنیم و از کدهای زیر استفاده میکنیم.
در مسیر زیر :
src/NotificationAlert.java
فایل هشدار را قرار می دهید که شامل کدهای زیر می باشد.
import android.os.Bundle; import android.app.Activity; import android.app.Notification; import android.app.NotificationManager; import android.app.PendingIntent; import android.content.Context; import android.content.Intent; public class NotificationAlert extends Activity { private static final int NOTIFY_ME_ID=1337; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.notification_alert); /*********** Create notification ***********/ final NotificationManager mgr= (NotificationManager)this.getSystemService(Context.NOTIFICATION_SERVICE); Notification note=new Notification(R.drawable.stat_notify_chat, "Android Example Status message!", System.currentTimeMillis()); PendingIntent i=PendingIntent.getActivity(this, 0, new Intent(this, NotifyMessage.class), 0); note.setLatestEventInfo(this, "Android Example Notification Title", "This is the android example notification message", i); mgr.notify(NOTIFY_ME_ID, note); } }
و در مسیر زیر :
src/NotifyMessage.java
کد های زیر را قرار می دهید که پنجره هشدار را باز میکند :
import android.app.Activity; import android.os.Bundle; import android.widget.TextView; public class NotifyMessage extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); TextView txt=new TextView(this); txt.setText("Activity after click on notification"); setContentView(txt); } }
پروژه اندروید ما خروجی به شکل زیر خواهد داشت که بسته به نیازتان می توانید کد پیام را در قسمت اول سورس تغییر و اصلاح کنید.
دانلود سورس پروژه :
[button color=”green” size=”medium” link=”http://proje724.ir/wp-content/uploads/2016/10/Notification-Proje724.ir_.rar” icon=”” target=”true”]دانلود سورس پروژه پنجره هشدار اندروید[/button]
دیدگاهتان را بنویسید
برای نوشتن دیدگاه باید وارد بشوید.