gpt4 book ai didi

android - 如何在 android 中使用时间选择器创建每日自定义通知?

转载 作者:行者123 更新时间:2023-11-30 00:19:55 25 4
gpt4 key购买 nike

我想要什么:-

  • 制作一个在用户给定时间发出通知的应用。
  • 它也应该在后台运行。
  • 我可以在 SharedPreferences 数据库中保存时间小时和分钟 24 小时格式,以便我可以再次覆盖时间。
  • Else部分,如何不通知?

但我得到了什么

我做了这个,但我面临几个问题:-

  1. 我可以发出通知,但只能在我的应用程序处于运行状态时发出。
  2. 无法将变量 Xhours 和 Xminute 存储到 SharedPrefrences 数据库中。
  3. 我的广播接收器类和通知类工作正常,但在应用未运行时无法收到通知。访问这个:> link查看屏幕截图

Please Help, Thanks.

我的 settings_aztro.java Activity

    public void TimePickerDialog() {


TimeTv_settings.setOnClickListener(new View.OnClickListener() {
@SuppressWarnings("deprecation")
@Override
public void onClick(View v) {

showDialog(DIALOG_ID);
}
});

}

@SuppressWarnings("deprecation")
@Override
protected Dialog onCreateDialog(int id) {
if (id == DIALOG_ID) {
return new TimePickerDialog(settings_aztro.this, mTimePickerListener, Xhour, Xminute, false);
} else {
Toast.makeText(this, "Unable to locate time picker", Toast.LENGTH_SHORT).show();
return null;

}

}




protected TimePickerDialog.OnTimeSetListener mTimePickerListener = new TimePickerDialog.OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute)
{
// This will give 24hr value for the notification
Xhour = hourOfDay;
Xminute = minute;

// This will show the 12 hour time on the TextView
YHour = hourOfDay;
Yminute = minute;


if (YHour == 0) {
YHour += 12;
format = "AM";
} else if (YHour == 12) {
format = "PM";
} else if (YHour > 12) {
YHour -= 12;
format = "PM";
} else {
format = "AM";
}
SharedPreferences sharedPreferences = getSharedPreferences("timeinfo", Context.MODE_ENABLE_WRITE_AHEAD_LOGGING);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putInt("keyHour",Xhour);
editor.putInt("keyMinute",Xminute);
editor.commit();
TimeTv_settings.setText(YHour + ":" + Yminute + " " + format);
if(NotifyMe==true)
{
SharedPreferences xsharedPreferences = getSharedPreferences("timeinfo", Context.MODE_ENABLE_WRITE_AHEAD_LOGGING);
savedHour = xsharedPreferences.getInt("keyHour", Integer.parseInt(""));
savedMinute = xsharedPreferences.getInt("keyMinute",Integer.parseInt(""));

Toast.makeText(settings_aztro.this, "YHour: "+YHour+"Ymin: "+Yminute+" "+format+"\tNotify: "+NotifyMe, Toast.LENGTH_SHORT).show();
generateCustomNotification();
}
else
{
NotifyMe = false;

}
}
};

public void generateCustomNotification()
{

CustomCalendar.set(Calendar.HOUR_OF_DAY,savedHour);
CustomCalendar.set(Calendar.MINUTE,savedMinute);
CustomCalendar.set(Calendar.SECOND,0);

Intent CallReciverIntent = new Intent(settings_aztro.this,NotificationBReceiver.class);
PendingIntent pIntent = PendingIntent.getBroadcast(settings_aztro.this,0,CallReciverIntent,PendingIntent.FLAG_UPDATE_CURRENT);

AlarmManager alarmManager = (AlarmManager) settings_aztro.this.getSystemService(ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP,CustomCalendar.getTimeInMillis(),pIntent);
Log.d("samay","Hour-> "+Xhour+" Minute-> "+Xminute+"Time in milis -> "+CustomCalendar.getTimeInMillis());

}


public void saveNotificationTime()
{

}



@Override
public void onClick(View v)
{

switch (v.getId())
{
case R.id.About_settingsTv2:
Toast.makeText(this, "C", Toast.LENGTH_SHORT).show();
generateCustomNotification();
Log.d("Timeqq","Hour-> "+Xhour+" Minute-> "+Xminute);
Xhour = 0;Xminute=0;
break;

case R.id.FacebookSignin_settings:

Log.d("newtime","hour= "+Xhour+" minute= "+Xminute+"Time in milis -> "+CustomCalendar.getTimeInMillis()+"Notify "+NotifyMe);
Log.d("newtime","hour= "+savedHour+" minute= "+savedMinute+"Time in milis -> "+CustomCalendar.getTimeInMillis()+"Notify "+NotifyMe);
break;


}


}

