سلام
من میخوام برنامم سر یه ساعت خاص نوتیفیکیشن بده برای همین از سرویس و الارم منیجر استفاده کردم
الان نوتیفیکیشن میاد اما هروقت که دوست داره نوتیفیکیشن میده هیچ قاعده و قانونی هم نداره اصلا نمیدونم مشکلش چیه
این کد های اکتیویتی
SharedPreferences sp = getSharedPreferences("my_prefs", Activity.MODE_PRIVATE);
int show_notif = sp.getInt("show_notification", 1);
PendingIntent pendingIntent;
Intent myIntent = new Intent(this, My_Service_Notification.class);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
if (show_notif == 1) {
pendingIntent = PendingIntent.getService(this, 0, myIntent, 0);
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, 19);
calendar.set(Calendar.MINUTE, 36);
calendar.set(Calendar.SECOND, 0);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 24 * 60 * 60 * 1000, pendingIntent);
} else {
pendingIntent = PendingIntent.getService(this, 0, myIntent, 0);
alarmManager.cancel(pendingIntent);
}
این هم از کدهای قسمت سرویس
public class My_Service_Notification extends Service {
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Random random_text = new Random();
int random_num = random_text.nextInt(4);
String text_notification[] = {"همه قدر قدرتا دارن مسابقه اسم و فامیل میدن، شما نمیای؟", "نمیخوای روی حریفات رو توی اسم و فامیل کم کنی؟", "خیلی وقته آنلاین نشدی، بدو بیا یه دست اسم و فامیل بزنیم", "حوصلت سر نرفته بیای اسم و فامیل بازی کنیم؟", "حواست هست از لیگ اسم و فامیل جا نمونی؟ کلی جایزه منتظرته"};
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_action_name)
.setContentTitle("اسم و فامیل")
.setContentText(text_notification[random_num])
.setVibrate(new long[]{1000, 1000})
.setLights(Color.RED, 2000, 2000)
.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.avatar3))
.setAutoCancel(true);
Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
mBuilder.setSound(alarmSound);
intent = new Intent(this, Activity_splash.class);
PendingIntent pi = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
mBuilder.setContentIntent(pi);
NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(0, mBuilder.build());
return START_STICKY;
}
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
}
ممنون میشم کمکم کنید برنامه نویس...
ما را در سایت برنامه نویس دنبال می کنید
برچسب : نویسنده : خنجی prog بازدید : 237 تاريخ : شنبه 23 ارديبهشت 1396 ساعت: 17:52