我想在每天上午8:00播出节目

时间:2016-02-01 00:13:52

标签: android notifications alarmmanager

我尝试使用闹钟管理器在特定时间(每天上午8:00)向用户发送通知。 但我的代码不正确的工作,请帮助我显示通知

我的MainActivity

  public class AlarmController {

    private Context m_Context;
    private AlarmManager mgr;
    private static final long PERIOD = 1000 * 30;

    public AlarmController(Context context){

        m_Context = context;
        mgr = (AlarmManager)m_Context.getSystemService(Context.ALARM_SERVICE);
    }

    public void StartAlarm(){



        Intent i = new Intent(m_Context, OnAlarmReceiver.class);
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(System.currentTimeMillis());
        calendar.set(Calendar.HOUR_OF_DAY,8);

        PendingIntent pi=PendingIntent.getBroadcast(m_Context, 0,i, PendingIntent.FLAG_UPDATE_CURRENT);
        mgr.setInexactRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
                AlarmManager.INTERVAL_DAY, pi);



        Log.i("AlarmController", "StartAlarm");
    }

    public void StopAlarm(){
        Intent i = new Intent(m_Context, OnAlarmReceiver.class);
        PendingIntent pi=PendingIntent.getBroadcast(m_Context, 0,i, PendingIntent.FLAG_UPDATE_CURRENT);
        mgr.cancel(pi);

        Log.i("AlarmController", "StopAlarm");
    }
}

我的AlarmController

public class OnAlarmReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
context.startService(new Intent(context, AppService.class));

和OnAlarmReceiver

AppService

 public int onStartCommand(Intent intent, int flags, int startId) {
    Log.i(TAG, "start job");

    NotificationCompat.Builder mBuilder =
            new NotificationCompat.Builder(this)
                    .setSmallIcon(R.mipmap.ic_launcher)
                    .setContentTitle("My notification")
                    .setContentText("از اپ سرویس!");

    int mNotificationId = 001;
    NotificationManager mNotifyMgr =
            (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

    mNotifyMgr.notify(mNotificationId, mBuilder.build());
    Log.i(TAG, "stop job");
    return START_STICKY;
}

} }

{{1}}

2 个答案:

答案 0 :(得分:2)

alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmIntent = new Intent(context of current file, AlarmReceiver1.class);

    pendingIntent = PendingIntent.getBroadcast(Menu.this, 0, alarmIntent,PendingIntent.FLAG_UPDATE_CURRENT);
    alarmIntent.setData((Uri.parse("custom://"+System.currentTimeMillis())));
    alarmManager.cancel(pendingIntent);

    Calendar alarmStartTime = Calendar.getInstance();
    Calendar now = Calendar.getInstance();
    alarmStartTime.set(Calendar.HOUR_OF_DAY, 8);
    alarmStartTime.set(Calendar.MINUTE, 00);
    alarmStartTime.set(Calendar.SECOND, 0);
    if (now.after(alarmStartTime)) {
        Log.d("Hey","Added a day");
        alarmStartTime.add(Calendar.DATE, 1);
    }

     alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, alarmStartTime.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pendingIntent);
     Log.d("Alarm","Alarms set for everyday 8 am.");

来到广播接收器课程。您需要在清单中注册您的广播接收器。这将导致您接收时钟事件。     覆盖此广播接收器的onReceive方法并在其中进行通知或制作单独的通知构建服务,并在那里构建和显示您的通知。

清单代码段:

<receiver android:name="AlarmReceiver1"  android:enabled="true">

广播接收器代码段:

public class AlarmReceiver1 extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
      Intent service1 = new Intent(context, NotificationService1.class);
  service1.setData((Uri.parse("custom://"+System.currentTimeMillis())));
              context.startService(service1);
}

通知构建服务代码段:

public class NotificationService1 extends IntentService{

    private NotificationManager notificationManager;
    private PendingIntent pendingIntent;
    private static int NOTIFICATION_ID = 1;
    Notification notification;
@Override
    protected void onHandleIntent(Intent intent) {
ontext context = this.getApplicationContext();
           notificationManager = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
 Intent mIntent = new Intent(this, Activity to be opened after clicking on the notif);
            Bundle bundle = new Bundle();
            bundle.putString("test", "test");
            mIntent.putExtras(bundle);
            pendingIntent = PendingIntent.getActivity(context, 0, mIntent, PendingIntent.FLAG_UPDATE_CURRENT);    

            Resources res = this.getResources();
            NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
            Uri soundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM);
            notification = new NotificationCompat.Builder(this)
                        .setContentIntent(pendingIntent)
                        .setSmallIcon(R.drawable.ic_launcher)
                        .setLargeIcon(BitmapFactory.decodeResource(res, R.drawable.ic_launcher))
                        .setTicker("ticker value")
                        .setAutoCancel(true)
                        .setPriority(8)
                        .setSound(soundUri)
                        .setContentTitle("Notif title")
                        .setContentText("Text").build();
            notification.flags |= Notification.FLAG_AUTO_CANCEL | Notification.FLAG_SHOW_LIGHTS;
            notification.defaults |= Notification.DEFAULT_SOUND | Notification.DEFAULT_VIBRATE;
            notification.ledARGB = 0xFFFFA500;
            notification.ledOnMS = 800;
            notification.ledOffMS = 1000;
            notificationManager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
            notificationManager.notify(NOTIFICATION_ID, notification);
            Log.i("notif","Notifications sent.");

    }

}

答案 1 :(得分:1)

试试这套代码,你的服务永远不会阻止它对我有用。 “mgr.setInexactReapeating”在许多棒棒糖和棉花糖设备中不起作用,一旦它开始失去对服务的控制,这将帮助你。

if (android.os.Build.VERSION.SDK_INT >= 23) {
        piLR = PendingIntent.getBroadcast(context, 0, intentLR,
                PendingIntent.FLAG_UPDATE_CURRENT);
        amLR.setAndAllowWhileIdle(AlarmManager.ELAPSED_REALTIME_WAKEUP,
                interval, piLR);
    } else if (android.os.Build.VERSION.SDK_INT >= 19
            && android.os.Build.VERSION.SDK_INT < 23) {
        piLR = PendingIntent.getBroadcast(context, 0, intentLR,
                PendingIntent.FLAG_UPDATE_CURRENT);
        amLR.setInexactRepeating(AlarmManager.RTC_WAKEUP,
                System.currentTimeMillis(), interval, piLR);
    } else {
        amLR.setRepeating(AlarmManager.RTC_WAKEUP,
                System.currentTimeMillis(), interval, piLR);
    }