@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
{
if(switch_settings.isChecked())
{
NotifyMe = true;
TimeRLayout.setVisibility(View.VISIBLE);
Toast.makeText(this, "On", Toast.LENGTH_SHORT).show();
Toast.makeText(this, "Hr -> "+Xhour+"Min -> "+Xminute, Toast.LENGTH_SHORT).show();
Log.d("Waqt","Hour-> "+Xhour+" Minute-> "+Xminute);

}
else if(!switch_settings.isChecked())
{
NotifyMe = false;
Toast.makeText(this, "Off", Toast.LENGTH_SHORT).show();
if(NotifyMe==false)
{
TimeRLayout.setVisibility(View.GONE);

}
else
{

}

}
}

我的接收器类

 public class NotificationBReceiver extends BroadcastReceiver
{
int MID = 98;
@Override
public void onReceive(Context context, Intent intent)
{

long when = System.currentTimeMillis();
NotificationManager nManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
Intent notificationIntent = new Intent(context, list_of_signs.class);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

PendingIntent pendingIntent = PendingIntent.getActivity(context, 10,
notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);

NotificationCompat.Builder NotificationBuilder = new NotificationCompat.Builder(context)
.setSmallIcon(R.drawable.aztro2)
.setSound(defaultSoundUri)
.setContentTitle("Hello! Check Your Today's Horoscope.")
.setAutoCancel(true)
.setWhen(when)
.setContentIntent(pendingIntent)
.setVibrate(new long[]{1000, 1000, 1000, 1000, 1000});
nManager.notify(MID, NotificationBuilder.build());
MID++;;

list 文件

<uses-permission android:name="android.permission.INTERNET"/>

<application
android:allowBackup="true"
android:icon="@mipmap/ic_main_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/NoActionBar">
<activity android:name=".list_of_signs">
<!--list_of_signs-->
<intent-filter>
<action android:name="android.intent.action.MAIN"/>

<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
<activity android:name=".MainActivity"/>
<activity android:name=".AllSignsData.class_aquarius"/>
<activity android:name=".AllSignsData.class_aries"/>
<activity android:name=".AllSignsData.class_cancer"/>
<activity android:name=".AllSignsData.class_gemini"/>
<activity android:name=".AllSignsData.class_capricorn"/>
<activity android:name=".AllSignsData.class_leo"/>
<activity android:name=".AllSignsData.class_libra"/>
<activity android:name=".AllSignsData.class_pisces"/>
<activity android:name=".AllSignsData.class_sagittarius"/>
<activity android:name=".AllSignsData.class_scorpio"/>
<activity android:name=".AllSignsData.class_taurus"/>
<activity android:name=".AllSignsData.class_virgo"/>

<activity android:name=".settings_aztro"/>



<receiver android:name=".NotificationBReceiver"/>
</application>

</manifest>

最佳答案

好的,这就是警报管理器的模式。在此实现中,根据 Android O 最佳实践,没有使用/也不应该使用静态定义的广播接收器来调度任务,简而言之,如果您想要后台通知,请忘记 BROADCAST RECEIVER

1) 创建一个 Intent 服务(例如 ReminderAlarmService)来发布您的通知。该服务将以 alarmManager 使用的未决 Intent 启动。 onHandleIntent 应该创建通知并显示它:例如。

ReminderAlarmService.class
@Override
protected void onHandleIntent(Intent intent) {

//..... Create your notification here
manager.notify(NOTIFICATION_ID, notification);

//..... Persist your data here in sharedPreferences here too, with or without user action
}

2) 在该服务中,创建一个公共(public) util 方法以返回“操作”挂起 Intent 以启动上述 Intent 服务(也称为深层链接 Intent )ex。

public static PendingIntent getReminderPendingIntent(Context context, Uri uri) {
Intent action = new Intent(context, ReminderAlarmService.class);
action.setData(uri);
return PendingIntent.getService(context, 0, action, PendingIntent.FLAG_UPDATE_CURRENT);
}

3) 创建一个util类来获取报警管理器的一个实例,用它来设置时间和用上面的方法创建的“操作” Intent 。操作 Intent 启动 Intent 服务,它会执行您在 onHandleIntent 中的任何操作。例如。

public class AlarmScheduler {

public static void scheduleAlarm(Context context, long alarmTime, Uri reminderTask) {
AlarmManager manager = AlarmManagerProvider.getAlarmManager(context);

PendingIntent operation =
ReminderAlarmService.getReminderPendingIntent(context, reminderTask);

manager.setExact(AlarmManager.RTC, time, operation);
}
}

总体设计流程:

  1. AlarmManager 安排警报与待定 Intent 在时间选择器作为参数的时间启动您的服务。
  2. 当时间到来时,您的 Intent 服务将由挂起的 Intent 启动
  3. 在 Intent 服务中,构建并显示您的通知
  4. 当您的 Intent 服务完成工作时,服务会自动停止,干净。

关于android - 如何在 android 中使用时间选择器创建每日自定义通知?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46507974/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com