Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/27.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android自定义时间通知服务/广播/警报管理器_Android_Service_Notifications_Alarmmanager_Receiver - Fatal编程技术网

Android自定义时间通知服务/广播/警报管理器

Android自定义时间通知服务/广播/警报管理器,android,service,notifications,alarmmanager,receiver,Android,Service,Notifications,Alarmmanager,Receiver,我的目标是实现一个在用户选择的时间显示的通知。 我已经尝试了服务,但问题是它需要大量后台处理(代码如下),另一个是AlarmManager,但似乎无法正常工作,尤其是在API 19+上。我在stackoverflow上找不到最新的编码良好的教程,也找不到关于类似问题的有用答案,所以我转向你们 你们都知道:如果朋友发短信给你Whatsapp,你会收到通知,即使你的手机关机或处于睡眠状态(屏幕关闭)。 我相信你们中的一些人已经实现了类似的东西 这项任务正是: 我的应用程序应每天向用户通知一次其日历中

我的目标是实现一个在用户选择的时间显示的通知。 我已经尝试了
服务
,但问题是它需要大量后台处理(代码如下),另一个是
AlarmManager
,但似乎无法正常工作,尤其是在API 19+上。我在stackoverflow上找不到最新的编码良好的教程,也找不到关于类似问题的有用答案,所以我转向你们

你们都知道:如果朋友发短信给你Whatsapp,你会收到通知,即使你的手机关机或处于睡眠状态(屏幕关闭)。 我相信你们中的一些人已经实现了类似的东西

这项任务正是: 我的应用程序应每天向用户通知一次其日历中计划的事件

例如: 他选择了13:00(我一直使用24小时制)来提醒他自己的事件。 他在第二天安排了两项活动: -开车送孩子上学 -去健身房

第二天13:00,应用程序将显示一条通知: “您今天有两个活动!:1)…2)…”

用信息填充通知不是问题。这个很好用,我做了很多测试。 我只丢失了一个精心编码的后台线程,它正在监视用户选择的显示通知的时间

请注意:

  • TimerTask时间间隔为1000ms(1秒)。我相信这不是很好的编码,但它的工作但我正在寻找一种更好、更方便电池的解决方案。

  • 此外,我还添加了“android.permission.RECEIVE\u BOOT\u COMPLETED”“android.permission.WAKE\u LOCK”权限

  • 该服务被标记为启动\u粘性

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>
public class BootReceiver extends WakefulBroadcastReceiver{

    @Override
    public void onReceive(Context context, Intent intent) {
        context.startService(new Intent(context, NotificationService.class));
    }
}
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;
    }

}
通知服务:

<?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>
public class BootReceiver extends WakefulBroadcastReceiver{

    @Override
    public void onReceive(Context context, Intent intent) {
        context.startService(new Intent(context, NotificationService.class));
    }
}
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;
    }

}

用于计划您的服务将在所需时间接收和处理的
Intent
s。如果配置正确,它确实可以工作

“我已经试过这个服务了,它能用”——在Android 6.0+上不行。在较旧的设备上,它可以可靠地工作,但用户会因为您造成的功耗而威胁您的生命。“另一个是AlarmManager,但它似乎不能正常工作,特别是在API 19+上。”——这取决于您使用的是什么<例如,code>setAlarmClock()是可靠的,尽管它的UI元素与“闹钟”的比喻紧密相连。“如果朋友发短信给你Whatsapp,即使你的手机关机或处于睡眠状态(屏幕关闭),你也会收到通知”——他们使用谷歌云消息,AFAIK。@Commonware感谢你的快速建议。我以各种想象的方式尝试了AlarmManager:setExakt()、setInexakt()、setInExaktRepeating()等等。问题是,当手机处于睡眠状态时,即使使用RTC_wake_,它也不会将其唤醒。此外,我没有找到api 19+问题的解决方案。因为在一个通知之后,您必须手动设置下一个。这就是为什么我选择了服务。“当手机处于睡眠状态时,即使使用RTC_wake_up,它也不会将其唤醒”——至少在Android 6.0+之前(打瞌睡模式/应用程序待机模式开始干扰),当您没有bug时,其他开发人员也会这样做。“此外,我没有找到api 19+问题的解决方案。因为在一个通知后,您必须手动设置下一个”-同样,它对其他开发人员有效。@Commonware但当手机重新启动时,该过程是否会自动重新启动并显示下一个通知,或者用户是否需要再次启动该应用程序?两者都不是。通常,应用程序会注册
操作\u BOOT\u COMPLETED
并在该点重新安排警报。我已经提到我使用了AlarmManager。我有多个问题,尤其是我的api 21设备。^^这意味着只有一件事-您没有正确配置它。请参阅@commonware评论以了解您的问题