android– 我希望每天早上8点播出通知

android– 我希望每天早上8点播出通知,第1张

概述我正在尝试使用警报管理器在特定时间(每天上午8:00)向用户发送通知.但我的代码不正确的工作,请帮助我显示通知我的MainActivity@OverrideprotectedvoidonCreate(BundlesavedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.activ

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

我的MainActivity

  @OverrIDeprotected voID onCreate(Bundle savedInstanceState) {    super.onCreate(savedInstanceState);    setContentVIEw(R.layout.activity_main);    AlarmController al=new AlarmController(this);    al.StartAlarm();}

我的AlarmController

  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");    }}

和OnAlarmReceiver

public class OnAlarmReceiver extends broadcastReceiver {@OverrIDepublic voID onReceive(Context context, Intent intent) {context.startService(new Intent(context, AppService.class));

}
}

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;}

解决方法:

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.");    }}

总结

以上是内存溢出为你收集整理的android – 我希望每天早上8点播出通知全部内容,希望文章能够帮你解决android – 我希望每天早上8点播出通知所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1103044.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-28
下一篇 2022-05-28

发表评论

登录后才能评论

评论列表(0条)

保存