我的目标是实施一个在用户选择的时间出现的通知。
我已经尝试了Service
,但是问题是它需要很多后台处理(代码在下面)而另一个是AlarmManager
但是这似乎无法正常工作,尤其如此在API 19 + 。我无法在stackoverflow上找到关于类似问题的最新编码良好的教程或有用的答案,所以我转向你。
所有人都知道:如果朋友给你发了Whatsapp,即使你的手机处于关机或睡眠状态(屏幕关闭),你也会收到通知。 我相信你们中的一些人已经实现了这样的目标。
任务正是: 我的应用程序应每天通知用户关于他日历中的预定活动。
例如: 他选择了13:00(我总是使用24小时格式)来提醒他他的事件。 他在第二天安排了2个活动: - 带孩子上学 - 去健身房
第二天13:00,应用会显示通知: “你今天有两个活动!:1)...... 2)......”
使用信息填充通知不是问题。这工作正常,我测试了很多。 我只缺少编码良好的后台线程,可以观察用户选择的时间来显示通知。
请注意:
TimerTask ticks-intervall是1000ms(1秒)。我确定这不是很好的编码,但它的工作原理。 但我正在寻找一种更好,更友好的电池解决方案。
此外,我添加了“android.permission.RECEIVE_BOOT_COMPLETED”和“android.permission.WAKE_LOCK”权限。
该服务被标记为 START_STICKY
的Manifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest ... >
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
<uses-permission android:name="android.permission.WAKE_LOCK" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<service android:name=".services.NotificationService" android:exported="false"/>
<receiver android:name=".services.BootReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
</intent-filter>
</receiver>
<activity
android:name=".activities.MainActivity"
android:label="@string/app_name"
android:screenOrientation="portrait" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
//Other Activities
</application>
</manifest>
BootReceiver:
public class BootReceiver extends WakefulBroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
context.startService(new Intent(context, NotificationService.class));
}
}
NotificationService:
public class NotificationService extends Service{
private Calendar currentCalendar, plannedCalendar;
public static final String FILENAME_SETTINGS = "MySettings";
private static final int MODE_PRIVATE = 0;
SharedPreferences data;
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
return START_STICKY;
}
private Timer timer;
private TimerTask timerTask = new TimerTask() {
@Override
public void run() {
data = getSharedPreferences(FILENAME_SETTINGS, MODE_PRIVATE);
int plannedHour = data.getInt("alarm_hour", 0);
int plannedMinute = data.getInt("alarm_minute", 0);
plannedCalendar = Calendar.getInstance(Locale.getDefault());
plannedCalendar.set(Calendar.MINUTE, plannedMinute);
plannedCalendar.set(Calendar.HOUR_OF_DAY, plannedHour);
plannedCalendar.set(Calendar.SECOND, 0);
plannedCalendar.set(Calendar.MILLISECOND, 0);
//Kalender aktualisieren
currentCalendar = Calendar.getInstance(Locale.getDefault());
currentCalendar.setTimeInMillis(System.currentTimeMillis());
//Check ob es 00:00:00 Uhr ist --> Notification wieder erwuenscht!!!
currentCalendar.set(Calendar.SECOND, 0);
currentCalendar.set(Calendar.MILLISECOND, 0);
if(currentCalendar.get(Calendar.SECOND) == 0 && currentCalendar.get(Calendar.MINUTE) == 0 && currentCalendar.get(Calendar.HOUR_OF_DAY) == 0){
SharedPreferences.Editor editor = data.edit();
editor.putBoolean("notification", false);
editor.apply();
}
//Nochmal aktualisieren weil durch den "Check" manipuliert wurde
currentCalendar = Calendar.getInstance(Locale.getDefault());
currentCalendar.setTimeInMillis(System.currentTimeMillis());
if((currentCalendar.getTimeInMillis() >= plannedCalendar.getTimeInMillis()) & !data.getBoolean("notification", false)) {
PowerManager mgr = (PowerManager) getSystemService(Context.POWER_SERVICE);
PowerManager.WakeLock wakeLock = mgr.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "MyWakeLock");
wakeLock.acquire();
notification();
SharedPreferences.Editor editor = data.edit();
editor.putBoolean("notification", true);
editor.apply();
wakeLock.release();
}
}
};
@Override
public void onCreate(){
super.onCreate();
timer = new Timer();
timer.schedule(timerTask, 0, 1000);
}
@Override
public void onDestroy() {
try{
timer.cancel();
timerTask.cancel();
}catch (Exception e){
e.printStackTrace();
}
Intent intent = new Intent("Intent");
intent.putExtra("VALUE", "blalal");
sendBroadcast(intent);
}
public void notification(){
Context context = getApplicationContext();
Intent intent = new Intent(context, LiveSelectActivity.class);
PendingIntent pen = PendingIntent.getActivity(getBaseContext(), 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder builder = new NotificationCompat.Builder(context)
.setContentTitle(getString(R.string.hineweisTrainingsGeplant))
.setContentText("Content")
.setContentIntent(pen)
.setDefaults(Notification.DEFAULT_ALL)
.setAutoCancel(true)
.setSmallIcon(R.drawable.ic_add_white_24dp);
Notification notification = builder.build();
NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
manager.notify(1, notification);
}
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
}